[lang] [LANG-1304] StringUtils method to check for mixed case in string (closes #223)
Repository: commons-lang Updated Branches: refs/heads/master ebe7bb58a -> ef3015cc9 [LANG-1304] StringUtils method to check for mixed case in string (closes #223) Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/ef3015cc Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/ef3015cc Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/ef3015cc Branch: refs/heads/master Commit: ef3015cc99f7bd51862c1d95014e8332d24c2341 Parents: ebe7bb5 Author: Andy Klimczak Authored: Thu Dec 29 21:01:12 2016 -0500 Committer: pascalschumacher Committed: Thu May 25 16:14:46 2017 +0200 -- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/StringUtils.java | 41 .../apache/commons/lang3/StringUtilsTest.java | 23 +++ 3 files changed, 65 insertions(+) -- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/ef3015cc/src/changes/changes.xml -- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1761d8e..cb2cbf3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -46,6 +46,7 @@ The type attribute can be add,update,fix,remove. +Add method in StringUtils to determine if string contains both mixed cased characters Deprecate CharEncoding in favour of java.nio.charset.StandardCharsets MultilineRecursiveToStringStyle StackOverflowError when object is an array Increase test coverage of ToStringBuilder class to 100% http://git-wip-us.apache.org/repos/asf/commons-lang/blob/ef3015cc/src/main/java/org/apache/commons/lang3/StringUtils.java -- diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 7134853..7f64322 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -7303,6 +7303,47 @@ public class StringUtils { return true; } +/** + * Checks if the CharSequence contains mixed casing of both uppercase and lowercase characters. + * + * {@code null} will return {@code false}. An empty CharSequence ({@code length()=0}) will return + * {@code false}. + * + * + * StringUtils.isMixedCase(null)= false + * StringUtils.isMixedCase("") = false + * StringUtils.isMixedCase("ABC") = false + * StringUtils.isMixedCase("abc") = false + * StringUtils.isMixedCase("aBc") = true + * StringUtils.isMixedCase("A c") = true + * StringUtils.isMixedCase("A1c") = true + * StringUtils.isMixedCase("a/C") = true + * StringUtils.isMixedCase("aC\t") = true + * + * + * @param cs the CharSequence to check, may be null + * @return {@code true} if the CharSequence contains both uppercase and lowercase characters + * @since 3.5 + */ +public static boolean isMixedCase(final CharSequence cs) { +if (isEmpty(cs) || cs.length() == 1) { +return false; +} +boolean containsUppercase = false; +boolean containsLowercase = false; +final int sz = cs.length(); +for (int i = 0; i < sz; i++) { +if (containsUppercase && containsLowercase) { +return true; +} else if (Character.isUpperCase(cs.charAt(i))) { +containsUppercase = true; +} else if (Character.isLowerCase(cs.charAt(i))) { +containsLowercase = true; +} +} +return containsUppercase && containsLowercase; +} + // Defaults //--- /** http://git-wip-us.apache.org/repos/asf/commons-lang/blob/ef3015cc/src/test/java/org/apache/commons/lang3/StringUtilsTest.java -- diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 3051b47..2b49669 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -2581,6 +2581,29 @@ public class StringUtilsTest { assertFalse(StringUtils.isAllUpperCase("A/C")); } +/** + * Test for {@link StringUtils#isMixedCase(CharSequence)}. + */ +@Test +public void testIsMixedCase() { +assertFalse(StringUtils.isMixedCase(null)); +assertFalse(StringUtils.isMixedCase(StringUtils.EMPTY)); +assertFalse(StringUtils.isMixedCase(" ")); +assertFalse(StringUtils.isMixedCase("A"));
[08/48] [lang] [LANG-1304] StringUtils method to check for mixed case in string (closes #223)
[LANG-1304] StringUtils method to check for mixed case in string (closes #223) Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/ef3015cc Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/ef3015cc Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/ef3015cc Branch: refs/heads/release Commit: ef3015cc99f7bd51862c1d95014e8332d24c2341 Parents: ebe7bb5 Author: Andy Klimczak Authored: Thu Dec 29 21:01:12 2016 -0500 Committer: pascalschumacher Committed: Thu May 25 16:14:46 2017 +0200 -- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/StringUtils.java | 41 .../apache/commons/lang3/StringUtilsTest.java | 23 +++ 3 files changed, 65 insertions(+) -- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/ef3015cc/src/changes/changes.xml -- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1761d8e..cb2cbf3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -46,6 +46,7 @@ The type attribute can be add,update,fix,remove. +Add method in StringUtils to determine if string contains both mixed cased characters Deprecate CharEncoding in favour of java.nio.charset.StandardCharsets MultilineRecursiveToStringStyle StackOverflowError when object is an array Increase test coverage of ToStringBuilder class to 100% http://git-wip-us.apache.org/repos/asf/commons-lang/blob/ef3015cc/src/main/java/org/apache/commons/lang3/StringUtils.java -- diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 7134853..7f64322 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -7303,6 +7303,47 @@ public class StringUtils { return true; } +/** + * Checks if the CharSequence contains mixed casing of both uppercase and lowercase characters. + * + * {@code null} will return {@code false}. An empty CharSequence ({@code length()=0}) will return + * {@code false}. + * + * + * StringUtils.isMixedCase(null)= false + * StringUtils.isMixedCase("") = false + * StringUtils.isMixedCase("ABC") = false + * StringUtils.isMixedCase("abc") = false + * StringUtils.isMixedCase("aBc") = true + * StringUtils.isMixedCase("A c") = true + * StringUtils.isMixedCase("A1c") = true + * StringUtils.isMixedCase("a/C") = true + * StringUtils.isMixedCase("aC\t") = true + * + * + * @param cs the CharSequence to check, may be null + * @return {@code true} if the CharSequence contains both uppercase and lowercase characters + * @since 3.5 + */ +public static boolean isMixedCase(final CharSequence cs) { +if (isEmpty(cs) || cs.length() == 1) { +return false; +} +boolean containsUppercase = false; +boolean containsLowercase = false; +final int sz = cs.length(); +for (int i = 0; i < sz; i++) { +if (containsUppercase && containsLowercase) { +return true; +} else if (Character.isUpperCase(cs.charAt(i))) { +containsUppercase = true; +} else if (Character.isLowerCase(cs.charAt(i))) { +containsLowercase = true; +} +} +return containsUppercase && containsLowercase; +} + // Defaults //--- /** http://git-wip-us.apache.org/repos/asf/commons-lang/blob/ef3015cc/src/test/java/org/apache/commons/lang3/StringUtilsTest.java -- diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 3051b47..2b49669 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -2581,6 +2581,29 @@ public class StringUtilsTest { assertFalse(StringUtils.isAllUpperCase("A/C")); } +/** + * Test for {@link StringUtils#isMixedCase(CharSequence)}. + */ +@Test +public void testIsMixedCase() { +assertFalse(StringUtils.isMixedCase(null)); +assertFalse(StringUtils.isMixedCase(StringUtils.EMPTY)); +assertFalse(StringUtils.isMixedCase(" ")); +assertFalse(StringUtils.isMixedCase("A")); +assertFalse(StringUtils.isMixedCase("a")); +assertFalse(StringUtils.i