diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 1fc0c33d9..99c193a5f 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -5514,6 +5514,28 @@ public class StringUtils {
return str == null ? defaultStr : str;
}
+ /**
+ *
Returns either the passed in CharSequence, or if the CharSequence is
+ * whitespace, empty ("") or null
, the value of defaultStr
.
+ *
+ *
+ * StringUtils.defaultIfBlank(null, "NULL") = "NULL"
+ * StringUtils.defaultIfBlank("", "NULL") = "NULL"
+ * StringUtils.defaultIfBlank(" ", "NULL") = "NULL"
+ * StringUtils.defaultIfBlank("bat", "NULL") = "bat"
+ * StringUtils.defaultIfBlank("", null) = null
+ *
+ * @param the specific kind of CharSequence
+ * @param str the CharSequence to check, may be null
+ * @param defaultStr the default CharSequence to return
+ * if the input is whitespace, empty ("") or null
, may be null
+ * @return the passed in CharSequence, or the default
+ * @see StringUtils#defaultString(String, String)
+ */
+ public static T defaultIfBlank(T str, T defaultStr) {
+ return StringUtils.isBlank(str) ? defaultStr : str;
+ }
+
/**
* Returns either the passed in CharSequence, or if the CharSequence is
* empty or null
, the value of defaultStr
.
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index eef7e3d72..4781fba9d 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -1447,6 +1447,17 @@ public class StringUtilsTest extends TestCase {
assertEquals("abc", s);
}
+ public void testDefaultIfBlank_StringString() {
+ assertEquals("NULL", StringUtils.defaultIfBlank(null, "NULL"));
+ assertEquals("NULL", StringUtils.defaultIfBlank("", "NULL"));
+ assertEquals("NULL", StringUtils.defaultIfBlank(" ", "NULL"));
+ assertEquals("abc", StringUtils.defaultIfBlank("abc", "NULL"));
+ assertNull(StringUtils.defaultIfBlank("", null));
+ // Tests compatibility for the API return type
+ String s = StringUtils.defaultIfBlank("abc", "NULL");
+ assertEquals("abc", s);
+ }
+
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());
@@ -1456,6 +1467,16 @@ public class StringUtilsTest extends TestCase {
assertEquals("abc", s.toString());
}
+ public void testDefaultIfBlank_StringBuilders() {
+ assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuilder(""), new StringBuilder("NULL")).toString());
+ assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuilder(" "), new StringBuilder("NULL")).toString());
+ assertEquals("abc", StringUtils.defaultIfBlank(new StringBuilder("abc"), new StringBuilder("NULL")).toString());
+ assertNull(StringUtils.defaultIfBlank(new StringBuilder(""), null));
+ // Tests compatibility for the API return type
+ StringBuilder s = StringUtils.defaultIfBlank(new StringBuilder("abc"), new StringBuilder("NULL"));
+ assertEquals("abc", s.toString());
+ }
+
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());
@@ -1465,6 +1486,16 @@ public class StringUtilsTest extends TestCase {
assertEquals("abc", s.toString());
}
+ public void testDefaultIfBlank_StringBuffers() {
+ assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuffer(""), new StringBuffer("NULL")).toString());
+ assertEquals("NULL", StringUtils.defaultIfBlank(new StringBuffer(" "), new StringBuffer("NULL")).toString());
+ assertEquals("abc", StringUtils.defaultIfBlank(new StringBuffer("abc"), new StringBuffer("NULL")).toString());
+ assertNull(StringUtils.defaultIfBlank(new StringBuffer(""), null));
+ // Tests compatibility for the API return type
+ StringBuffer s = StringUtils.defaultIfBlank(new StringBuffer("abc"), new StringBuffer("NULL"));
+ assertEquals("abc", s.toString());
+ }
+
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());
@@ -1474,6 +1505,16 @@ public class StringUtilsTest extends TestCase {
assertEquals("abc", s.toString());
}
+ public void testDefaultIfBlank_CharBuffers() {
+ assertEquals("NULL", StringUtils.defaultIfBlank(CharBuffer.wrap(""), CharBuffer.wrap("NULL")).toString());
+ assertEquals("NULL", StringUtils.defaultIfBlank(CharBuffer.wrap(" "), CharBuffer.wrap("NULL")).toString());
+ assertEquals("abc", StringUtils.defaultIfBlank(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL")).toString());
+ assertNull(StringUtils.defaultIfBlank(CharBuffer.wrap(""), null));
+ // Tests compatibility for the API return type
+ CharBuffer s = StringUtils.defaultIfBlank(CharBuffer.wrap("abc"), CharBuffer.wrap("NULL"));
+ assertEquals("abc", s.toString());
+ }
+
//-----------------------------------------------------------------------
public void testAbbreviate_StringInt() {
assertEquals(null, StringUtils.abbreviate(null, 10));