Rename toCharacter to toChar

Rename  toInteger  to  toIntValue
Add  toCharacterObject(String)


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137824 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-03-10 23:23:46 +00:00
parent 172d1fa7f8
commit 8774e0a985
2 changed files with 126 additions and 93 deletions

View File

@ -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 {
}
}
/**
* <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 @@ public class CharUtils {
* <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 @@ public class CharUtils {
//-----------------------------------------------------------------------
/**
* <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 @@ public class CharUtils {
}
/**
* <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 @@ public class CharUtils {
* <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 @@ public class CharUtils {
* <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 @@ public class CharUtils {
* <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 @@ public class CharUtils {
* <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);
}
//-----------------------------------------------------------------------

View File

@ -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));
}
//-----------------------------------------------------------------------