diff --git a/src/java/org/apache/commons/lang/CharUtils.java b/src/java/org/apache/commons/lang/CharUtils.java index 55a50488d..8646e6d20 100644 --- a/src/java/org/apache/commons/lang/CharUtils.java +++ b/src/java/org/apache/commons/lang/CharUtils.java @@ -24,7 +24,7 @@ package org.apache.commons.lang; * * @author Stephen Colebourne * @since 2.1 - * @version $Id: CharUtils.java,v 1.7 2004/03/10 22:59:45 scolebourne Exp $ + * @version $Id: CharUtils.java,v 1.8 2004/03/10 23:23:46 scolebourne Exp $ */ public class CharUtils { @@ -89,21 +89,45 @@ public class CharUtils { } } + /** + *

Converts the String to a Character using the first character, returning + * null for empty Strings.

+ * + *

For ASCII 7 bit characters, this uses a cache that will return the + * same Character object each time.

+ * + *
+     *   CharUtils.toCharacterObject(null) = null
+     *   CharUtils.toCharacterObject("")   = null
+     *   CharUtils.toCharacterObject("A")  = 'A'
+     *   CharUtils.toCharacterObject("BA") = 'B'
+     * 
+ * + * @param str the character to convert + * @return the Character value of the first letter of the String + */ + public static Character toCharacterObject(String str) { + if (StringUtils.isEmpty(str)) { + return null; + } + return toCharacterObject(str.charAt(0)); + } + //----------------------------------------------------------------------- /** *

Converts the Character to a char throwing an exception for null.

* *
-     *   CharUtils.toCharacter(null) = IllegalArgumentException
-     *   CharUtils.toCharacter(' ')  = ' '
-     *   CharUtils.toCharacter('A')  = 'A'
+     *   CharUtils.toChar(null) = IllegalArgumentException
+     *   CharUtils.toChar(' ')  = ' '
+     *   CharUtils.toChar('A')  = 'A'
      * 
* * @param ch the character to convert * @return the char value of the Character * @throws IllegalArgumentException if the Character is null */ - public static char toCharacter(Character ch) { + public static char toChar(Character ch) { if (ch == null) { throw new IllegalArgumentException("The Character must not be null"); } @@ -114,16 +138,16 @@ public class CharUtils { *

Converts the Character to a char handling null.

* *
-     *   CharUtils.toCharacter(null, 'X') = 'X'
-     *   CharUtils.toCharacter(' ', 'X')  = ' '
-     *   CharUtils.toCharacter('A', 'X')  = 'A'
+     *   CharUtils.toChar(null, 'X') = 'X'
+     *   CharUtils.toChar(' ', 'X')  = ' '
+     *   CharUtils.toChar('A', 'X')  = 'A'
      * 
* * @param ch the character to convert * @param defaultValue the value to use if the Character is null * @return the char value of the Character or the default if null */ - public static char toCharacter(Character ch, char defaultValue) { + public static char toChar(Character ch, char defaultValue) { if (ch == null) { return defaultValue; } @@ -132,21 +156,21 @@ public class CharUtils { //----------------------------------------------------------------------- /** - *

Converts the String to a char using the first character throwing + *

Converts the String to a char using the first character, throwing * an exception on empty Strings.

* *
-     *   CharUtils.toCharacter(null) = IllegalArgumentException
-     *   CharUtils.toCharacter("")   = IllegalArgumentException
-     *   CharUtils.toCharacter("A")  = 'A'
-     *   CharUtils.toCharacter("BA") = 'B'
+     *   CharUtils.toChar(null) = IllegalArgumentException
+     *   CharUtils.toChar("")   = IllegalArgumentException
+     *   CharUtils.toChar("A")  = 'A'
+     *   CharUtils.toChar("BA") = 'B'
      * 
* * @param str the character to convert - * @return the char value of the Character + * @return the char value of the first letter of the String * @throws IllegalArgumentException if the String is empty */ - public static char toCharacter(String str) { + public static char toChar(String str) { if (StringUtils.isEmpty(str)) { throw new IllegalArgumentException("The String must not be empty"); } @@ -154,21 +178,21 @@ public class CharUtils { } /** - *

Converts the String to a char using the first character defaulting + *

Converts the String to a char using the first character, defaulting * the value on empty Strings.

* *
-     *   CharUtils.toCharacter(null, 'X') = 'X'
-     *   CharUtils.toCharacter("", 'X')   = 'X'
-     *   CharUtils.toCharacter("A", 'X')  = 'A'
-     *   CharUtils.toCharacter("BA", 'X') = 'B'
+     *   CharUtils.toChar(null, 'X') = 'X'
+     *   CharUtils.toChar("", 'X')   = 'X'
+     *   CharUtils.toChar("A", 'X')  = 'A'
+     *   CharUtils.toChar("BA", 'X') = 'B'
      * 
* * @param str the character to convert * @param defaultValue the value to use if the Character is null - * @return the char value of the Character or the default if null + * @return the char value of the first letter of the String or the default if null */ - public static char toCharacter(String str, char defaultValue) { + public static char toChar(String str, char defaultValue) { if (StringUtils.isEmpty(str)) { return defaultValue; } @@ -183,15 +207,15 @@ public class CharUtils { *

This method coverts the char '1' to the int 1 and so on.

* *
-     *   CharUtils.toInteger('3')  = 3
-     *   CharUtils.toInteger('A')  = IllegalArgumentException
+     *   CharUtils.toIntValue('3')  = 3
+     *   CharUtils.toIntValue('A')  = IllegalArgumentException
      * 
* * @param ch the character to convert * @return the int value of the character * @throws IllegalArgumentException if the character is not ASCII numeric */ - public static int toInteger(char ch) { + public static int toIntValue(char ch) { if (isAsciiNumeric(ch) == false) { throw new IllegalArgumentException("The character " + ch + " is not in the range '0' - '9'"); } @@ -205,15 +229,15 @@ public class CharUtils { *

This method coverts the char '1' to the int 1 and so on.

* *
-     *   CharUtils.toInteger('3', -1)  = 3
-     *   CharUtils.toInteger('A', -1)  = -1
+     *   CharUtils.toIntValue('3', -1)  = 3
+     *   CharUtils.toIntValue('A', -1)  = -1
      * 
* * @param ch the character to convert * @param defaultValue the default value to use if the character is not numeric * @return the int value of the character */ - public static int toInteger(char ch, int defaultValue) { + public static int toIntValue(char ch, int defaultValue) { if (isAsciiNumeric(ch) == false) { return defaultValue; } @@ -227,20 +251,20 @@ public class CharUtils { *

This method coverts the char '1' to the int 1 and so on.

* *
-     *   CharUtils.toInteger(null) = IllegalArgumentException
-     *   CharUtils.toInteger('3')  = 3
-     *   CharUtils.toInteger('A')  = IllegalArgumentException
+     *   CharUtils.toIntValue(null) = IllegalArgumentException
+     *   CharUtils.toIntValue('3')  = 3
+     *   CharUtils.toIntValue('A')  = IllegalArgumentException
      * 
* * @param ch the character to convert, not null * @return the int value of the character * @throws IllegalArgumentException if the Character is not ASCII numeric or is null */ - public static int toInteger(Character ch) { + public static int toIntValue(Character ch) { if (ch == null) { throw new IllegalArgumentException("The character must not be null"); } - return toInteger(ch.charValue()); + return toIntValue(ch.charValue()); } /** @@ -250,20 +274,20 @@ public class CharUtils { *

This method coverts the char '1' to the int 1 and so on.

* *
-     *   CharUtils.toInteger(null, -1) = -1
-     *   CharUtils.toInteger('3', -1)  = 3
-     *   CharUtils.toInteger('A', -1)  = -1
+     *   CharUtils.toIntValue(null, -1) = -1
+     *   CharUtils.toIntValue('3', -1)  = 3
+     *   CharUtils.toIntValue('A', -1)  = -1
      * 
* * @param ch the character to convert * @param defaultValue the default value to use if the character is not numeric * @return the int value of the character */ - public static int toInteger(Character ch, int defaultValue) { + public static int toIntValue(Character ch, int defaultValue) { if (ch == null) { return defaultValue; } - return toInteger(ch.charValue(), defaultValue); + return toIntValue(ch.charValue(), defaultValue); } //----------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/lang/CharUtilsTest.java b/src/test/org/apache/commons/lang/CharUtilsTest.java index 37a757c2b..6f0c369f4 100644 --- a/src/test/org/apache/commons/lang/CharUtilsTest.java +++ b/src/test/org/apache/commons/lang/CharUtilsTest.java @@ -27,7 +27,7 @@ import junit.textui.TestRunner; * Unit tests {@link org.apache.commons.lang.CharUtils}. * * @author Stephen Colebourne - * @version $Id: CharUtilsTest.java,v 1.3 2004/02/24 22:22:51 fredrik Exp $ + * @version $Id: CharUtilsTest.java,v 1.4 2004/03/10 23:23:46 scolebourne Exp $ */ public class CharUtilsTest extends TestCase { @@ -88,80 +88,89 @@ public class CharUtilsTest extends TestCase { } } - //----------------------------------------------------------------------- - public void testToCharacter_Character() { - assertEquals('A', CharUtils.toCharacter(CHARACTER_A)); - assertEquals('B', CharUtils.toCharacter(CHARACTER_B)); - try { - CharUtils.toCharacter((Character) null); - } catch (IllegalArgumentException ex) {} - } - - public void testToCharacter_Character_char() { - assertEquals('A', CharUtils.toCharacter(CHARACTER_A, 'X')); - assertEquals('B', CharUtils.toCharacter(CHARACTER_B, 'X')); - assertEquals('X', CharUtils.toCharacter((Character) null, 'X')); + public void testToCharacterObject_String() { + assertEquals(null, CharUtils.toCharacterObject(null)); + assertEquals(null, CharUtils.toCharacterObject("")); + assertEquals(new Character('a'), CharUtils.toCharacterObject("a")); + assertEquals(new Character('a'), CharUtils.toCharacterObject("abc")); + assertSame(CharUtils.toCharacterObject("a"), CharUtils.toCharacterObject("a")); + assertSame(CharUtils.toCharacterObject("a"), CharUtils.toCharacterObject('a')); } //----------------------------------------------------------------------- - public void testToCharacter_String() { - assertEquals('A', CharUtils.toCharacter("A")); - assertEquals('B', CharUtils.toCharacter("BA")); + public void testToChar_Character() { + assertEquals('A', CharUtils.toChar(CHARACTER_A)); + assertEquals('B', CharUtils.toChar(CHARACTER_B)); try { - CharUtils.toCharacter((String) null); - } catch (IllegalArgumentException ex) {} - try { - CharUtils.toCharacter(""); + CharUtils.toChar((Character) null); } catch (IllegalArgumentException ex) {} } - public void testToCharacter_String_char() { - assertEquals('A', CharUtils.toCharacter("A", 'X')); - assertEquals('B', CharUtils.toCharacter("BA", 'X')); - assertEquals('X', CharUtils.toCharacter("", 'X')); - assertEquals('X', CharUtils.toCharacter((String) null, 'X')); + public void testToChar_Character_char() { + assertEquals('A', CharUtils.toChar(CHARACTER_A, 'X')); + assertEquals('B', CharUtils.toChar(CHARACTER_B, 'X')); + assertEquals('X', CharUtils.toChar((Character) null, 'X')); } //----------------------------------------------------------------------- - public void testToInteger_char() { - assertEquals(0, CharUtils.toInteger('0')); - assertEquals(1, CharUtils.toInteger('1')); - assertEquals(2, CharUtils.toInteger('2')); - assertEquals(3, CharUtils.toInteger('3')); - assertEquals(4, CharUtils.toInteger('4')); - assertEquals(5, CharUtils.toInteger('5')); - assertEquals(6, CharUtils.toInteger('6')); - assertEquals(7, CharUtils.toInteger('7')); - assertEquals(8, CharUtils.toInteger('8')); - assertEquals(9, CharUtils.toInteger('9')); + public void testToChar_String() { + assertEquals('A', CharUtils.toChar("A")); + assertEquals('B', CharUtils.toChar("BA")); try { - CharUtils.toInteger('a'); + CharUtils.toChar((String) null); + } catch (IllegalArgumentException ex) {} + try { + CharUtils.toChar(""); } catch (IllegalArgumentException ex) {} } - public void testToInteger_char_int() { - assertEquals(0, CharUtils.toInteger('0', -1)); - assertEquals(3, CharUtils.toInteger('3', -1)); - assertEquals(-1, CharUtils.toInteger('a', -1)); + public void testToChar_String_char() { + assertEquals('A', CharUtils.toChar("A", 'X')); + assertEquals('B', CharUtils.toChar("BA", 'X')); + assertEquals('X', CharUtils.toChar("", 'X')); + assertEquals('X', CharUtils.toChar((String) null, 'X')); } //----------------------------------------------------------------------- - public void testToInteger_Character() { - assertEquals(0, CharUtils.toInteger(new Character('0'))); - assertEquals(3, CharUtils.toInteger(new Character('3'))); + public void testToIntValue_char() { + assertEquals(0, CharUtils.toIntValue('0')); + assertEquals(1, CharUtils.toIntValue('1')); + assertEquals(2, CharUtils.toIntValue('2')); + assertEquals(3, CharUtils.toIntValue('3')); + assertEquals(4, CharUtils.toIntValue('4')); + assertEquals(5, CharUtils.toIntValue('5')); + assertEquals(6, CharUtils.toIntValue('6')); + assertEquals(7, CharUtils.toIntValue('7')); + assertEquals(8, CharUtils.toIntValue('8')); + assertEquals(9, CharUtils.toIntValue('9')); try { - CharUtils.toInteger(null); - } catch (IllegalArgumentException ex) {} - try { - CharUtils.toInteger(CHARACTER_A); + CharUtils.toIntValue('a'); } catch (IllegalArgumentException ex) {} } - public void testToInteger_Character_int() { - assertEquals(0, CharUtils.toInteger(new Character('0'), -1)); - assertEquals(3, CharUtils.toInteger(new Character('3'), -1)); - assertEquals(-1, CharUtils.toInteger(new Character('A'), -1)); - assertEquals(-1, CharUtils.toInteger(null, -1)); + public void testToIntValue_char_int() { + assertEquals(0, CharUtils.toIntValue('0', -1)); + assertEquals(3, CharUtils.toIntValue('3', -1)); + assertEquals(-1, CharUtils.toIntValue('a', -1)); + } + + //----------------------------------------------------------------------- + public void testToIntValue_Character() { + assertEquals(0, CharUtils.toIntValue(new Character('0'))); + assertEquals(3, CharUtils.toIntValue(new Character('3'))); + try { + CharUtils.toIntValue(null); + } catch (IllegalArgumentException ex) {} + try { + CharUtils.toIntValue(CHARACTER_A); + } catch (IllegalArgumentException ex) {} + } + + public void testToIntValue_Character_int() { + assertEquals(0, CharUtils.toIntValue(new Character('0'), -1)); + assertEquals(3, CharUtils.toIntValue(new Character('3'), -1)); + assertEquals(-1, CharUtils.toIntValue(new Character('A'), -1)); + assertEquals(-1, CharUtils.toIntValue(null, -1)); } //-----------------------------------------------------------------------