From 6b9c3315885f8f9e4be19db9ef05257035b97bca Mon Sep 17 00:00:00 2001 From: Martin Date: Sun, 4 Dec 2016 22:13:20 +0100 Subject: [PATCH] LANG-1293: Add StringUtils#isAllEmpty and #isAllBlank methods --- .../org/apache/commons/lang3/StringUtils.java | 48 +++++++++++++++++++ .../lang3/StringUtilsEmptyBlankTest.java | 29 +++++++++++ 2 files changed, 77 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 63caeacba..d91ad5513 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -310,6 +310,29 @@ public class StringUtils { return !isAnyEmpty(css); } + /** + *

Checks if all of the CharSequences are empty ("") or null.

+ * + *
+     * StringUtils.isAllEmpty(null)             = true
+     * StringUtils.isAllEmpty(null, "")         = true
+     * StringUtils.isAllEmpty(new String[] {})  = true
+     * StringUtils.isAllEmpty(null, "foo")      = false
+     * StringUtils.isAllEmpty("", "bar")        = false
+     * StringUtils.isAllEmpty("bob", "")        = false
+     * StringUtils.isAllEmpty("  bob  ", null)  = false
+     * StringUtils.isAllEmpty(" ", "bar")       = false
+     * StringUtils.isAllEmpty("foo", "bar")     = false
+     * 
+ * + * @param css the CharSequences to check, may be null or empty + * @return {@code true} if all of the CharSequences are empty or null + * @since 3.6 + */ + public static boolean isAllEmpty(final CharSequence... css) { + return !isAnyNotEmpty(css); + } + /** *

Checks if a CharSequence is empty (""), null or whitespace only.

* @@ -455,6 +478,31 @@ public class StringUtils { return !isAnyBlank(css); } + /** + *

Checks if all of the CharSequences are empty (""), null or whitespace only.

+ * + *

Whitespace is defined by {@link Character#isWhitespace(char)}.

+ * + *
+     * StringUtils.isAllBlank(null)             = true
+     * StringUtils.isAllBlank(null, "foo")      = false
+     * StringUtils.isAllBlank(null, null)       = true
+     * StringUtils.isAllBlank("", "bar")        = false
+     * StringUtils.isAllBlank("bob", "")        = false
+     * StringUtils.isAllBlank("  bob  ", null)  = false
+     * StringUtils.isAllBlank(" ", "bar")       = false
+     * StringUtils.isAllBlank("foo", "bar")     = false
+     * StringUtils.isAllBlank(new String[] {})  = true
+     * 
+ * + * @param css the CharSequences to check, may be null or empty + * @return {@code true} if all of the CharSequences are empty or null or whitespace only + * @since 3.6 + */ + public static boolean isAllBlank(final CharSequence... css) { + return !isAnyNotBlank(css); + } + // Trim //----------------------------------------------------------------------- /** diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java index 769a138ff..b04d0daef 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java @@ -81,6 +81,21 @@ public class StringUtilsEmptyBlankTest { assertTrue(StringUtils.isNoneEmpty("foo", "bar")); } + @Test + public void testIsAllEmpty() { + assertTrue(StringUtils.isAllEmpty()); + assertTrue(StringUtils.isAllEmpty(new String[]{})); + assertTrue(StringUtils.isAllEmpty((String) null)); + assertTrue(StringUtils.isAllEmpty((String[]) null)); + assertFalse(StringUtils.isAllEmpty(null, "foo")); + assertFalse(StringUtils.isAllEmpty("", "bar")); + assertFalse(StringUtils.isAllEmpty("bob", "")); + assertFalse(StringUtils.isAllEmpty(" bob ", null)); + assertFalse(StringUtils.isAllEmpty(" ", "bar")); + assertFalse(StringUtils.isAllEmpty("foo", "bar")); + assertTrue(StringUtils.isAllEmpty("", null)); + } + @Test public void testIsBlank() { assertTrue(StringUtils.isBlank(null)); @@ -137,4 +152,18 @@ public class StringUtilsEmptyBlankTest { assertFalse(StringUtils.isNoneBlank(" ", "bar")); assertTrue(StringUtils.isNoneBlank("foo", "bar")); } + + @Test + public void testIsAllBlank() { + assertTrue(StringUtils.isAllBlank((String) null)); + assertTrue(StringUtils.isAllBlank((String[]) null)); + assertTrue(StringUtils.isAllBlank(null, null)); + assertTrue(StringUtils.isAllBlank(null, " ")); + assertFalse(StringUtils.isAllBlank(null, "foo")); + assertFalse(StringUtils.isAllBlank("", "bar")); + assertFalse(StringUtils.isAllBlank("bob", "")); + assertFalse(StringUtils.isAllBlank(" bob ", null)); + assertFalse(StringUtils.isAllBlank(" ", "bar")); + assertFalse(StringUtils.isAllBlank("foo", "bar")); + } }