commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r618882 - in /commons/proper/lang/trunk: ./ src/java/org/apache/commons/lang/StringUtils.java src/test/org/apache/commons/lang/StringUtilsTest.java
Date Wed, 06 Feb 2008 04:33:07 GMT
Author: ggregory
Date: Tue Feb  5 20:32:56 2008
New Revision: 618882

URL: http://svn.apache.org/viewvc?rev=618882&view=rev
Log:
[LANG-407] StringUtils.length(String) returns null-safe length.

Modified:
    commons/proper/lang/trunk/   (props changed)
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java

Propchange: commons/proper/lang/trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Tue Feb  5 20:32:56 2008
@@ -1,10 +1,11 @@
-target
-*.iws
-*.ipr
-*.iml
-dist
-.classpath
-.project
-bin
-build.properties
-*.log
+target
+*.iws
+*.ipr
+*.iml
+dist
+.classpath
+.project
+bin
+build.properties
+*.log
+.settings

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?rev=618882&r1=618881&r2=618882&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java Tue Feb  5
20:32:56 2008
@@ -4581,6 +4581,18 @@
         }
     }
 
+    /**
+     * Gets a String's length or <code>0</code> if the String is <code>null</code>.
+     * 
+     * @param str
+     *            a String or <code>null</code>
+     * @return String length or <code>0</code> if the String is <code>null</code>.
+     * @since 2.4
+     */
+    public static int length(String str) {
+        return str == null ? 0 : str.length();
+    }
+    
     // Centering
     //-----------------------------------------------------------------------
     /**

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java?rev=618882&r1=618881&r2=618882&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsTest.java Tue Feb
 5 20:32:56 2008
@@ -1403,6 +1403,15 @@
         assertEquals("  abc", StringUtils.leftPad("abc", 5, ""));
     }
 
+    public void testLength() {
+        assertEquals(0, StringUtils.length(null));
+        assertEquals(0, StringUtils.length(""));
+        assertEquals(0, StringUtils.length(StringUtils.EMPTY));
+        assertEquals(1, StringUtils.length("A"));
+        assertEquals(1, StringUtils.length(" "));
+        assertEquals(8, StringUtils.length("ABCDEFGH"));
+    }
+    
     //-----------------------------------------------------------------------
     public void testCenter_StringInt() {
         assertEquals(null, StringUtils.center(null, -1));
@@ -1647,6 +1656,7 @@
     public void testEMPTY() {
         assertNotNull(StringUtils.EMPTY);
         assertEquals("", StringUtils.EMPTY);
+        assertEquals(0, StringUtils.EMPTY.length());
     }
     
     public void testRemoveStart() {



Mime
View raw message