- java.lang.Object
-
- io.helidon.common.http.CharMatcher
-
public abstract class CharMatcher extends Object
Extracted from Guava.Determines a true or false value for any Java
char
value, just asPredicate
does for anyObject
. Also offers basic text processing methods based on this function. Implementations are strongly encouraged to be side-effect-free and immutable.Throughout the documentation of this class, the phrase "matching character" is used to mean "any
char
valuec
for whichthis.matches(c)
returnstrue
".Warning: This class deals only with
char
values; it does not understand supplementary Unicode code points in the range0x10000
to0x10FFFF
. Such logical characters are encoded into aString
using surrogate pairs, and aCharMatcher
treats these just as two separate characters.See the Guava User Guide article on
CharMatcher
.- Author:
- Kevin Bourrillion
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
CharMatcher()
Constructor for use by subclasses.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description CharMatcher
and(CharMatcher other)
Returns a matcher that matches any character matched by both this matcher andother
.static CharMatcher
any()
Matches any character.static CharMatcher
anyOf(CharSequence sequence)
Returns achar
matcher that matches any character present in the given character sequence.static CharMatcher
ascii()
Determines whether a character is ASCII, meaning that its code point is less than 128.int
countIn(CharSequence sequence)
Returns the number of matching characters found in a character sequence.int
indexIn(CharSequence sequence)
Returns the index of the first matching character in a character sequence, or-1
if no matching character is present.int
indexIn(CharSequence sequence, int start)
Returns the index of the first matching character in a character sequence, starting from a given position, or-1
if no character matches after that position.static CharMatcher
is(char match)
Returns achar
matcher that matches only one specified character.static CharMatcher
isNot(char match)
Returns achar
matcher that matches any character except the one specified.static CharMatcher
javaIsoControl()
Determines whether a character is an ISO control character as specified byCharacter.isISOControl(char)
.abstract boolean
matches(char c)
Determines a true or false value for the given character.boolean
matchesAllOf(CharSequence sequence)
Returnstrue
if a character sequence contains only matching characters.boolean
matchesAnyOf(CharSequence sequence)
Returnstrue
if a character sequence contains at least one matching character.boolean
matchesNoneOf(CharSequence sequence)
Returnstrue
if a character sequence contains no matching characters.CharMatcher
negate()
Returns a matcher that matches any character not matched by this matcher.static CharMatcher
none()
Matches no character.static CharMatcher
noneOf(CharSequence sequence)
Returns achar
matcher that matches any character not present in the given character sequence.CharMatcher
or(CharMatcher other)
Returns a matcher that matches any character matched by either this matcher orother
.
-
-
-
Method Detail
-
ascii
public static CharMatcher ascii()
Determines whether a character is ASCII, meaning that its code point is less than 128.- Returns:
- a CharMatcher instance that matches ASCII characters
-
isNot
public static CharMatcher isNot(char match)
Returns achar
matcher that matches any character except the one specified.To negate another
CharMatcher
, usenegate()
.- Parameters:
match
- the character that should not match- Returns:
- a CharMatcher instance that matches any character except the one specified
-
any
public static CharMatcher any()
Matches any character.- Returns:
- a CharMatcher that matches any character
-
none
public static CharMatcher none()
Matches no character.- Returns:
- a CharMatcher that matches no character
-
javaIsoControl
public static CharMatcher javaIsoControl()
Determines whether a character is an ISO control character as specified byCharacter.isISOControl(char)
.- Returns:
- a CharMatcher that matches ISO control character
-
is
public static CharMatcher is(char match)
Returns achar
matcher that matches only one specified character.- Parameters:
match
- the character that should match- Returns:
- a CharMatcher that matches the one specified character
-
noneOf
public static CharMatcher noneOf(CharSequence sequence)
Returns achar
matcher that matches any character not present in the given character sequence.- Parameters:
sequence
- all the characters that should not be matched- Returns:
- a CharMatcher that matches any character not present in the given sequence
-
anyOf
public static CharMatcher anyOf(CharSequence sequence)
Returns achar
matcher that matches any character present in the given character sequence.- Parameters:
sequence
- all the characters that should be matched- Returns:
- a CharMatcher that matches any character present in the given sequence
-
matches
public abstract boolean matches(char c)
Determines a true or false value for the given character.- Parameters:
c
- the character to match- Returns:
true
if thisCharMatcher
instance matches the given character,false
otherwise
-
negate
public CharMatcher negate()
Returns a matcher that matches any character not matched by this matcher.- Returns:
- new
CharMatcher
instance representing the logical negation of this instance
-
and
public CharMatcher and(CharMatcher other)
Returns a matcher that matches any character matched by both this matcher andother
.- Parameters:
other
- the other instance- Returns:
- new
CharMatcher
instance representing the logical and of this instance and theother
instance
-
or
public CharMatcher or(CharMatcher other)
Returns a matcher that matches any character matched by either this matcher orother
.- Parameters:
other
- the other instance- Returns:
- new
CharMatcher
instance representing the logical and of this instance and theother
instance
-
matchesAnyOf
public boolean matchesAnyOf(CharSequence sequence)
Returnstrue
if a character sequence contains at least one matching character. Equivalent to!matchesNoneOf(sequence)
.The default implementation iterates over the sequence, invoking
matches(char)
for each character, until this returnstrue
or the end is reached.- Parameters:
sequence
- the character sequence to examine, possibly empty- Returns:
true
if this matcher matches at least one character in the sequence- Since:
- 8.0
-
matchesAllOf
public boolean matchesAllOf(CharSequence sequence)
Returnstrue
if a character sequence contains only matching characters.The default implementation iterates over the sequence, invoking
matches(char)
for each character, until this returnsfalse
or the end is reached.- Parameters:
sequence
- the character sequence to examine, possibly empty- Returns:
true
if this matcher matches every character in the sequence, including when the sequence is empty
-
matchesNoneOf
public boolean matchesNoneOf(CharSequence sequence)
Returnstrue
if a character sequence contains no matching characters. Equivalent to!matchesAnyOf(sequence)
.The default implementation iterates over the sequence, invoking
matches(char)
for each character, until this returnstrue
or the end is reached.- Parameters:
sequence
- the character sequence to examine, possibly empty- Returns:
true
if this matcher matches no characters in the sequence, including when the sequence is empty
-
indexIn
public int indexIn(CharSequence sequence)
Returns the index of the first matching character in a character sequence, or-1
if no matching character is present.The default implementation iterates over the sequence in forward order calling
matches(char)
for each character.- Parameters:
sequence
- the character sequence to examine from the beginning- Returns:
- an index, or
-1
if no character matches
-
indexIn
public int indexIn(CharSequence sequence, int start)
Returns the index of the first matching character in a character sequence, starting from a given position, or-1
if no character matches after that position.The default implementation iterates over the sequence in forward order, beginning at
start
, callingmatches(char)
for each character.- Parameters:
sequence
- the character sequence to examinestart
- the first index to examine; must be nonnegative and no greater thansequence.length()
- Returns:
- the index of the first matching character, guaranteed to be no less than
start
, or-1
if no character matches - Throws:
IndexOutOfBoundsException
- if start is negative or greater thansequence.length()
-
countIn
public int countIn(CharSequence sequence)
Returns the number of matching characters found in a character sequence.- Parameters:
sequence
- sequence to count the number of matching characters- Returns:
- count of matching characters
-
-