Convert defaultIfEmpty from String to CharSequence.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@920187 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2010-03-08 03:41:49 +00:00
parent e1077c023d
commit 9c0379850b
2 changed files with 24 additions and 5 deletions

View File

@ -5353,7 +5353,7 @@ public static String defaultString(String str, String defaultStr) {
} }
/** /**
* <p>Returns either the passed in String, or if the String is * <p>Returns either the passed in CharSequence, or if the CharSequence is
* empty or <code>null</code>, the value of <code>defaultStr</code>.</p> * empty or <code>null</code>, the value of <code>defaultStr</code>.</p>
* *
* <pre> * <pre>
@ -5364,12 +5364,12 @@ public static String defaultString(String str, String defaultStr) {
* </pre> * </pre>
* *
* @see StringUtils#defaultString(String, String) * @see StringUtils#defaultString(String, String)
* @param str the String to check, may be null * @param str the CharSequence to check, may be null
* @param defaultStr the default String to return * @param defaultStr the default CharSequence to return
* if the input is empty ("") or <code>null</code>, may be null * if the input is empty ("") or <code>null</code>, may be null
* @return the passed in String, or the default * @return the passed in CharSequence, or the default
*/ */
public static String defaultIfEmpty(String str, String defaultStr) { public static CharSequence defaultIfEmpty(CharSequence str, CharSequence defaultStr) {
return StringUtils.isEmpty(str) ? defaultStr : str; return StringUtils.isEmpty(str) ? defaultStr : str;
} }

View File

@ -18,6 +18,7 @@
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.nio.CharBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
@ -1404,6 +1405,24 @@ public void testDefaultIfEmpty_StringString() {
assertNull(StringUtils.defaultIfEmpty("", null)); assertNull(StringUtils.defaultIfEmpty("", null));
} }
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());
assertNull(StringUtils.defaultIfEmpty(new StringBuilder(""), null));
}
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());
assertNull(StringUtils.defaultIfEmpty(new StringBuffer(""), null));
}
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());
assertNull(StringUtils.defaultIfEmpty(CharBuffer.wrap(""), null));
}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public void testAbbreviate_StringInt() { public void testAbbreviate_StringInt() {
assertEquals(null, StringUtils.abbreviate(null, 10)); assertEquals(null, StringUtils.abbreviate(null, 10));