You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2018/06/08 16:13:17 UTC

[1/2] [lang] LANG-1392: Methods for getting first non empty or non blank value (closes #325)

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 <je...@thebernardgroup.com>
Authored: Thu Apr 19 15:00:19 2018 -0500
Committer: pascalschumacher <pa...@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"));
+    }
 }


[2/2] [lang] LANG-1392: add changes.xml entry

Posted by pa...@apache.org.
LANG-1392: add changes.xml entry


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

Branch: refs/heads/master
Commit: 672cd146f2c2824c3c4ea28d406a6e5feeee7b18
Parents: 152e5d4
Author: pascalschumacher <pa...@gmx.net>
Authored: Fri Jun 8 18:12:51 2018 +0200
Committer: pascalschumacher <pa...@gmx.net>
Committed: Fri Jun 8 18:12:51 2018 +0200

----------------------------------------------------------------------
 src/changes/changes.xml | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/672cd146/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index b7c70d1..4fb91b1 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -65,7 +65,8 @@ The <action> type attribute can be add,update,fix,remove.
     <action issue="LANG-1393" type="add" dev="ggregory" due-to="Gary Gregory">Add API SystemUtils.String getEnvironmentVariable(final String name, final String defaultValue)</action>   
     <action issue="LANG-1394" type="add" dev="ggregory" due-to="Sebb, Gary Gregory">org.apache.commons.lang3.SystemUtils should not write to System.err.</action>   
     <action issue="LANG-1238" type="add" dev="ggregory" due-to="Christopher Cordeiro, Gary Gregory, Bruno P. Kinoshita, Oleg Chubaryov">Add RegexUtils class instead of overloading methods in StringUtils that take a regex to take precompiled Pattern.</action>   
-    <action issue="LANG-1290" type="add" dev="ggregory" due-to="Jochen Schalanda">StringUtils.join() with support for List&lt;?> with configurable start/end indices.</action>   
+    <action issue="LANG-1290" type="add" dev="ggregory" due-to="Jochen Schalanda">StringUtils.join() with support for List&lt;?> with configurable start/end indices.</action>
+    <action issue="LANG-1392" type="add" dev="pschumacher" due-to="Jeff Nelson">Methods for getting first non empty or non blank value</action> 
   </release>
 
   <release version="3.7" date="2017-11-04" description="New features and bug fixes. Requires Java 7, supports Java 8, 9, 10.">