commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r160807 - in jakarta/commons/proper/lang/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/lang/StringUtils.java src/test/org/apache/commons/lang/StringUtilsTest.java
Date Sun, 10 Apr 2005 23:25:27 GMT
Author: scolebourne
Date: Sun Apr 10 16:25:25 2005
New Revision: 160807

URL: http://svn.apache.org/viewcvs?view=rev&rev=160807
Log:
Add StringUtils.defaultIfEmpty

Modified:
    jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java

Modified: jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt?view=diff&r1=160806&r2=160807
==============================================================================
--- jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt Sun Apr 10 16:25:25 2005
@@ -60,6 +60,7 @@
              -- ordinalIndexOf to find the nth index of a string
              -- various remove methods to remove parts of a string
              -- various split methods to provide more control over splitting a string
+             -- defaultIfEmpty to default a string if null or empty
 - SystemUtils - methods to get system properties as File objects
              -- extra constants representing system properties
 - Validate - new methods to check whether all elements in a collection are of a specific
type

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?view=diff&r1=160806&r2=160807
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java Sun
Apr 10 16:25:25 2005
@@ -4405,9 +4405,9 @@
      * <code>null</code>, the value of <code>defaultStr</code>.</p>
      *
      * <pre>
-     * StringUtils.defaultString(null, "null")  = "null"
-     * StringUtils.defaultString("", "null")    = ""
-     * StringUtils.defaultString("bat", "null") = "bat"
+     * StringUtils.defaultString(null, "NULL")  = "NULL"
+     * StringUtils.defaultString("", "NULL")    = ""
+     * StringUtils.defaultString("bat", "NULL") = "bat"
      * </pre>
      *
      * @see ObjectUtils#toString(Object,String)
@@ -4419,6 +4419,26 @@
      */
     public static String defaultString(String str, String defaultStr) {
         return str == null ? 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>
+     * StringUtils.defaultIfEmpty(null, "NULL")  = "NULL"
+     * StringUtils.defaultIfEmpty("", "NULL")    = "NULL"
+     * StringUtils.defaultIfEmpty("bat", "NULL") = "bat"
+     * </pre>
+     *
+     * @see StringUtils#defaultString(String, String)
+     * @param str  the String to check, may be null
+     * @param defaultStr  the default String to return
+     *  if the input is empty ("") or <code>null</code>, may be null
+     * @return the passed in String, or the default
+     */
+    public static String defaultIfEmpty(String str, String defaultStr) {
+        return StringUtils.isEmpty(str) ? defaultStr : str;
     }
 
     // Reversing

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java?view=diff&r1=160806&r2=160807
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
Sun Apr 10 16:25:25 2005
@@ -1173,17 +1173,23 @@
 
     //-----------------------------------------------------------------------
     public void testDefault_String() {
-        assertEquals("", StringUtils.defaultString(null) );
-        assertEquals("", StringUtils.defaultString("") );
-        assertEquals("abc", StringUtils.defaultString("abc") );
+        assertEquals("", StringUtils.defaultString(null));
+        assertEquals("", StringUtils.defaultString(""));
+        assertEquals("abc", StringUtils.defaultString("abc"));
     }
-            
+
     public void testDefault_StringString() {
-        assertEquals("xyz", StringUtils.defaultString(null, "xyz") );
-        assertEquals("", StringUtils.defaultString("", "xyz") );
-        assertEquals("abc", StringUtils.defaultString("abc", "xyz") );
+        assertEquals("NULL", StringUtils.defaultString(null, "NULL"));
+        assertEquals("", StringUtils.defaultString("", "NULL"));
+        assertEquals("abc", StringUtils.defaultString("abc", "NULL"));
     }
-    
+
+    public void testDefaultIfEmpty_StringString() {
+        assertEquals("NULL", StringUtils.defaultIfEmpty(null, "NULL"));
+        assertEquals("NULL", StringUtils.defaultIfEmpty("", "NULL"));
+        assertEquals("abc", StringUtils.defaultIfEmpty("abc", "NULL"));
+    }
+
     //-----------------------------------------------------------------------
     public void testEscapeFunctions_String() {
         assertEquals("", StringUtils.escape("") );



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message