LANG-1392: Methods for getting first non empty or non blank value (closes #325)
This commit is contained in:
parent
9ea0063bc9
commit
152e5d48ea
|
@ -7478,6 +7478,72 @@ public class StringUtils {
|
|||
return str == null ? defaultStr : str;
|
||||
}
|
||||
|
||||
/**
|
||||
* <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>
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue