commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r997979 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/StringUtils.java test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java
Date Fri, 17 Sep 2010 05:10:27 GMT
Author: bayard
Date: Fri Sep 17 05:10:27 2010
New Revision: 997979

URL: http://svn.apache.org/viewvc?rev=997979&view=rev
Log:
Adding Shashi Kant Sharma's implementation of endsWithAny - LANG-614

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java?rev=997979&r1=997978&r2=997979&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java Fri
Sep 17 05:10:27 2010
@@ -6310,4 +6310,35 @@ public class StringUtils {
         }
         return WHITESPACE_BLOCK.matcher(trim(str)).replaceAll(" ");         
     }
+
+    /**
+     * <p>Check if a String ends with any of an array of specified strings.</p>
+     *
+     * <pre>
+     * StringUtils.endsWithAny(null, null)      = false
+     * StringUtils.endsWithAny(null, new String[] {"abc"})  = false
+     * StringUtils.endsWithAny("abcxyz", null)     = false
+     * StringUtils.endsWithAny("abcxyz", new String[] {""}) = true
+     * StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}) = true
+     * StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
+     * </pre>
+     *
+     * @param string  the String to check, may be null
+     * @param searchStrings the Strings to find, may be null or empty
+     * @return <code>true</code> if the String ends with any of the the prefixes,
case insensitive, or
+     *  both <code>null</code>
+     * @since 3.1
+     */
+    public static boolean endsWithAny(String string, String... searchStrings) {
+        if (isEmpty(string) || ArrayUtils.isEmpty(searchStrings)) {
+            return false;
+        }
+        for (int i = 0; i < searchStrings.length; i++) {
+            String searchString = searchStrings[i];
+            if (StringUtils.endsWith(string, searchString)) {
+                return true;
+            }
+        }
+        return false;
+    }
 }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java?rev=997979&r1=997978&r2=997979&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java
Fri Sep 17 05:10:27 2010
@@ -135,4 +135,16 @@ public class StringUtilsStartsEndsWithTe
         assertFalse(StringUtils.endsWithIgnoreCase("ABCDEF", "cde"));
     }
 
+    public void testEndsWithAny() {
+        assertFalse("StringUtils.endsWithAny(null, null)", StringUtils.endsWithAny(null,
(String)null));
+        assertFalse("StringUtils.endsWithAny(null, new String[] {abc})", StringUtils.endsWithAny(null,
new String[] {"abc"}));
+        assertFalse("StringUtils.endsWithAny(abcxyz, null)", StringUtils.endsWithAny("abcxyz",
(String)null));
+        assertTrue("StringUtils.endsWithAny(abcxyz, new String[] {\"\"})", StringUtils.endsWithAny("abcxyz",
new String[] {""}));
+        assertTrue("StringUtils.endsWithAny(abcxyz, new String[] {xyz})", StringUtils.endsWithAny("abcxyz",
new String[] {"xyz"}));
+        assertTrue("StringUtils.endsWithAny(abcxyz, new String[] {null, xyz, abc})", StringUtils.endsWithAny("abcxyz",
new String[] {null, "xyz", "abc"}));
+        assertFalse("StringUtils.endsWithAny(defg, new String[] {null, xyz, abc})", StringUtils.endsWithAny("defg",
new String[] {null, "xyz", "abc"}));
+
+    }
+
+
 }



Mime
View raw message