Author: bayard Date: Tue Jun 30 06:24:20 2009 New Revision: 789573 URL: http://svn.apache.org/viewvc?rev=789573&view=rev Log: Applying Vincent Ricard's patch in LANG-471 (reported by Ivica Mikic) adding isAllUpperCase and isAllLowerCase to StringUtils
Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?rev=789573&r1=789572&r2=789573&view=diff ============================================================================== --- commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java (original) +++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java Tue Jun 30 06:24:20 2009 @@ -4916,6 +4916,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/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java?rev=789573&r1=789572&r2=789573&view=diff ============================================================================== --- commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java (original) +++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java Tue Jun 30 06:24:20 2009 @@ -1541,7 +1541,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));