commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r1056879 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/StringUtils.java test/java/org/apache/commons/lang/StringUtilsTest.java
Date Sun, 09 Jan 2011 03:33:41 GMT
Author: niallp
Date: Sun Jan  9 03:33:40 2011
New Revision: 1056879

URL: http://svn.apache.org/viewvc?rev=1056879&view=rev
Log:
Port LANG-655 to LANG 2.x Branch - add defaultIfBlank() method to StringUtils

Modified:
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
    commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java

Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java?rev=1056879&r1=1056878&r2=1056879&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/StringUtils.java
Sun Jan  9 03:33:40 2011
@@ -5701,6 +5701,28 @@ public class StringUtils {
 
     /**
      * <p>Returns either the passed in String, or if the String is
+     * whitespace, empty ("") or <code>null</code>, the value of <code>defaultStr</code>.</p>
+     *
+     * <pre>
+     * StringUtils.defaultIfBlank(null, "NULL")  = "NULL"
+     * StringUtils.defaultIfBlank("", "NULL")    = "NULL"
+     * StringUtils.defaultIfBlank(" ", "NULL")   = "NULL"
+     * StringUtils.defaultIfBlank("bat", "NULL") = "bat"
+     * StringUtils.defaultIfBlank("", null)      = null
+     * </pre>
+     * @param str the String to check, may be null
+     * @param defaultStr  the default String to return
+     *  if the input is whitespace, empty ("") or <code>null</code>, may be null
+     * @return the passed in String, or the default
+     * @see StringUtils#defaultString(String, String)
+     * @since 2.6
+     */
+    public static String defaultIfBlank(String str, String defaultStr) {
+        return StringUtils.isBlank(str) ? 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>

Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java?rev=1056879&r1=1056878&r2=1056879&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/StringUtilsTest.java
Sun Jan  9 03:33:40 2011
@@ -1515,6 +1515,16 @@ public class StringUtilsTest extends Tes
         assertNull(StringUtils.defaultIfEmpty("", null));
     }
 
+    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));
+        String s = StringUtils.defaultIfBlank("abc", "NULL");
+        assertEquals("abc", s);
+    }
+
     //-----------------------------------------------------------------------
     public void testDeprecatedEscapeFunctions_String() {
         assertEquals("", StringUtils.escape("") );



Mime
View raw message