Adding StringUtils.defaultIfBlank(String, String). Requested by Adam Dyga, patch from Nicklas Holm. LANG-655

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1030830 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2010-11-04 04:05:57 +00:00
parent 88bb2bf846
commit 6f0dc1070c
2 changed files with 63 additions and 0 deletions

View File

@ -5514,6 +5514,28 @@ public class StringUtils {
return str == null ? defaultStr : str;
}
/**
* <p>Returns either the passed in CharSequence, or if the CharSequence is
* whitespace, empty ("") or <code>null</code>, the value of <code>defaultStr</code>.</p>
*
* <pre>
* StringUtils.defaultIfBlank(null, "NULL") = "NULL"
* StringUtils.defaultIfBlank("", "NULL") = "NULL"
* StringUtils.defaultIfBlank(" ", "NULL") = "NULL"
* StringUtils.defaultIfBlank("bat", "NULL") = "bat"
* StringUtils.defaultIfBlank("", null) = null
* </pre>
* @param <T> the specific kind of CharSequence
* @param str the CharSequence to check, may be null
* @param defaultStr the default CharSequence to return
* if the input is whitespace, empty ("") or <code>null</code>, may be null
* @return the passed in CharSequence, or the default
* @see StringUtils#defaultString(String, String)
*/
public static <T extends CharSequence> T defaultIfBlank(T str, T defaultStr) {
return StringUtils.isBlank(str) ? defaultStr : str;
}
/**
* <p>Returns either the passed in CharSequence, or if the CharSequence is
* empty or <code>null</code>, the value of <code>defaultStr</code>.</p>

View File

@ -1447,6 +1447,17 @@ public class StringUtilsTest extends TestCase {
assertEquals("abc", s);
}
public void testDefaultIfBlank_StringString() {
assertEquals("NULL", StringUtils.defaultIfBlank(null, "NULL"));
assertEquals("NULL", StringUtils.defaultIfBlank("", "NULL"));
assertEquals("NULL", StringUtils.defaultIfBlank(" ", "NULL"));
assertEquals("abc", StringUtils.defaultIfBlank("abc", "NULL"));
assertNull(StringUtils.defaultIfBlank("", null));
// Tests compatibility for the API return type
String s = StringUtils.defaultIfBlank("abc", "NULL");
assertEquals("abc", s);
}
public void testDefaultIfEmpty_StringBuilders() {
assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuilder(""), new StringBuilder("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
@ -1456,6 +1467,16 @@ public class StringUtilsTest extends TestCase {
assertEquals("abc", s.toString());
}
public void testDefaultIfBlank_StringBuilders() {
assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuilder(""), new StringBuilder("NULL")).toString());
assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuilder(" "), new StringBuilder("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfBlank(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
assertNull(StringUtils.defaultIfBlank(new StringBuilder(""), null));
// Tests compatibility for the API return type
StringBuilder s = StringUtils.defaultIfBlank(new StringBuilder("abc"), new StringBuilder("NULL"));
assertEquals("abc", s.toString());
}
public void testDefaultIfEmpty_StringBuffers() {
assertEquals("NULL", StringUtils.defaultIfEmpty(new StringBuffer(""), new StringBuffer("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfEmpty(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
@ -1465,6 +1486,16 @@ public class StringUtilsTest extends TestCase {
assertEquals("abc", s.toString());
}
public void testDefaultIfBlank_StringBuffers() {
assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuffer(""), new StringBuffer("NULL")).toString());
assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuffer(" "), new StringBuffer("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfBlank(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
assertNull(StringUtils.defaultIfBlank(new StringBuffer(""), null));
// Tests compatibility for the API return type
StringBuffer s = StringUtils.defaultIfBlank(new StringBuffer("abc"), new StringBuffer("NULL"));
assertEquals("abc", s.toString());
}
public void testDefaultIfEmpty_CharBuffers() {
assertEquals("NULL", StringUtils.defaultIfEmpty(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfEmpty(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
@ -1474,6 +1505,16 @@ public class StringUtilsTest extends TestCase {
assertEquals("abc", s.toString());
}
public void testDefaultIfBlank_CharBuffers() {
assertEquals("NULL", StringUtils.defaultIfBlank(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
assertEquals("NULL", StringUtils.defaultIfBlank(CharBuffer.wrap(" "), CharBuffer.wrap("NULL")).toString());
assertEquals("abc", StringUtils.defaultIfBlank(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
assertNull(StringUtils.defaultIfBlank(CharBuffer.wrap(""), null));
// Tests compatibility for the API return type
CharBuffer s = StringUtils.defaultIfBlank(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL"));
assertEquals("abc", s.toString());
}
//-----------------------------------------------------------------------
public void testAbbreviate_StringInt() {
assertEquals(null, StringUtils.abbreviate(null, 10));