Top Description Fields Constructors Methods
org.apache.commons.lang3

public Class CharSet

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.Serializable, java.util.Collections, .HashMap, .HashSet, .Map, .Set, java.util.stream.Stream

A set of characters.

Instances are immutable, but instances of subclasses may not be.

#ThreadSafe#

Since
1.0

Field Summary

Modifier and TypeField and Description
public static final CharSet
ASCII_ALPHA

A CharSet defining ASCII alphabetic characters "a-zA-Z".

public static final CharSet
ASCII_ALPHA_LOWER

A CharSet defining ASCII alphabetic characters "a-z".

public static final CharSet
ASCII_ALPHA_UPPER

A CharSet defining ASCII alphabetic characters "A-Z".

public static final CharSet
ASCII_NUMERIC

A CharSet defining ASCII alphabetic characters "0-9".

protected static final Map<String, CharSet>
COMMON

A Map of the common cases used in the factory.

public static final CharSet
EMPTY

A CharSet defining no characters.

private static final long
serialVersionUID

Required for serialization support.

private final Set<CharRange>
set

The set of CharRange objects.

Constructor Summary

AccessConstructor and Description
protected
CharSet(final String...
Strings to merge into the initial set
set
)

Constructs a new CharSet using the set syntax.

Method Summary

Modifier and TypeMethod and Description
protected void
add(final String
set definition string
str
)

Add a set definition string to the CharSet.

public boolean

Returns:

true if the set contains the characters
contains
(final char
the character to check for
ch
)

Does the CharSet contain the specified character ch.

public boolean

Returns:

true if equal
equals
(final Object
the object to compare to
obj
)

Overrides java.lang.Object.equals.

Compares two CharSet objects, returning true if they represent exactly the same set of characters defined in the same way.

pack-priv CharRange[]

Returns:

an array of immutable CharRange objects
getCharRanges
()

Gets the internal set as an array of CharRange objects.

public static CharSet

Returns:

a CharSet instance
getInstance
(final String...
Strings to merge into the set, may be null
setStrs
)

Factory method to create a new CharSet using a special syntax.

public int

Returns:

a suitable hash code
hashCode
()

Overrides java.lang.Object.hashCode.

Gets a hash code compatible with the equals method.

public String

Returns:

string representation of the set
toString
()

Overrides java.lang.Object.toString.

Gets a string representation of the set.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

ASCII_ALPHAback to summary
public static final CharSet ASCII_ALPHA

A CharSet defining ASCII alphabetic characters "a-zA-Z".

Since
2.0
ASCII_ALPHA_LOWERback to summary
public static final CharSet ASCII_ALPHA_LOWER

A CharSet defining ASCII alphabetic characters "a-z".

Since
2.0
ASCII_ALPHA_UPPERback to summary
public static final CharSet ASCII_ALPHA_UPPER

A CharSet defining ASCII alphabetic characters "A-Z".

Since
2.0
ASCII_NUMERICback to summary
public static final CharSet ASCII_NUMERIC

A CharSet defining ASCII alphabetic characters "0-9".

Since
2.0
COMMONback to summary
protected static final Map<String, CharSet> COMMON

A Map of the common cases used in the factory. Subclasses can add more common patterns if desired

Since
2.0
EMPTYback to summary
public static final CharSet EMPTY

A CharSet defining no characters.

Since
2.0
serialVersionUIDback to summary
private static final long serialVersionUID

Required for serialization support. Lang version 2.0.

See Also
java.io.Serializable
setback to summary
private final Set<CharRange> set

The set of CharRange objects.

Constructor Detail

CharSetback to summary
protected CharSet(final String... set)

Constructs a new CharSet using the set syntax. Each string is merged in with the set.

Parameters
set:String[]

Strings to merge into the initial set

Exceptions
NullPointerException:
if set is null

Method Detail

addback to summary
protected void add(final String str)

Add a set definition string to the CharSet.

Parameters
str:String

set definition string

containsback to summary
public boolean contains(final char ch)

Does the CharSet contain the specified character ch.

Parameters
ch:char

the character to check for

Returns:boolean

true if the set contains the characters

equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

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.

Parameters
obj:Object

the object to compare to

Returns:boolean

true if equal

Annotations
@Override
Since
2.0
getCharRangesback to summary
pack-priv CharRange[] getCharRanges()

Gets the internal set as an array of CharRange objects.

Returns:CharRange[]

an array of immutable CharRange objects

Since
2.0
getInstanceback to summary
public static CharSet getInstance(final String... setStrs)

Factory method to create a new CharSet using a special syntax.

  • null or empty string ("") - set containing no characters
  • Single character, such as "a" - set containing just that character
  • Multi character, such as "a-e" - set containing characters from one character to the other
  • Negated, such as "^a" or "^a-e" - set containing all characters except those defined
  • Combinations, such as "abe-g" - set containing all the characters from the individual sets

The matching order is:

  1. Negated multi character range, such as "^a-e"
  2. Ordinary multi character range, such as "a-e"
  3. Negated single character, such as "^a"
  4. Ordinary single character, such as "a"

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 (^):

  • As the last character in a string, e.g. CharSet.getInstance("a-z^")
  • As a separate element, e.g. 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.

Parameters
setStrs:String[]

Strings to merge into the set, may be null

Returns:CharSet

a CharSet instance

Since
2.4
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Gets a hash code compatible with the equals method.

Returns:int

a suitable hash code

Annotations
@Override
Since
2.0
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Gets a string representation of the set.

Returns:String

string representation of the set

Annotations
@Override