public class CharSet
extends java.lang.Object
implements java.io.Serializable
A set of characters.
Instances are immutable, but instances of subclasses may not be.
#ThreadSafe#
Modifier and Type | Field and Description |
---|---|
static CharSet |
ASCII_ALPHA
A CharSet defining ASCII alphabetic characters "a-zA-Z".
|
static CharSet |
ASCII_ALPHA_LOWER
A CharSet defining ASCII alphabetic characters "a-z".
|
static CharSet |
ASCII_ALPHA_UPPER
A CharSet defining ASCII alphabetic characters "A-Z".
|
static CharSet |
ASCII_NUMERIC
A CharSet defining ASCII alphabetic characters "0-9".
|
protected static java.util.Map<java.lang.String,CharSet> |
COMMON
A Map of the common cases used in the factory.
|
static CharSet |
EMPTY
A CharSet defining no characters.
|
private static long |
serialVersionUID
Required for serialization support.
|
private java.util.Set<CharRange> |
set
The set of CharRange objects.
|
Modifier | Constructor and Description |
---|---|
protected |
CharSet(java.lang.String... set)
Constructs a new CharSet using the set syntax.
|
Modifier and Type | Method and Description |
---|---|
protected void |
add(java.lang.String str)
Add a set definition string to the
CharSet . |
boolean |
contains(char ch)
Does the
CharSet contain the specified
character ch . |
boolean |
equals(java.lang.Object obj)
Compares two
CharSet objects, returning true if they represent
exactly the same set of characters defined in the same way. |
(package private) CharRange[] |
getCharRanges()
Gets the internal set as an array of CharRange objects.
|
static CharSet |
getInstance(java.lang.String... setStrs)
Factory method to create a new CharSet using a special syntax.
|
int |
hashCode()
Gets a hash code compatible with the equals method.
|
java.lang.String |
toString()
Gets a string representation of the set.
|
private static final long serialVersionUID
Serializable
,
Constant Field Valuespublic static final CharSet EMPTY
public static final CharSet ASCII_ALPHA
public static final CharSet ASCII_ALPHA_LOWER
public static final CharSet ASCII_ALPHA_UPPER
public static final CharSet ASCII_NUMERIC
protected static final java.util.Map<java.lang.String,CharSet> COMMON
private final java.util.Set<CharRange> set
protected CharSet(java.lang.String... set)
Constructs a new CharSet using the set syntax. Each string is merged in with the set.
set
- Strings to merge into the initial setjava.lang.NullPointerException
- if set is null
public static CharSet getInstance(java.lang.String... setStrs)
Factory method to create a new CharSet using a special syntax.
null
or empty string ("")
- set containing no charactersThe matching order is:
Matching works left to right. Once a match is found the search starts again from the next character.
If the same range is defined twice using the same syntax, only one range will be kept. Thus, "a-ca-c" creates only one range of "a-c".
If the start and end of a range are in the wrong order, they are reversed. Thus "a-e" is the same as "e-a". As a result, "a-ee-a" would create only one range, as the "a-e" and "e-a" are the same.
The set of characters represented is the union of the specified ranges.
There are two ways to add a literal negation character (^
):
CharSet.getInstance("a-z^")
CharSet.getInstance("^","a-z")
Examples using the negation character:
CharSet.getInstance("^a-c").contains('a') = false CharSet.getInstance("^a-c").contains('d') = true CharSet.getInstance("^^a-c").contains('a') = true // (only '^' is negated) CharSet.getInstance("^^a-c").contains('^') = false CharSet.getInstance("^a-cd-f").contains('d') = true CharSet.getInstance("a-c^").contains('^') = true CharSet.getInstance("^", "a-c").contains('^') = true
All CharSet objects returned by this method will be immutable.
setStrs
- Strings to merge into the set, may be nullprotected void add(java.lang.String str)
Add a set definition string to the CharSet
.
str
- set definition stringCharRange[] getCharRanges()
Gets the internal set as an array of CharRange objects.
public boolean contains(char ch)
Does the CharSet
contain the specified
character ch
.
ch
- the character to check fortrue
if the set contains the characterspublic boolean equals(java.lang.Object obj)
Compares two CharSet
objects, returning true if they represent
exactly the same set of characters defined in the same way.
The two sets abc
and a-c
are not
equal according to this method.
equals
in class java.lang.Object
obj
- the object to compare topublic int hashCode()
Gets a hash code compatible with the equals method.
hashCode
in class java.lang.Object
public java.lang.String toString()
Gets a string representation of the set.
toString
in class java.lang.Object