commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r279988 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/WordUtils.java test/org/apache/commons/lang/WordUtilsTest.java
Date Sat, 10 Sep 2005 11:38:43 GMT
Author: scolebourne
Date: Sat Sep 10 04:38:35 2005
New Revision: 279988

URL: http://svn.apache.org/viewcvs?rev=279988&view=rev
Log:
Add WordUtils.initials

Modified:
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/WordUtils.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/WordUtilsTest.java

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/WordUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/WordUtils.java?rev=279988&r1=279987&r2=279988&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/WordUtils.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/WordUtils.java Sat
Sep 10 04:38:35 2005
@@ -273,34 +273,17 @@
      * @since 2.1
      */
     public static String capitalize(String str, char[] delimiters) {
-        if (str == null || str.length() == 0) {
+        int delimLen = (delimiters == null ? -1 : delimiters.length);
+        if (str == null || str.length() == 0 || delimLen == 0) {
             return str;
         }
         int strLen = str.length();
         StringBuffer buffer = new StringBuffer(strLen);
-
-        int delimitersLen = 0;
-        if(delimiters != null) {
-            delimitersLen = delimiters.length;
-        }
-
         boolean capitalizeNext = true;
         for (int i = 0; i < strLen; i++) {
             char ch = str.charAt(i);
 
-            boolean isDelimiter = false;
-            if(delimiters == null) {
-                isDelimiter = Character.isWhitespace(ch);
-            } else {
-                for(int j=0; j < delimitersLen; j++) {
-                    if(ch == delimiters[j]) {
-                        isDelimiter = true;
-                        break;
-                    }
-                }
-            }
-
-            if (isDelimiter) {
+            if (isDelimiter(ch, delimiters)) {
                 buffer.append(ch);
                 capitalizeNext = true;
             } else if (capitalizeNext) {
@@ -313,6 +296,7 @@
         return buffer.toString();
     }
 
+    //-----------------------------------------------------------------------
     /**
      * <p>Converts all the whitespace separated words in a String into capitalized
words, 
      * that is each word is made up of a titlecase character and then a series of 
@@ -363,13 +347,15 @@
      * @since 2.1
      */
     public static String capitalizeFully(String str, char[] delimiters) {
-        if (str == null || str.length() == 0) {
+        int delimLen = (delimiters == null ? -1 : delimiters.length);
+        if (str == null || str.length() == 0 || delimLen == 0) {
             return str;
         }
         str = str.toLowerCase();
         return capitalize(str, delimiters);
     }
 
+    //-----------------------------------------------------------------------
     /**
      * <p>Uncapitalizes all the whitespace separated words in a String.
      * Only the first letter of each word is changed.</p>
@@ -417,34 +403,17 @@
      * @since 2.1
      */
     public static String uncapitalize(String str, char[] delimiters) {
-        if (str == null || str.length() == 0) {
+        int delimLen = (delimiters == null ? -1 : delimiters.length);
+        if (str == null || str.length() == 0 || delimLen == 0) {
             return str;
         }
         int strLen = str.length();
-
-        int delimitersLen = 0;
-        if(delimiters != null) {
-            delimitersLen = delimiters.length;
-        }
-
         StringBuffer buffer = new StringBuffer(strLen);
         boolean uncapitalizeNext = true;
         for (int i = 0; i < strLen; i++) {
             char ch = str.charAt(i);
 
-            boolean isDelimiter = false;
-            if(delimiters == null) {
-                isDelimiter = Character.isWhitespace(ch);
-            } else {
-                for(int j=0; j < delimitersLen; j++) {
-                    if(ch == delimiters[j]) {
-                        isDelimiter = true;
-                        break;
-                    }
-                }
-            }
-
-            if (isDelimiter) {
+            if (isDelimiter(ch, delimiters)) {
                 buffer.append(ch);
                 uncapitalizeNext = true;
             } else if (uncapitalizeNext) {
@@ -457,6 +426,7 @@
         return buffer.toString();
     }
 
+    //-----------------------------------------------------------------------
     /**
      * <p>Swaps the case of a String using a word based algorithm.</p>
      * 
@@ -510,5 +480,105 @@
         }
         return buffer.toString();
     }
-    
+
+    //-----------------------------------------------------------------------
+    /**
+     * <p>Extracts the initial letters from each word in the String.</p>
+     * 
+     * <p>The first letter of the string and all first letters after
+     * whitespace are returned as a new string.
+     * Their case is not changed.</p>
+     *
+     * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
+     * A <code>null</code> input String returns <code>null</code>.</p>
+     *
+     * <pre>
+     * WordUtils.initials(null)             = null
+     * WordUtils.initials("")               = ""
+     * WordUtils.initials("Ben John Lee")   = "BJL"
+     * WordUtils.initials("Ben J.Lee")      = "BJ"
+     * </pre>
+     *
+     * @param str  the String to get initials from, may be null
+     * @return String of initial letters, <code>null</code> if null String input
+     * @see #initials(String,char[])
+     * @since 2.2
+     */
+    public static String initials(String str) {
+        return initials(str, null);
+    }
+
+    /**
+     * <p>Extracts the initial letters from each word in the String.</p>
+     * 
+     * <p>The first letter of the string and all first letters after the
+     * defined delimiters are returned as a new string.
+     * Their case is not changed.</p>
+     *
+     * <p>If the delimiters array is null, then Whitespace is used.
+     * Whitespace is defined by {@link Character#isWhitespace(char)}.
+     * A <code>null</code> input String returns <code>null</code>.
+     * An empty delimiter array returns an empty String.</p>
+     *
+     * <pre>
+     * WordUtils.initials(null, *)                = null
+     * WordUtils.initials("", *)                  = ""
+     * WordUtils.initials("Ben John Lee", null)   = "BJL"
+     * WordUtils.initials("Ben J.Lee", null)      = "BJ"
+     * WordUtils.initials("Ben J.Lee", [' ','.']) = "BJL"
+     * WordUtils.initials(*, new char[0])         = ""
+     * </pre>
+     * 
+     * @param str  the String to get initials from, may be null
+     * @param delimiters  set of characters to determine words, null means whitespace
+     * @return String of initial letters, <code>null</code> if null String input
+     * @see #initials(String)
+     * @since 2.2
+     */
+    public static String initials(String str, char[] delimiters) {
+        if (str == null || str.length() == 0) {
+            return str;
+        }
+        if (delimiters != null && delimiters.length == 0) {
+            return "";
+        }
+        int strLen = str.length();
+        char[] buf = new char[strLen / 2 + 1];
+        int count = 0;
+        boolean lastWasGap = true;
+        for (int i = 0; i < strLen; i++) {
+            char ch = str.charAt(i);
+
+            if (isDelimiter(ch, delimiters)) {
+                lastWasGap = true;
+            } else if (lastWasGap) {
+                buf[count++] = ch;
+                lastWasGap = false;
+            } else {
+                // ignore ch
+            }
+        }
+        return new String(buf, 0, count);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Is the character a delimiter.
+     *
+     * @param ch  the character to check
+     * @param delimiters  the delimiters
+     * @return true if it is a delimiter
+     */
+    private static boolean isDelimiter(char ch, char[] delimiters) {
+        if (delimiters == null) {
+            return Character.isWhitespace(ch);
+        }
+        for (int i = 0, isize = delimiters.length; i < isize; i++) {
+            if (ch == delimiters[i]) {
+                return true;
+            }
+        }
+        return false;
+    }
+
 }

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/WordUtilsTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/WordUtilsTest.java?rev=279988&r1=279987&r2=279988&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/WordUtilsTest.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/WordUtilsTest.java
Sat Sep 10 04:38:35 2005
@@ -248,7 +248,103 @@
         assertEquals("i AM.fINE", WordUtils.uncapitalize("I AM.FINE", chars) );
         assertEquals("i aM.FINE", WordUtils.uncapitalize("I AM.FINE", null) );
     }
-    
+
+    //-----------------------------------------------------------------------
+    public void testInitials_String() {
+        assertEquals(null, WordUtils.initials(null));
+        assertEquals("", WordUtils.initials(""));
+        assertEquals("", WordUtils.initials("  "));
+
+        assertEquals("I", WordUtils.initials("I"));
+        assertEquals("i", WordUtils.initials("i"));
+        assertEquals("BJL", WordUtils.initials("Ben John Lee"));
+        assertEquals("BJ", WordUtils.initials("Ben J.Lee"));
+        assertEquals("BJ.L", WordUtils.initials(" Ben   John  . Lee"));
+        assertEquals("iah1", WordUtils.initials("i am here 123"));
+    }
+
+    // -----------------------------------------------------------------------
+    public void testInitials_String_charArray() {
+        char[] array = null;
+        assertEquals(null, WordUtils.initials(null, array));
+        assertEquals("", WordUtils.initials("", array));
+        assertEquals("", WordUtils.initials("  ", array));
+        assertEquals("I", WordUtils.initials("I", array));
+        assertEquals("i", WordUtils.initials("i", array));
+        assertEquals("S", WordUtils.initials("SJC", array));
+        assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
+        assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
+        assertEquals("BJ.L", WordUtils.initials(" Ben   John  . Lee", array));
+        assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
+        assertEquals("iah1", WordUtils.initials("i am here 123", array));
+        
+        array = new char[0];
+        assertEquals(null, WordUtils.initials(null, array));
+        assertEquals("", WordUtils.initials("", array));
+        assertEquals("", WordUtils.initials("  ", array));
+        assertEquals("", WordUtils.initials("I", array));
+        assertEquals("", WordUtils.initials("i", array));
+        assertEquals("", WordUtils.initials("SJC", array));
+        assertEquals("", WordUtils.initials("Ben John Lee", array));
+        assertEquals("", WordUtils.initials("Ben J.Lee", array));
+        assertEquals("", WordUtils.initials(" Ben   John  . Lee", array));
+        assertEquals("", WordUtils.initials("Kay O'Murphy", array));
+        assertEquals("", WordUtils.initials("i am here 123", array));
+        
+        array = " ".toCharArray();
+        assertEquals(null, WordUtils.initials(null, array));
+        assertEquals("", WordUtils.initials("", array));
+        assertEquals("", WordUtils.initials("  ", array));
+        assertEquals("I", WordUtils.initials("I", array));
+        assertEquals("i", WordUtils.initials("i", array));
+        assertEquals("S", WordUtils.initials("SJC", array));
+        assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
+        assertEquals("BJ", WordUtils.initials("Ben J.Lee", array));
+        assertEquals("BJ.L", WordUtils.initials(" Ben   John  . Lee", array));
+        assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
+        assertEquals("iah1", WordUtils.initials("i am here 123", array));
+        
+        array = " .".toCharArray();
+        assertEquals(null, WordUtils.initials(null, array));
+        assertEquals("", WordUtils.initials("", array));
+        assertEquals("", WordUtils.initials("  ", array));
+        assertEquals("I", WordUtils.initials("I", array));
+        assertEquals("i", WordUtils.initials("i", array));
+        assertEquals("S", WordUtils.initials("SJC", array));
+        assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
+        assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
+        assertEquals("BJL", WordUtils.initials(" Ben   John  . Lee", array));
+        assertEquals("KO", WordUtils.initials("Kay O'Murphy", array));
+        assertEquals("iah1", WordUtils.initials("i am here 123", array));
+        
+        array = " .'".toCharArray();
+        assertEquals(null, WordUtils.initials(null, array));
+        assertEquals("", WordUtils.initials("", array));
+        assertEquals("", WordUtils.initials("  ", array));
+        assertEquals("I", WordUtils.initials("I", array));
+        assertEquals("i", WordUtils.initials("i", array));
+        assertEquals("S", WordUtils.initials("SJC", array));
+        assertEquals("BJL", WordUtils.initials("Ben John Lee", array));
+        assertEquals("BJL", WordUtils.initials("Ben J.Lee", array));
+        assertEquals("BJL", WordUtils.initials(" Ben   John  . Lee", array));
+        assertEquals("KOM", WordUtils.initials("Kay O'Murphy", array));
+        assertEquals("iah1", WordUtils.initials("i am here 123", array));
+        
+        array = "SIJo1".toCharArray();
+        assertEquals(null, WordUtils.initials(null, array));
+        assertEquals("", WordUtils.initials("", array));
+        assertEquals(" ", WordUtils.initials("  ", array));
+        assertEquals("", WordUtils.initials("I", array));
+        assertEquals("i", WordUtils.initials("i", array));
+        assertEquals("C", WordUtils.initials("SJC", array));
+        assertEquals("Bh", WordUtils.initials("Ben John Lee", array));
+        assertEquals("B.", WordUtils.initials("Ben J.Lee", array));
+        assertEquals(" h", WordUtils.initials(" Ben   John  . Lee", array));
+        assertEquals("K", WordUtils.initials("Kay O'Murphy", array));
+        assertEquals("i2", WordUtils.initials("i am here 123", array));
+    }
+
+    // -----------------------------------------------------------------------
     public void testSwapCase_String() {
         assertEquals(null, WordUtils.swapCase(null));
         assertEquals("", WordUtils.swapCase(""));



---------------------------------------------------------------------
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