diff --git a/src/java/org/apache/commons/lang/CharUtils.java b/src/java/org/apache/commons/lang/CharUtils.java
new file mode 100644
index 000000000..df0476eab
--- /dev/null
+++ b/src/java/org/apache/commons/lang/CharUtils.java
@@ -0,0 +1,556 @@
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2004 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowledgement:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgement may appear in the software itself,
+ * if and wherever such third-party acknowledgements normally appear.
+ *
+ * 4. The names "The Jakarta Project", "Commons", and "Apache Software
+ * Foundation" must not be used to endorse or promote products derived
+ * from this software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ *
Operations on char primitives and Char objects.
+ * + *This class tries to handle null
input gracefully.
+ * An exception will not be thrown for a null
input.
+ * Each method documents its behaviour in more detail.
CharUtils
instances should NOT be constructed in standard programming.
+ * Instead, the class should be used as CharUtils.toString('c');
.
This constructor is public to permit tools that require a JavaBean instance + * to operate.
+ */ + public CharUtils() { + } + + //----------------------------------------------------------------------- + /** + *Converts the character to a Character.
+ * + *For ASCII 7 bit characters, this uses a cache that will return the + * same Character object each time.
+ * + *+ * CharUtils.toCharacterObject(' ') = ' ' + * CharUtils.toCharacterObject('A') = 'A' + *+ * + * @param ch the character to convert + * @return a Character of the specified character + */ + public static Character toCharacterObject(char ch) { + if (ch < 128) { + return CHAR_ARRAY[ch]; + } else { + return new Character(ch); + } + } + + //----------------------------------------------------------------------- + /** + *
Converts the Character to a char throwing an exception for null
.
+ * CharUtils.toCharacter(null, 'X') = IllegalArgumentException + * CharUtils.toCharacter(' ', 'X') = ' ' + * CharUtils.toCharacter('A', 'X') = 'A' + *+ * + * @param ch the character to convert + * @return the char value of the Character or the default if null + * @throws IllegalArgumentException if the Character is null + */ + public static char toCharacter(Character ch) { + if (ch == null) { + throw new IllegalArgumentException("The Character must not be null"); + } + return ch.charValue(); + } + + /** + *
Converts the Character to a char handling null
.
+ * CharUtils.toCharacter(null, 'X') = 'X' + * CharUtils.toCharacter(' ', 'X') = ' ' + * CharUtils.toCharacter('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) { + if (ch == null) { + return defaultValue; + } + return ch.charValue(); + } + + //----------------------------------------------------------------------- + /** + *
Converts the String to a char using the first character throwing + * an exception on empty Strings.
+ * + *+ * CharUtils.toCharacter(null, 'X') = IllegalArgumentException + * CharUtils.toCharacter("", 'X') = IllegalArgumentException + * CharUtils.toCharacter("A", 'X') = 'A' + * CharUtils.toCharacter("BA", 'X') = 'B' + *+ * + * @param ch the character to convert + * @return the char value of the Character or the default if null + * @throws IllegalArgumentException if the String is empty + */ + public static char toCharacter(String str) { + if (str == null || str.length() == 0) { + throw new IllegalArgumentException("The String must not be empty"); + } + return str.charAt(0); + } + + /** + *
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' + *+ * + * @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(String str, char defaultValue) { + if (str == null || str.length() == 0) { + return defaultValue; + } + return str.charAt(0); + } + + //----------------------------------------------------------------------- + /** + *
Converts the character to the Integer it represents, throwing an + * exception if the character is not numeric.
+ * + *This method coverts the char '1' to the int 1 and so on.
+ * + *+ * CharUtils.toInteger('3') = 3 + * CharUtils.toInteger('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) { + if (isASCIINumeric(ch) == false) { + throw new IllegalArgumentException("The character " + ch + " is not in the range '0' - '9'"); + } + return (ch - 48); + } + + /** + *
Converts the character to the Integer it represents, throwing an + * exception if the character is not numeric.
+ * + *This method coverts the char '1' to the int 1 and so on.
+ * + *+ * CharUtils.toInteger('3', -1) = 3 + * CharUtils.toInteger('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) { + if (isASCIINumeric(ch) == false) { + return defaultValue; + } + return (ch - 48); + } + + /** + *
Converts the character to the Integer it represents, throwing an + * exception if the character is not numeric.
+ * + *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 + *+ * + * @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) { + if (ch == null) { + throw new IllegalArgumentException("The character must not be null"); + } + return toInteger(ch.charValue()); + } + + /** + *
Converts the character to the Integer it represents, throwing an + * exception if the character is not numeric.
+ * + *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 + *+ * + * @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) { + if (ch == null) { + return defaultValue; + } + return toInteger(ch.charValue(), defaultValue); + } + + //----------------------------------------------------------------------- + /** + *
Converts the character to a String that contains the one character.
+ * + *For ASCII 7 bit characters, this uses a cache that will return the + * same String object each time.
+ * + *+ * CharUtils.toString(' ') = " " + * CharUtils.toString('A') = "A" + *+ * + * @param ch the character to convert + * @return a String containing the one specified character + */ + public static String toString(char ch) { + if (ch < 128) { + return CHAR_STRING_ARRAY[ch]; + } else { + return new String(new char[] {ch}); + } + } + + /** + *
Converts the character to a String that contains the one character.
+ * + *For ASCII 7 bit characters, this uses a cache that will return the + * same String object each time.
+ * + *If null
is passed in, null
will be returned.
+ * CharUtils.toString(null) = null + * CharUtils.toString(' ') = " " + * CharUtils.toString('A') = "A" + *+ * + * @param ch the character to convert + * @return a String containing the one specified character + */ + public static String toString(Character ch) { + if (ch == null) { + return null; + } else { + return toString(ch.charValue()); + } + } + + //-------------------------------------------------------------------------- + /** + *
Converts the string to the unicode format '\u0020'.
+ * + *This format is the Java source code format.
+ * + *+ * CharUtils.toUnicode(' ') = "\u0020" + * CharUtils.toUnicode('A') = "\u0041" + *+ * + * @param ch the character to convert + * @return the escaped unicode string + */ + public static String unicodeEscaped(char ch) { + if (ch < 0x10) { + return "\\u000" + Integer.toHexString(ch); + } else if (ch < 0x100) { + return "\\u00" + Integer.toHexString(ch); + } else if (ch < 0x1000) { + return "\\u0" + Integer.toHexString(ch); + } + return "\\u" + Integer.toHexString(ch); + } + + /** + *
Converts the string to the unicode format '\u0020'.
+ * + *This format is the Java source code format.
+ * + *If null
is passed in, null
will be returned.
+ * CharUtils.toUnicode(null) = null + * CharUtils.toUnicode(' ') = "\u0020" + * CharUtils.toUnicode('A') = "\u0041" + *+ * + * @param ch the character to convert, may be null + * @return the escaped unicode string, null if null input + */ + public static String unicodeEscaped(Character ch) { + if (ch == null) { + return null; + } + return unicodeEscaped(ch.charValue()); + } + + //-------------------------------------------------------------------------- + /** + *
Checks whether the character is ASCII 7 bit.
+ * + *+ * CharUtils.isASCII('a') = true + * CharUtils.isASCII('A') = true + * CharUtils.isASCII('3') = true + * CharUtils.isASCII('-') = true + * CharUtils.isASCII('\n') = true + * CharUtils.isASCII('©') = false + *+ * + * @param ch the character to check + * @return true if less than 128 + */ + public static boolean isASCII(char ch) { + return (ch < 128); + } + + /** + *
Checks whether the character is ASCII 7 bit printable.
+ * + *+ * CharUtils.isASCIIPrintable('a') = true + * CharUtils.isASCIIPrintable('A') = true + * CharUtils.isASCIIPrintable('3') = true + * CharUtils.isASCIIPrintable('-') = true + * CharUtils.isASCIIPrintable('\n') = false + * CharUtils.isASCIIPrintable('©') = false + *+ * + * @param ch the character to check + * @return true if between 32 and 126 inclusive + */ + public static boolean isASCIIPrintable(char ch) { + return (ch >= 32 && ch < 127); + } + + /** + *
Checks whether the character is ASCII 7 bit control.
+ * + *+ * CharUtils.isASCIIControl('a') = false + * CharUtils.isASCIIControl('A') = false + * CharUtils.isASCIIControl('3') = false + * CharUtils.isASCIIControl('-') = false + * CharUtils.isASCIIControl('\n') = true + * CharUtils.isASCIIControl('©') = false + *+ * + * @param ch the character to check + * @return true if less than 32 or equals 127 + */ + public static boolean isASCIIControl(char ch) { + return (ch < 32 || ch == 127); + } + + /** + *
Checks whether the character is ASCII 7 bit alphabetic.
+ * + *+ * CharUtils.isASCIIAlpha('a') = true + * CharUtils.isASCIIAlpha('A') = true + * CharUtils.isASCIIAlpha('3') = false + * CharUtils.isASCIIAlpha('-') = false + * CharUtils.isASCIIAlpha('\n') = false + * CharUtils.isASCIIAlpha('©') = false + *+ * + * @param ch the character to check + * @return true if between 65 and 90 or 97 and 122 inclusive + */ + public static boolean isASCIIAlpha(char ch) { + return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); + } + + /** + *
Checks whether the character is ASCII 7 bit alphabetic upper case.
+ * + *+ * CharUtils.isASCIIAlphaUpper('a') = false + * CharUtils.isASCIIAlphaUpper('A') = true + * CharUtils.isASCIIAlphaUpper('3') = false + * CharUtils.isASCIIAlphaUpper('-') = false + * CharUtils.isASCIIAlphaUpper('\n') = false + * CharUtils.isASCIIAlphaUpper('©') = false + *+ * + * @param ch the character to check + * @return true if between 65 and 90 inclusive + */ + public static boolean isASCIIAlphaUpper(char ch) { + return (ch >= 'A' && ch <= 'Z'); + } + + /** + *
Checks whether the character is ASCII 7 bit alphabetic lower case.
+ * + *+ * CharUtils.isASCIIAlphaLower('a') = true + * CharUtils.isASCIIAlphaLower('A') = false + * CharUtils.isASCIIAlphaLower('3') = false + * CharUtils.isASCIIAlphaLower('-') = false + * CharUtils.isASCIIAlphaLower('\n') = false + * CharUtils.isASCIIAlphaLower('©') = false + *+ * + * @param ch the character to check + * @return true if between 97 and 122 inclusive + */ + public static boolean isASCIIAlphaLower(char ch) { + return (ch >= 'a' && ch <= 'z'); + } + + /** + *
Checks whether the character is ASCII 7 bit numeric.
+ * + *+ * CharUtils.isASCIINumeric('a') = false + * CharUtils.isASCIINumeric('A') = false + * CharUtils.isASCIINumeric('3') = true + * CharUtils.isASCIINumeric('-') = false + * CharUtils.isASCIINumeric('\n') = false + * CharUtils.isASCIINumeric('©') = false + *+ * + * @param ch the character to check + * @return true if between 48 and 57 inclusive + */ + public static boolean isASCIINumeric(char ch) { + return (ch >= '0' && ch <= '9'); + } + + /** + *
Checks whether the character is ASCII 7 bit numeric.
+ * + *+ * CharUtils.isASCIIAlphanumeric('a') = true + * CharUtils.isASCIIAlphanumeric('A') = true + * CharUtils.isASCIIAlphanumeric('3') = true + * CharUtils.isASCIIAlphanumeric('-') = false + * CharUtils.isASCIIAlphanumeric('\n') = false + * CharUtils.isASCIIAlphanumeric('©') = false + *+ * + * @param ch the character to check + * @return true if between 48 and 57 or 65 and 90 or 97 and 122 inclusive + */ + public static boolean isASCIIAlphanumeric(char ch) { + return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'); + } + +} diff --git a/src/test/org/apache/commons/lang/CharUtilsTest.java b/src/test/org/apache/commons/lang/CharUtilsTest.java new file mode 100644 index 000000000..59f575a3f --- /dev/null +++ b/src/test/org/apache/commons/lang/CharUtilsTest.java @@ -0,0 +1,396 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2004 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + *