commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r1056871 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/StringUtils.java test/java/org/apache/commons/lang/StringUtilsStartsEndsWithTest.java
Date Sun, 09 Jan 2011 02:51:19 GMT
Author: niallp
Date: Sun Jan  9 02:51:19 2011
New Revision: 1056871

URL: http://svn.apache.org/viewvc?rev=1056871&view=rev
Log:
Port LANG-614 to LANG 2.x Branch - add StringUtils endsWithAny() method

Modified:
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
    commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsStartsEndsWithTest.java

Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java?rev=1056871&r1=1056870&r2=1056871&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
Sun Jan  9 02:51:19 2011
@@ -6530,4 +6530,35 @@ public class StringUtils {
         return b.toString();
     }
 
+    /**
+     * <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 2.6
+     */
+    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/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsStartsEndsWithTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsStartsEndsWithTest.java?rev=1056871&r1=1056870&r2=1056871&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsStartsEndsWithTest.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsStartsEndsWithTest.java
Sun Jan  9 02:51:19 2011
@@ -135,4 +135,16 @@ public class StringUtilsStartsEndsWithTe
         assertFalse(StringUtils.endsWithIgnoreCase("ABCDEF", "cde"));
     }
 
+    public void testEndsWithAny() {
+        assertFalse("StringUtils.endsWithAny(null, null)", StringUtils.endsWithAny(null,
null));
+        assertFalse("StringUtils.endsWithAny(null, new String[] {abc})", StringUtils.endsWithAny(null,
new String[] {"abc"}));
+        assertFalse("StringUtils.endsWithAny(abcxyz, null)", StringUtils.endsWithAny("abcxyz",
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