commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r904612 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/StringUtils.java test/java/org/apache/commons/lang/StringUtilsTest.java
Date Fri, 29 Jan 2010 20:02:36 GMT
Author: niallp
Date: Fri Jan 29 20:02:35 2010
New Revision: 904612

URL: http://svn.apache.org/viewvc?rev=904612&view=rev
Log:
Port LANG-471 to 2.x branch - add isAllUpperCase() and isAllLowerCase() to StringUtils

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/StringUtilsTest.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=904612&r1=904611&r2=904612&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
Fri Jan 29 20:02:35 2010
@@ -5508,6 +5508,66 @@
         return true;
     }
 
+    /**
+     * <p>Checks if the String contains only lowercase characters.</p>
+     *
+     * <p><code>null</code> will return <code>false</code>.
+     * An empty String ("") will return <code>false</code>.</p>
+     *
+     * <pre>
+     * StringUtils.isAllLowerCase(null)   = false
+     * StringUtils.isAllLowerCase("")     = false
+     * StringUtils.isAllLowerCase("  ")   = false
+     * StringUtils.isAllLowerCase("abc")  = true
+     * StringUtils.isAllLowerCase("abC") = false
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @return <code>true</code> if only contains lowercase characters, and is
non-null
+     */
+    public static boolean isAllLowerCase(String str) {
+        if (str == null || isEmpty(str)) {
+            return false;
+        }
+        int sz = str.length();
+        for (int i = 0; i < sz; i++) {
+            if (Character.isLowerCase(str.charAt(i)) == false) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * <p>Checks if the String contains only uppercase characters.</p>
+     *
+     * <p><code>null</code> will return <code>false</code>.
+     * An empty String ("") will return <code>false</code>.</p>
+     *
+     * <pre>
+     * StringUtils.isAllUpperCase(null)   = false
+     * StringUtils.isAllUpperCase("")     = false
+     * StringUtils.isAllUpperCase("  ")   = false
+     * StringUtils.isAllUpperCase("ABC")  = true
+     * StringUtils.isAllUpperCase("aBC") = false
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @return <code>true</code> if only contains uppercase characters, and is
non-null
+     */
+    public static boolean isAllUpperCase(String str) {
+        if (str == null || isEmpty(str)) {
+            return false;
+        }
+        int sz = str.length();
+        for (int i = 0; i < sz; i++) {
+            if (Character.isUpperCase(str.charAt(i)) == false) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     // Defaults
     //-----------------------------------------------------------------------
     /**

Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java?rev=904612&r1=904611&r2=904612&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java
Fri Jan 29 20:02:35 2010
@@ -1727,7 +1727,29 @@
         assertEquals("", StringUtils.EMPTY);
         assertEquals(0, StringUtils.EMPTY.length());
     }
-    
+
+    /**
+     * Test for {@link StringUtils#isAllLowerCase(String)}.
+     */
+    public void testIsAllLowerCase() {
+        assertFalse(StringUtils.isAllLowerCase(null));
+        assertFalse(StringUtils.isAllLowerCase(StringUtils.EMPTY));
+        assertTrue(StringUtils.isAllLowerCase("abc"));
+        assertFalse(StringUtils.isAllLowerCase("abc "));
+        assertFalse(StringUtils.isAllLowerCase("abC"));
+    }
+
+    /**
+     * Test for {@link StringUtils#isAllUpperCase(String)}.
+     */
+    public void testIsAllUpperCase() {
+        assertFalse(StringUtils.isAllUpperCase(null));
+        assertFalse(StringUtils.isAllUpperCase(StringUtils.EMPTY));
+        assertTrue(StringUtils.isAllUpperCase("ABC"));
+        assertFalse(StringUtils.isAllUpperCase("ABC "));
+        assertFalse(StringUtils.isAllUpperCase("aBC"));
+    }
+
     public void testRemoveStart() {
         // StringUtils.removeStart("", *)        = ""
         assertNull(StringUtils.removeStart(null, null));



Mime
View raw message