commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject [08/48] [lang] [LANG-1304] StringUtils method to check for mixed case in string (closes #223)
Date Thu, 08 Jun 2017 08:15:42 GMT
[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 <andyklimczak@fastmail.com>
Authored: Thu Dec 29 21:01:12 2016 -0500
Committer: pascalschumacher <pascalschumacher@gmx.net>
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 <action> type attribute can be add,update,fix,remove.
   <body>
 
   <release version="3.6" date="2017-MM-DD" description="TBD">
+    <action issue="LANG-1304" type="add" dev="pschumacher" due-to="Andy Klimczak">Add
method in StringUtils to determine if string contains both mixed cased characters</action>
     <action issue="LANG-1334" type="update" dev="djones">Deprecate CharEncoding in
favour of java.nio.charset.StandardCharsets</action>
     <action issue="LANG-1319" type="fix" dev="djones">MultilineRecursiveToStringStyle
StackOverflowError when object is an array</action>
     <action issue="LANG-1325" type="add" dev="kinow" due-to="Arshad Basha">Increase
test coverage of ToStringBuilder class to 100%</action>

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;
     }
 
+    /**
+     * <p>Checks if the CharSequence contains mixed casing of both uppercase and lowercase
characters.</p>
+     *
+     * <p>{@code null} will return {@code false}. An empty CharSequence ({@code length()=0})
will return
+     * {@code false}.</p>
+     *
+     * <pre>
+     * 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
+     * </pre>
+     *
+     * @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.isMixedCase("/"));
+        assertFalse(StringUtils.isMixedCase("A/"));
+        assertFalse(StringUtils.isMixedCase("/b"));
+        assertFalse(StringUtils.isMixedCase("abc"));
+        assertFalse(StringUtils.isMixedCase("ABC"));
+        assertTrue(StringUtils.isMixedCase("aBc"));
+        assertTrue(StringUtils.isMixedCase("aBc "));
+        assertTrue(StringUtils.isMixedCase("A c"));
+        assertTrue(StringUtils.isMixedCase("aBc\n"));
+        assertTrue(StringUtils.isMixedCase("A1c"));
+        assertTrue(StringUtils.isMixedCase("a/C"));
+    }
+
     @Test
     public void testRemoveStart() {
         // StringUtils.removeStart("", *)        = ""


Mime
View raw message