commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r1535515 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/StringUtils.java test/java/org/apache/commons/lang3/StringUtilsTest.java
Date Thu, 24 Oct 2013 19:54:48 GMT
Author: bayard
Date: Thu Oct 24 19:54:47 2013
New Revision: 1535515

URL: http://svn.apache.org/r1535515
Log:
Applying Alexander Muthmann's patch from LANG-848, adding isBlank/isEmpty CharSequence...
variants

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java?rev=1535515&r1=1535514&r2=1535515&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java Thu
Oct 24 19:54:47 2013
@@ -236,7 +236,54 @@ public class StringUtils {
     public static boolean isNotEmpty(final CharSequence cs) {
         return !StringUtils.isEmpty(cs);
     }
-
+       
+    /**
+     * <p>Checks if any one of the CharSequences are empty ("") or null.</p>
+     *
+     * <pre>
+     * StringUtils.isAnyEmpty(null)             = true
+     * StringUtils.isAnyEmpty(null, "foo")      = true
+     * StringUtils.isAnyEmpty("", "bar")        = true
+     * StringUtils.isAnyEmpty("bob", "")        = true
+     * StringUtils.isAnyEmpty("  bob  ", null)  = true
+     * StringUtils.isAnyEmpty(" ", "bar")       = false
+     * StringUtils.isAnyEmpty("foo", "bar")     = false
+     * </pre>
+     *
+     * @param css  the CharSequences to check, may be null or empty
+     * @return {@code true} if any of the CharSequences are empty or null
+     */
+    public static boolean isAnyEmpty(CharSequence... css) {
+      if (ArrayUtils.isEmpty(css)) {
+        return true;
+      }
+      for (CharSequence cs : css){
+        if (isEmpty(cs)) {
+          return true;
+        }
+      }
+      return false;
+    }
+    
+    /**
+     * <p>Checks if none of the CharSequences are empty ("") or null.</p>
+     *
+     * <pre>
+     * StringUtils.isNoneEmpty(null)             = false
+     * StringUtils.isNoneEmpty(null, "foo")      = false
+     * StringUtils.isNoneEmpty("", "bar")        = false
+     * StringUtils.isNoneEmpty("bob", "")        = false
+     * StringUtils.isNoneEmpty("  bob  ", null)  = false
+     * StringUtils.isNoneEmpty(" ", "bar")       = true
+     * StringUtils.isNoneEmpty("foo", "bar")     = true
+     * </pre>
+     *
+     * @param css  the CharSequences to check, may be null or empty
+     * @return {@code true} if none of the CharSequences are empty or null
+     */
+    public static boolean isNoneEmpty(CharSequence... css) {
+      return !isAnyEmpty(css);
+    }    
     /**
      * <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
      *
@@ -286,6 +333,56 @@ public class StringUtils {
     public static boolean isNotBlank(final CharSequence cs) {
         return !StringUtils.isBlank(cs);
     }
+    
+        /**
+     * <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace
only..</p>
+     *
+     * <pre>
+     * StringUtils.isAnyBlank(null)             = true
+     * StringUtils.isAnyBlank(null, "foo")      = true
+     * StringUtils.isAnyBlank(null, null)       = true
+     * StringUtils.isAnyBlank("", "bar")        = true
+     * StringUtils.isAnyBlank("bob", "")        = true
+     * StringUtils.isAnyBlank("  bob  ", null)  = true
+     * StringUtils.isAnyBlank(" ", "bar")       = true
+     * StringUtils.isAnyBlank("foo", "bar")     = false
+     * </pre>
+     *
+     * @param css  the CharSequences to check, may be null or empty
+     * @return {@code true} if any of the CharSequences are blank or null or whitespace only
+     */
+    public static boolean isAnyBlank(CharSequence... css) {
+      if (ArrayUtils.isEmpty(css)) {
+        return true;
+      }
+      for (CharSequence cs : css){
+        if (isBlank(cs)) {
+          return true;
+        }
+      }
+      return false;
+    }
+    
+    /**
+     * <p>Checks if none of the CharSequences are blank ("") or null and whitespace
only..</p>
+     *
+     * <pre>
+     * StringUtils.isNoneBlank(null)             = false
+     * StringUtils.isNoneBlank(null, "foo")      = false
+     * StringUtils.isNoneBlank(null, null)       = false
+     * StringUtils.isNoneBlank("", "bar")        = false
+     * StringUtils.isNoneBlank("bob", "")        = false
+     * StringUtils.isNoneBlank("  bob  ", null)  = false
+     * StringUtils.isNoneBlank(" ", "bar")       = false
+     * StringUtils.isNoneBlank("foo", "bar")     = true
+     * </pre>
+     *
+     * @param css  the CharSequences to check, may be null or empty
+     * @return {@code true} if none of the CharSequences are blank or null or whitespace
only
+     */
+    public static boolean isNoneBlank(CharSequence... css) {
+      return !isAnyBlank(css);
+    }
 
     // Trim
     //-----------------------------------------------------------------------

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java?rev=1535515&r1=1535514&r2=1535515&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Thu Oct 24 19:54:47 2013
@@ -125,6 +125,89 @@ public class StringUtilsTest {
     
     //-----------------------------------------------------------------------
     @Test
+    public void testisEmpty(){
+      assertTrue(StringUtils.isEmpty(null));
+      assertTrue(StringUtils.isEmpty(""));
+      assertFalse(StringUtils.isEmpty(" "));
+      assertFalse(StringUtils.isEmpty("bob"));
+      assertFalse(StringUtils.isEmpty("  bob  "));
+    }
+    
+    @Test
+    public void testisNotEmpty(){
+      assertFalse(StringUtils.isNotEmpty(null));
+      assertFalse(StringUtils.isNotEmpty(""));
+      assertTrue(StringUtils.isNotEmpty(" "));
+      assertTrue(StringUtils.isNotEmpty("bob"));
+      assertTrue(StringUtils.isNotEmpty("  bob  "));
+    }
+    
+    @Test
+    public void testisAnyEmpty(){
+      assertTrue(StringUtils.isAnyEmpty(null));
+      assertTrue(StringUtils.isAnyEmpty(null, "foo"));
+      assertTrue(StringUtils.isAnyEmpty("", "bar"));
+      assertTrue(StringUtils.isAnyEmpty("bob", ""));
+      assertTrue(StringUtils.isAnyEmpty("  bob  ", null));
+      assertFalse(StringUtils.isAnyEmpty(" ","bar"));
+      assertFalse(StringUtils.isAnyEmpty("foo","bar"));
+    }
+    
+    @Test
+    public void testisNoneEmpty(){
+      assertFalse(StringUtils.isNoneEmpty(null));
+      assertFalse(StringUtils.isNoneEmpty(null, "foo"));
+      assertFalse(StringUtils.isNoneEmpty("", "bar"));
+      assertFalse(StringUtils.isNoneEmpty("bob", ""));
+      assertFalse(StringUtils.isNoneEmpty("  bob  ", null));
+      assertTrue(StringUtils.isNoneEmpty(" ", "bar"));
+      assertTrue(StringUtils.isNoneEmpty("foo", "bar"));
+    }
+    
+    @Test
+    public void testisBlank(){
+      assertTrue(StringUtils.isBlank(null));
+      assertTrue(StringUtils.isBlank(""));
+      assertTrue(StringUtils.isBlank(" "));
+      assertFalse(StringUtils.isBlank("bob"));
+      assertFalse(StringUtils.isBlank("  bob  "));
+    }
+    
+    @Test
+    public void testisNotBlank(){
+      assertFalse(StringUtils.isNotBlank(null));
+      assertFalse(StringUtils.isNotBlank(""));
+      assertFalse(StringUtils.isNotBlank(" "));
+      assertTrue(StringUtils.isNotBlank("bob"));
+      assertTrue(StringUtils.isNotBlank("  bob  "));
+    }
+    
+     @Test
+    public void testisAnyBlank(){
+      assertTrue(StringUtils.isAnyBlank(null));
+      assertTrue(StringUtils.isAnyBlank(null, "foo"));
+      assertTrue(StringUtils.isAnyBlank(null, null));
+      assertTrue(StringUtils.isAnyBlank("", "bar"));
+      assertTrue(StringUtils.isAnyBlank("bob", ""));
+      assertTrue(StringUtils.isAnyBlank("  bob  ", null));
+      assertTrue(StringUtils.isAnyBlank(" ","bar"));
+      assertFalse(StringUtils.isAnyBlank("foo","bar"));
+    }
+    
+    @Test
+    public void testisNoneBlank(){
+      assertFalse(StringUtils.isNoneBlank(null));
+      assertFalse(StringUtils.isNoneBlank(null, "foo"));
+      assertFalse(StringUtils.isNoneBlank(null, null));
+      assertFalse(StringUtils.isNoneBlank("", "bar"));
+      assertFalse(StringUtils.isNoneBlank("bob", ""));
+      assertFalse(StringUtils.isNoneBlank("  bob  ", null));
+      assertFalse(StringUtils.isNoneBlank(" ", "bar"));
+      assertTrue(StringUtils.isNoneBlank("foo", "bar"));
+    }
+    
+    //-----------------------------------------------------------------------
+    @Test
     public void testCaseFunctions() {
         assertEquals(null, StringUtils.upperCase(null));
         assertEquals(null, StringUtils.upperCase(null, Locale.ENGLISH));



Mime
View raw message