Repository: commons-lang
Updated Branches:
  refs/heads/master 9ea0063bc -> 672cd146f


LANG-1392: Methods for getting first non empty or non blank value (closes #325)


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/152e5d48
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/152e5d48
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/152e5d48

Branch: refs/heads/master
Commit: 152e5d48eac990bb3e08b2409816738020ba9a01
Parents: 9ea0063
Author: Jeff Nelson <jeff.nel...@thebernardgroup.com>
Authored: Thu Apr 19 15:00:19 2018 -0500
Committer: pascalschumacher <pascalschumac...@gmx.net>
Committed: Fri Jun 8 18:10:23 2018 +0200

----------------------------------------------------------------------
 .../org/apache/commons/lang3/StringUtils.java   | 66 ++++++++++++++++++++
 .../lang3/StringUtilsEmptyBlankTest.java        | 28 +++++++++
 2 files changed, 94 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/152e5d48/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 66a2960..4e421d1 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -7479,6 +7479,72 @@ public class StringUtils {
     }
 
     /**
+     * <p>Returns the first value in the array which is not blank.
+     * If all the values are blank or the array is {@code null}
+     * or empty then {@code null} is returned.</p>
+     *
+     * <pre>
+     * StringUtils.firstNonBlank(null, null, null)   = null
+     * StringUtils.firstNonBlank(null, "", " ")      = null
+     * StringUtils.firstNonBlank(null, null, " ")     = null
+     * StringUtils.firstNonBlank("abc")              = "abc"
+     * StringUtils.firstNonBlank(null, "xyz")        = "xyz"
+     * StringUtils.firstNonBlank(null, "xyz", "abc") = "xyz"
+     * StringUtils.firstNonBlank()                   = null
+     * </pre>
+     *
+     * @param <T> the specific kind of CharSequence
+     * @param values  the values to test, may be {@code null} or empty
+     * @return the first value from {@code values} which is not blank,
+     *  or {@code null} if there are no non-blank values
+     * @since 3.8
+     */
+    @SafeVarargs
+    public static <T extends CharSequence> T firstNonBlank(final T... values) {
+        if (values != null) {
+            for (final T val : values) {
+                if (isNotBlank(val)) {
+                    return val;
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * <p>Returns the first value in the array which is not empty.
+     * If all the values are empty or the array is {@code null}
+     * or empty then {@code null} is returned.</p>
+     *
+     * <pre>
+     * StringUtils.firstNonBlank(null, null, null)   = null
+     * StringUtils.firstNonBlank(null, "", " ")      = " "
+     * StringUtils.firstNonBlank(null, null, "")     = null
+     * StringUtils.firstNonBlank("abc")              = "abc"
+     * StringUtils.firstNonBlank(null, "xyz")        = "xyz"
+     * StringUtils.firstNonBlank(null, "xyz", "abc") = "xyz"
+     * StringUtils.firstNonBlank()                   = null
+     * </pre>
+     *
+     * @param <T> the specific kind of CharSequence
+     * @param values  the values to test, may be {@code null} or empty
+     * @return the first value from {@code values} which is not empty,
+     *  or {@code null} if there are no non-empty values
+     * @since 3.8
+     */
+    @SafeVarargs
+    public static <T extends CharSequence> T firstNonEmpty(final T... values) {
+        if (values != null) {
+            for (final T val : values) {
+                if (isNotEmpty(val)) {
+                    return val;
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
      * <p>Returns either the passed in CharSequence, or if the CharSequence is
      * whitespace, empty ("") or {@code null}, the value of {@code 
defaultStr}.</p>
      *

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/152e5d48/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java
index 8e872c3..2eb7368 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsEmptyBlankTest.java
@@ -16,7 +16,9 @@
  */
 package org.apache.commons.lang3;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
@@ -140,4 +142,30 @@ public class StringUtilsEmptyBlankTest  {
         assertFalse(StringUtils.isAllBlank(" ", "bar"));
         assertFalse(StringUtils.isAllBlank("foo", "bar"));
     }
+
+    @Test
+    public void testFirstNonBlank() {
+        assertNull(StringUtils.firstNonBlank());
+        assertNull(StringUtils.firstNonBlank((String[]) null));
+        assertNull(StringUtils.firstNonBlank(null, null, null));
+        assertNull(StringUtils.firstNonBlank(null, "", " "));
+        assertNull(StringUtils.firstNonBlank(null, null, " "));
+        assertEquals("zz", StringUtils.firstNonBlank(null, "zz"));
+        assertEquals("abc", StringUtils.firstNonBlank("abc"));
+        assertEquals("xyz", StringUtils.firstNonBlank(null, "xyz"));
+        assertEquals("xyz", StringUtils.firstNonBlank(null, "xyz", "abc"));
+    }
+
+    @Test
+    public void testFirstNonEmpty() {
+        assertNull(StringUtils.firstNonEmpty());
+        assertNull(StringUtils.firstNonEmpty((String[]) null));
+        assertNull(StringUtils.firstNonEmpty(null, null, null));
+        assertEquals(" ", StringUtils.firstNonEmpty(null, "", " "));
+        assertNull(StringUtils.firstNonEmpty(null, null, ""));
+        assertEquals("zz", StringUtils.firstNonEmpty(null, "zz"));
+        assertEquals("abc", StringUtils.firstNonEmpty("abc"));
+        assertEquals("xyz", StringUtils.firstNonEmpty(null, "xyz"));
+        assertEquals("xyz", StringUtils.firstNonEmpty(null, "xyz", "abc"));
+    }
 }

Reply via email to