bb.util
Class CharUtil

java.lang.Object
  extended by bb.util.CharUtil

public final class CharUtil
extends Object

Provides static utility methods for dealing with chars.

Note that the argument type for many of the methods is of int type, as opposed to char type. This was deliberately chosen for these reasons:

  1. as of JDK 1.5, Java now supports Unicode 4.0 which has 1,114,112 code points, which requires an int to represent (Java's char type has insufficient range)
  2. all Readers return int values when do single character reads, in order to indicate EOF by returning -1. So, using int type arguments allows this class's methods to handle EOF values, freeing the programmer from having to first check.
  3. the internal comparison operations done in many of the methods here would promote a char argument to an int anyways, so it is never slower to do a single cast at the start

This class is multithread safe: it is immutable (both its immediate state, as well as the deep state of its fields).

Author:
Brent Boyer

Nested Class Summary
static class CharUtil.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private static int maxAscii
          The maximum (int) value that a valid US-ASCII (i.e. 7 bit) char can have.
private static int maxChar
          The maximum (int) value that a valid Java char can have.
private static int minAscii
          The minimum (int) value that a valid US-ASCII (i.e. 7 bit) char can have.
private static int minChar
          The minimum (int) value that a valid Java char can have.
 
Constructor Summary
private CharUtil()
          This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class.
 
Method Summary
static String getEscapeForLiteral(char c)
          Returns the "simple" (i.e. 2 char) escape sequence for the supplied char, which is suitable for appearing in a Java char or String Literal.
static boolean hasEscapeForLiteral(char c)
          Reports whether or not the supplied char has a "simple" escape sequence that may appear in a Java char or String Literal.
static boolean isAscii(int c)
          Determines whether or not c has a value that a Java char can represent.
static boolean isChar(int c)
          Determines whether or not c has a value that a Java char can represent.
static boolean isDecimalDigit(int c)
          Determines whether or not c represents a decimal digit (i.e. '0', '1', ..., '9').
static boolean isLineEnd(int c)
          Determines whether or not c represents a (lower or upper case) Roman Letter (i.e.
static boolean isRomanLetter(int c)
          Determines whether or not c represents a (lower or upper case) Roman Letter (i.e.
static boolean isRomanLetterLowerCase(int c)
          Determines whether or not c represents a lower case Roman Letter (i.e.
static boolean isRomanLetterUpperCase(int c)
          Determines whether or not c represents an upper case Roman Letter (i.e.
static boolean matches(char c1, char c2, boolean isCaseSensitive)
          Determines whether or not c1 and c2 are matching char values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

minAscii

private static final int minAscii
The minimum (int) value that a valid US-ASCII (i.e. 7 bit) char can have.

See Also:
Constant Field Values

maxAscii

private static final int maxAscii
The maximum (int) value that a valid US-ASCII (i.e. 7 bit) char can have.

See Also:
Constant Field Values

minChar

private static final int minChar
The minimum (int) value that a valid Java char can have.

See Also:
Constant Field Values

maxChar

private static final int maxChar
The maximum (int) value that a valid Java char can have.

See Also:
Constant Field Values
Constructor Detail

CharUtil

private CharUtil()
This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class.

Method Detail

isAscii

public static boolean isAscii(int c)
Determines whether or not c has a value that a Java char can represent.


isChar

public static boolean isChar(int c)
Determines whether or not c has a value that a Java char can represent.


isDecimalDigit

public static boolean isDecimalDigit(int c)
Determines whether or not c represents a decimal digit (i.e. '0', '1', ..., '9').


isLineEnd

public static boolean isLineEnd(int c)
Determines whether or not c represents a (lower or upper case) Roman Letter (i.e. 'a', 'b', ..., 'z', 'A', 'B', ..., 'Z' ).


isRomanLetter

public static boolean isRomanLetter(int c)
Determines whether or not c represents a (lower or upper case) Roman Letter (i.e. 'a', 'b', ..., 'z', 'A', 'B', ..., 'Z' ).


isRomanLetterLowerCase

public static boolean isRomanLetterLowerCase(int c)
Determines whether or not c represents a lower case Roman Letter (i.e. 'a', 'b', ..., 'z').


isRomanLetterUpperCase

public static boolean isRomanLetterUpperCase(int c)
Determines whether or not c represents an upper case Roman Letter (i.e. 'A', 'B', ..., 'Z').


hasEscapeForLiteral

public static boolean hasEscapeForLiteral(char c)
Reports whether or not the supplied char has a "simple" escape sequence that may appear in a Java char or String Literal. (These chars are defined in section 3.10.6 of The Java Language Specification, Third Edition. They are the ones which have 2-char escape sequences which begin with a backslash.)


getEscapeForLiteral

public static String getEscapeForLiteral(char c)
                                  throws IllegalArgumentException
Returns the "simple" (i.e. 2 char) escape sequence for the supplied char, which is suitable for appearing in a Java char or String Literal.

Throws:
IllegalArgumentException - if the char has no "simple" escape (i.e. if CharUtil.hasEscapeForLiteral(c) returns false)

matches

public static boolean matches(char c1,
                              char c2,
                              boolean isCaseSensitive)
Determines whether or not c1 and c2 are matching char values. Immediately returns true if c1 == c2. Else if isCaseSensitive == false, then c1 and c2 are recompared on a case insensitive basis. Only if that too fails is false returned.