toIntValue() is used because it extracts the ASCII int value of a char. toInt() would just typecast char to int.
toCharacterObject() is chosen to be consistent with toBooleanObject(). I'm 0 to a change. Stephen ----- Original Message ----- From: "Gary Gregory" <[EMAIL PROTECTED]> I would like to address some naming inconsistencies WRT: > Rename toCharacter to toChar > Rename toInteger to toIntValue > Add toCharacterObject(String) IMHO, this name is correct: char toChar(*) since a char is returned. but this name is not: toIntValue(*) and should really be: toInt(*) I believe the naming pattern should be: toType(*) Where Type is either a primitive type or class. In the case where you can have both, as with "boolean" and "Boolean", you need a different name but this does not have to be dealt with in this class. Also, toCharacterObject(String) should really be toCharacter(String). Ops? Gary > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Sent: Wednesday, March 10, 2004 15:24 > To: [EMAIL PROTECTED] > Subject: cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang > CharUtilsTest.java > > scolebourne 2004/03/10 15:23:46 > > Modified: lang/src/java/org/apache/commons/lang CharUtils.java > lang/src/test/org/apache/commons/lang CharUtilsTest.java > Log: > Rename toCharacter to toChar > Rename toInteger to toIntValue > Add toCharacterObject(String) > > Revision Changes Path > 1.8 +63 -39 jakarta- > commons/lang/src/java/org/apache/commons/lang/CharUtils.java > > Index: CharUtils.java > =================================================================== > RCS file: /home/cvs/jakarta- > commons/lang/src/java/org/apache/commons/lang/CharUtils.java,v > retrieving revision 1.7 > retrieving revision 1.8 > diff -u -r1.7 -r1.8 > --- CharUtils.java 10 Mar 2004 22:59:45 -0000 1.7 > +++ CharUtils.java 10 Mar 2004 23:23:46 -0000 1.8 > @@ -89,21 +89,45 @@ > } > } > > + /** > + * <p>Converts the String to a Character using the first character, > returning > + * null for empty Strings.</p> > + * > + * <p>For ASCII 7 bit characters, this uses a cache that will > return the > + * same Character object each time.</p> > + * > + * <pre> > + * CharUtils.toCharacterObject(null) = null > + * CharUtils.toCharacterObject("") = null > + * CharUtils.toCharacterObject("A") = 'A' > + * CharUtils.toCharacterObject("BA") = 'B' > + * </pre> > + * > + * @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)); > + } > + > //----------------------------------------------------------------- > ------ > /** > * <p>Converts the Character to a char throwing an exception for > <code>null</code>.</p> > * > * <pre> > - * CharUtils.toCharacter(null) = IllegalArgumentException > - * CharUtils.toCharacter(' ') = ' ' > - * CharUtils.toCharacter('A') = 'A' > + * CharUtils.toChar(null) = IllegalArgumentException > + * CharUtils.toChar(' ') = ' ' > + * CharUtils.toChar('A') = 'A' > * </pre> > * > * @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 @@ > * <p>Converts the Character to a char handling > <code>null</code>.</p> > * > * <pre> > - * 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' > * </pre> > * > * @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 @@ > > //----------------------------------------------------------------- > ------ > /** > - * <p>Converts the String to a char using the first character > throwing > + * <p>Converts the String to a char using the first character, > throwing > * an exception on empty Strings.</p> > * > * <pre> > - * 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' > * </pre> > * > * @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 @@ > } > > /** > - * <p>Converts the String to a char using the first character > defaulting > + * <p>Converts the String to a char using the first character, > defaulting > * the value on empty Strings.</p> > * > * <pre> > - * 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' > * </pre> > * > * @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 @@ > * <p>This method coverts the char '1' to the int 1 and so on.</p> > * > * <pre> > - * CharUtils.toInteger('3') = 3 > - * CharUtils.toInteger('A') = IllegalArgumentException > + * CharUtils.toIntValue('3') = 3 > + * CharUtils.toIntValue('A') = IllegalArgumentException > * </pre> > * > * @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 @@ > * <p>This method coverts the char '1' to the int 1 and so on.</p> > * > * <pre> > - * CharUtils.toInteger('3', -1) = 3 > - * CharUtils.toInteger('A', -1) = -1 > + * CharUtils.toIntValue('3', -1) = 3 > + * CharUtils.toIntValue('A', -1) = -1 > * </pre> > * > * @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 @@ > * <p>This method coverts the char '1' to the int 1 and so on.</p> > * > * <pre> > - * CharUtils.toInteger(null) = IllegalArgumentException > - * CharUtils.toInteger('3') = 3 > - * CharUtils.toInteger('A') = IllegalArgumentException > + * CharUtils.toIntValue(null) = IllegalArgumentException > + * CharUtils.toIntValue('3') = 3 > + * CharUtils.toIntValue('A') = IllegalArgumentException > * </pre> > * > * @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 @@ > * <p>This method coverts the char '1' to the int 1 and so on.</p> > * > * <pre> > - * 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 > * </pre> > * > * @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); > } > > //----------------------------------------------------------------- > ------ > > > > 1.4 +54 -45 jakarta- > commons/lang/src/test/org/apache/commons/lang/CharUtilsTest.java > > Index: CharUtilsTest.java > =================================================================== > RCS file: /home/cvs/jakarta- > commons/lang/src/test/org/apache/commons/lang/CharUtilsTest.java,v > retrieving revision 1.3 > retrieving revision 1.4 > diff -u -r1.3 -r1.4 > --- CharUtilsTest.java 24 Feb 2004 22:22:51 -0000 1.3 > +++ CharUtilsTest.java 10 Mar 2004 23:23:46 -0000 1.4 > @@ -88,80 +88,89 @@ > } > } > > + 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_Character() { > - assertEquals('A', CharUtils.toCharacter(CHARACTER_A)); > - assertEquals('B', CharUtils.toCharacter(CHARACTER_B)); > + public void testToChar_Character() { > + assertEquals('A', CharUtils.toChar(CHARACTER_A)); > + assertEquals('B', CharUtils.toChar(CHARACTER_B)); > try { > - CharUtils.toCharacter((Character) null); > + CharUtils.toChar((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 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 testToCharacter_String() { > - assertEquals('A', CharUtils.toCharacter("A")); > - assertEquals('B', CharUtils.toCharacter("BA")); > + public void testToChar_String() { > + assertEquals('A', CharUtils.toChar("A")); > + assertEquals('B', CharUtils.toChar("BA")); > try { > - CharUtils.toCharacter((String) null); > + CharUtils.toChar((String) null); > } catch (IllegalArgumentException ex) {} > try { > - CharUtils.toCharacter(""); > + CharUtils.toChar(""); > } 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_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_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')); > - try { > - CharUtils.toInteger('a'); > + 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.toIntValue('a'); > } 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 testToIntValue_char_int() { > + assertEquals(0, CharUtils.toIntValue('0', -1)); > + assertEquals(3, CharUtils.toIntValue('3', -1)); > + assertEquals(-1, CharUtils.toIntValue('a', -1)); > } > > //----------------------------------------------------------------- > ------ > - public void testToInteger_Character() { > - assertEquals(0, CharUtils.toInteger(new Character('0'))); > - assertEquals(3, CharUtils.toInteger(new Character('3'))); > + public void testToIntValue_Character() { > + assertEquals(0, CharUtils.toIntValue(new Character('0'))); > + assertEquals(3, CharUtils.toIntValue(new Character('3'))); > try { > - CharUtils.toInteger(null); > + CharUtils.toIntValue(null); > } catch (IllegalArgumentException ex) {} > try { > - CharUtils.toInteger(CHARACTER_A); > + CharUtils.toIntValue(CHARACTER_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_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)); > } > > //----------------------------------------------------------------- > ------ > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
