You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2004/06/03 05:40:28 UTC

cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang WordUtilsTest.java

bayard      2004/06/02 20:40:28

  Modified:    lang/src/java/org/apache/commons/lang WordUtils.java
               lang/src/test/org/apache/commons/lang WordUtilsTest.java
  Log:
  added an overloaded method for capitalize and capitalizeFully so people can do things like capitalize double-barrelled names
  
  Revision  Changes    Path
  1.11      +75 -9     jakarta-commons/lang/src/java/org/apache/commons/lang/WordUtils.java
  
  Index: WordUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/WordUtils.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- WordUtils.java	3 Jun 2004 03:08:33 -0000	1.10
  +++ WordUtils.java	3 Jun 2004 03:40:28 -0000	1.11
  @@ -238,20 +238,64 @@
        * @see #capitalizeFully(String)
        */
       public static String capitalize(String str) {
  -        int strLen;
  -        if (str == null || (strLen = str.length()) == 0) {
  +        return capitalize(str, null);
  +    }
  +
  +    /**
  +     * <p>Capitalizes all the delimiter separated words in a String.
  +     * Only the first letter of each word is changed. To change all letters to
  +     * the capitalized case, use {@link #capitalizeFully(String)}.</p>
  +     *
  +     * <p>A <code>null</code> input String returns <code>null</code>.
  +     * Capitalization uses the unicode title case, normally equivalent to
  +     * upper case.</p>
  +     *
  +     * <pre>
  +     * WordUtils.capitalize(null)        = null
  +     * WordUtils.capitalize("")          = ""
  +     * WordUtils.capitalize("i am FINE") = "I Am FINE"
  +     * </pre>
  +     * 
  +     * @param str  the String to capitalize, may be null
  +     * @param delimiters  characters to capitalize afterwards
  +     * @return capitalized String, <code>null</code> if null String input
  +     * @see #uncapitalize(String)
  +     * @see #capitalizeFully(String)
  +     */
  +    public static String capitalize(String str, char[] delimiters) {
  +        if (str == null || str.length() == 0) {
               return str;
           }
  +        int strLen = str.length();
           StringBuffer buffer = new StringBuffer(strLen);
  -        boolean whitespace = true;
  +
  +        int delimitersLen = 0;
  +        if(delimiters != null) {
  +            delimitersLen = delimiters.length;
  +        }
  +
  +        boolean capitalizeNext = true;
           for (int i = 0; i < strLen; i++) {
               char ch = str.charAt(i);
  -            if (Character.isWhitespace(ch)) {
  +
  +            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) {
                   buffer.append(ch);
  -                whitespace = true;
  -            } else if (whitespace) {
  +                capitalizeNext = true;
  +            } else if (capitalizeNext) {
                   buffer.append(Character.toTitleCase(ch));
  -                whitespace = false;
  +                capitalizeNext = false;
               } else {
                   buffer.append(ch);
               }
  @@ -278,11 +322,33 @@
        * @return capitalized String, <code>null</code> if null String input
        */
       public static String capitalizeFully(String str) {
  +        return capitalizeFully(str, null);
  +    }
  +
  +    /**
  +     * <p>Capitalizes all the delimiter separated words in a String.
  +     * All letters are changed, so the resulting string will be fully changed.</p>
  +     *
  +     * <p>A <code>null</code> input String returns <code>null</code>.
  +     * Capitalization uses the unicode title case, normally equivalent to
  +     * upper case.</p>
  +     *
  +     * <pre>
  +     * WordUtils.capitalize(null)        = null
  +     * WordUtils.capitalize("")          = ""
  +     * WordUtils.capitalize("i am FINE") = "I Am Fine"
  +     * </pre>
  +     * 
  +     * @param str  the String to capitalize, may be null
  +     * @param delimiters  characters to capitalize afterwards
  +     * @return capitalized String, <code>null</code> if null String input
  +     */
  +    public static String capitalizeFully(String str, char[] delimiters) {
           if (str == null || str.length() == 0) {
               return str;
           }
           str = str.toLowerCase();
  -        return capitalize(str);
  +        return capitalize(str, delimiters);
       }
   
       /**
  
  
  
  1.6       +31 -3     jakarta-commons/lang/src/test/org/apache/commons/lang/WordUtilsTest.java
  
  Index: WordUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/WordUtilsTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- WordUtilsTest.java	18 Feb 2004 23:06:19 -0000	1.5
  +++ WordUtilsTest.java	3 Jun 2004 03:40:28 -0000	1.6
  @@ -172,19 +172,47 @@
           assertEquals("I AM HERE 123", WordUtils.capitalize("I AM HERE 123") );
       }
       
  +    public void testCapitalizeWithDelimiters_String() {
  +        assertEquals(null, WordUtils.capitalize(null, null));
  +        assertEquals("", WordUtils.capitalize("", new char[0]));
  +        assertEquals("  ", WordUtils.capitalize("  ", new char[0]));
  +        
  +        char[] chars = new char[] { '-', '+', ' ', '@' };
  +        assertEquals("I", WordUtils.capitalize("I", chars) );
  +        assertEquals("I", WordUtils.capitalize("i", chars) );
  +        assertEquals("I-Am Here+123", WordUtils.capitalize("i-am here+123", chars) );
  +        assertEquals("I Am+Here-123", WordUtils.capitalize("I Am+Here-123", chars) );
  +        assertEquals("I+Am-HERE 123", WordUtils.capitalize("i+am-HERE 123", chars) );
  +        assertEquals("I-AM HERE+123", WordUtils.capitalize("I-AM HERE+123", chars) );
  +    }
  +
       public void testCapitalizeFully_String() {
           assertEquals(null, WordUtils.capitalizeFully(null));
           assertEquals("", WordUtils.capitalizeFully(""));
           assertEquals("  ", WordUtils.capitalizeFully("  "));
           
  -        assertEquals("I", WordUtils.capitalize("I") );
  -        assertEquals("I", WordUtils.capitalize("i") );
  +        assertEquals("I", WordUtils.capitalizeFully("I") );
  +        assertEquals("I", WordUtils.capitalizeFully("i") );
           assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am here 123") );
           assertEquals("I Am Here 123", WordUtils.capitalizeFully("I Am Here 123") );
           assertEquals("I Am Here 123", WordUtils.capitalizeFully("i am HERE 123") );
           assertEquals("I Am Here 123", WordUtils.capitalizeFully("I AM HERE 123") );
       }
       
  +    public void testCapitalizeFullyWithDelimiters_String() {
  +        assertEquals(null, WordUtils.capitalizeFully(null, null));
  +        assertEquals("", WordUtils.capitalizeFully("", new char[0]));
  +        assertEquals("  ", WordUtils.capitalizeFully("  ", new char[0]));
  +        
  +        char[] chars = new char[] { '-', '+', ' ', '@' };
  +        assertEquals("I", WordUtils.capitalizeFully("I", chars) );
  +        assertEquals("I", WordUtils.capitalizeFully("i", chars) );
  +        assertEquals("I-Am Here+123", WordUtils.capitalizeFully("i-am here+123", chars) );
  +        assertEquals("I Am+Here-123", WordUtils.capitalizeFully("I Am+Here-123", chars) );
  +        assertEquals("I+Am-Here 123", WordUtils.capitalizeFully("i+am-HERE 123", chars) );
  +        assertEquals("I-Am Here+123", WordUtils.capitalizeFully("I-AM HERE+123", chars) );
  +    }
  +
       public void testUncapitalize_String() {
           assertEquals(null, WordUtils.uncapitalize(null));
           assertEquals("", WordUtils.uncapitalize(""));
  
  
  

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


Re: cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang WordUtilsTest.java

Posted by Henri Yandell <ba...@generationjava.com>.
Thanks, javadoc change made.

Hen

On Thu, 3 Jun 2004, C. Scott Ananian wrote:

> On Wed, 3 Jun 2004 bayard@apache.org wrote:
>
> >   +    /**
> >   +     * <p>Capitalizes all the delimiter separated words in a String.
>                                    delimiter-separated
> >   +     * Only the first letter of each word is changed. To change all letters to
> [...]
> >   +     * @param str  the String to capitalize, may be null
> >   +     * @param delimiters  characters to capitalize afterwards
>
> "characters to capitalize afterwards" is not clear -- it doesn't include
> the first letter in the string, for example.
>
> Better would be "A set of characters understood to separate words.  The
> first string character and the first non-delimiter character after a
> delimiter will be capitalized."
>
> Alternatively, "A set of characters understood to separate words.  Each
> word will have its first character capitalized."
>
> >   +     * @param str  the String to capitalize, may be null
> >   +     * @param delimiters  characters to capitalize afterwards
> >   +     * @return capitalized String, <code>null</code> if null String input
> >   +     */
> >   +    public static String capitalizeFully(String str, char[] delimiters) {
>
> Same thing here.
>
> "@param delimiters A set of characters understood to separate words.
>                    All non-delimiter characters will be capitalized."
>  --scott
>
> Moscow Marxist mail drop Noriega Panama fissionable Dictionary security
> COBRA JUDY South Africa Sudan bomb LA overthrow DC OVER THE HORIZON RADAR
>                          ( http://cscott.net/ )
>


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


Re: cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang WordUtilsTest.java

Posted by "C. Scott Ananian" <cs...@cscott.net>.
On Wed, 3 Jun 2004 bayard@apache.org wrote:

>   +    /**
>   +     * <p>Capitalizes all the delimiter separated words in a String.
                                   delimiter-separated
>   +     * Only the first letter of each word is changed. To change all letters to
[...]
>   +     * @param str  the String to capitalize, may be null
>   +     * @param delimiters  characters to capitalize afterwards

"characters to capitalize afterwards" is not clear -- it doesn't include
the first letter in the string, for example.

Better would be "A set of characters understood to separate words.  The
first string character and the first non-delimiter character after a
delimiter will be capitalized."

Alternatively, "A set of characters understood to separate words.  Each
word will have its first character capitalized."

>   +     * @param str  the String to capitalize, may be null
>   +     * @param delimiters  characters to capitalize afterwards
>   +     * @return capitalized String, <code>null</code> if null String input
>   +     */
>   +    public static String capitalizeFully(String str, char[] delimiters) {

Same thing here.

"@param delimiters A set of characters understood to separate words.
                   All non-delimiter characters will be capitalized."
 --scott

Moscow Marxist mail drop Noriega Panama fissionable Dictionary security
COBRA JUDY South Africa Sudan bomb LA overthrow DC OVER THE HORIZON RADAR
                         ( http://cscott.net/ )

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