Add StringUtils.defaultIfEmpty

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@160807 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-04-10 23:25:25 +00:00
parent d5f32c231c
commit 371c34bbea
3 changed files with 38 additions and 11 deletions

View File

@ -60,6 +60,7 @@ Updated:
-- ordinalIndexOf to find the nth index of a string
-- various remove methods to remove parts of a string
-- various split methods to provide more control over splitting a string
-- defaultIfEmpty to default a string if null or empty
- SystemUtils - methods to get system properties as File objects
-- extra constants representing system properties
- Validate - new methods to check whether all elements in a collection are of a specific type

View File

@ -4405,9 +4405,9 @@ public class StringUtils {
* <code>null</code>, the value of <code>defaultStr</code>.</p>
*
* <pre>
* StringUtils.defaultString(null, "null") = "null"
* StringUtils.defaultString("", "null") = ""
* StringUtils.defaultString("bat", "null") = "bat"
* StringUtils.defaultString(null, "NULL") = "NULL"
* StringUtils.defaultString("", "NULL") = ""
* StringUtils.defaultString("bat", "NULL") = "bat"
* </pre>
*
* @see ObjectUtils#toString(Object,String)
@ -4421,6 +4421,26 @@ public class StringUtils {
return str == null ? defaultStr : str;
}
/**
* <p>Returns either the passed in String, or if the String is
* empty or <code>null</code>, the value of <code>defaultStr</code>.</p>
*
* <pre>
* StringUtils.defaultIfEmpty(null, "NULL") = "NULL"
* StringUtils.defaultIfEmpty("", "NULL") = "NULL"
* StringUtils.defaultIfEmpty("bat", "NULL") = "bat"
* </pre>
*
* @see StringUtils#defaultString(String, String)
* @param str the String to check, may be null
* @param defaultStr the default String to return
* if the input is empty ("") or <code>null</code>, may be null
* @return the passed in String, or the default
*/
public static String defaultIfEmpty(String str, String defaultStr) {
return StringUtils.isEmpty(str) ? defaultStr : str;
}
// Reversing
//-----------------------------------------------------------------------
/**

View File

@ -1173,15 +1173,21 @@ public class StringUtilsTest extends TestCase {
//-----------------------------------------------------------------------
public void testDefault_String() {
assertEquals("", StringUtils.defaultString(null) );
assertEquals("", StringUtils.defaultString("") );
assertEquals("abc", StringUtils.defaultString("abc") );
assertEquals("", StringUtils.defaultString(null));
assertEquals("", StringUtils.defaultString(""));
assertEquals("abc", StringUtils.defaultString("abc"));
}
public void testDefault_StringString() {
assertEquals("xyz", StringUtils.defaultString(null, "xyz") );
assertEquals("", StringUtils.defaultString("", "xyz") );
assertEquals("abc", StringUtils.defaultString("abc", "xyz") );
assertEquals("NULL", StringUtils.defaultString(null, "NULL"));
assertEquals("", StringUtils.defaultString("", "NULL"));
assertEquals("abc", StringUtils.defaultString("abc", "NULL"));
}
public void testDefaultIfEmpty_StringString() {
assertEquals("NULL", StringUtils.defaultIfEmpty(null, "NULL"));
assertEquals("NULL", StringUtils.defaultIfEmpty("", "NULL"));
assertEquals("abc", StringUtils.defaultIfEmpty("abc", "NULL"));
}
//-----------------------------------------------------------------------