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 class StringUtils {
}
/**
* <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>
*
* <pre>
@ -5364,12 +5364,12 @@ public class StringUtils {
* </pre>
*
* @see StringUtils#defaultString(String, String)
* @param str the String to check, may be null
* @param defaultStr the default String to return
* @param str the CharSequence to check, may be null
* @param defaultStr the default CharSequence to return
* 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;
}

View File

@ -18,6 +18,7 @@ package org.apache.commons.lang3;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
@ -1404,6 +1405,24 @@ public class StringUtilsTest extends TestCase {
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() {
assertEquals(null, StringUtils.abbreviate(null, 10));