diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 8973b86f0..c68c3a420 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -5353,7 +5353,7 @@ public class StringUtils { } /** - *
Returns either the passed in String, or if the String is + *
Returns either the passed in CharSequence, or if the CharSequence is
* empty or null
, the value of defaultStr
.
@@ -5364,12 +5364,12 @@ public class StringUtils { ** * @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
null
, 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;
}
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index e954a1e5b..b8e03a967 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -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));