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:
parent
d5f32c231c
commit
371c34bbea
|
@ -60,6 +60,7 @@ Updated:
|
||||||
-- ordinalIndexOf to find the nth index of a string
|
-- ordinalIndexOf to find the nth index of a string
|
||||||
-- various remove methods to remove parts of a string
|
-- various remove methods to remove parts of a string
|
||||||
-- various split methods to provide more control over splitting 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
|
- SystemUtils - methods to get system properties as File objects
|
||||||
-- extra constants representing system properties
|
-- extra constants representing system properties
|
||||||
- Validate - new methods to check whether all elements in a collection are of a specific type
|
- Validate - new methods to check whether all elements in a collection are of a specific type
|
||||||
|
|
|
@ -4405,9 +4405,9 @@ public class StringUtils {
|
||||||
* <code>null</code>, the value of <code>defaultStr</code>.</p>
|
* <code>null</code>, the value of <code>defaultStr</code>.</p>
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* StringUtils.defaultString(null, "null") = "null"
|
* StringUtils.defaultString(null, "NULL") = "NULL"
|
||||||
* StringUtils.defaultString("", "null") = ""
|
* StringUtils.defaultString("", "NULL") = ""
|
||||||
* StringUtils.defaultString("bat", "null") = "bat"
|
* StringUtils.defaultString("bat", "NULL") = "bat"
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @see ObjectUtils#toString(Object,String)
|
* @see ObjectUtils#toString(Object,String)
|
||||||
|
@ -4421,6 +4421,26 @@ public class StringUtils {
|
||||||
return str == null ? defaultStr : str;
|
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
|
// Reversing
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1173,17 +1173,23 @@ public class StringUtilsTest extends TestCase {
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
public void testDefault_String() {
|
public void testDefault_String() {
|
||||||
assertEquals("", StringUtils.defaultString(null) );
|
assertEquals("", StringUtils.defaultString(null));
|
||||||
assertEquals("", StringUtils.defaultString("") );
|
assertEquals("", StringUtils.defaultString(""));
|
||||||
assertEquals("abc", StringUtils.defaultString("abc") );
|
assertEquals("abc", StringUtils.defaultString("abc"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDefault_StringString() {
|
public void testDefault_StringString() {
|
||||||
assertEquals("xyz", StringUtils.defaultString(null, "xyz") );
|
assertEquals("NULL", StringUtils.defaultString(null, "NULL"));
|
||||||
assertEquals("", StringUtils.defaultString("", "xyz") );
|
assertEquals("", StringUtils.defaultString("", "NULL"));
|
||||||
assertEquals("abc", StringUtils.defaultString("abc", "xyz") );
|
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"));
|
||||||
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
public void testEscapeFunctions_String() {
|
public void testEscapeFunctions_String() {
|
||||||
assertEquals("", StringUtils.escape("") );
|
assertEquals("", StringUtils.escape("") );
|
||||||
|
|
Loading…
Reference in New Issue