Class CharMatcher


  • public abstract class CharMatcher
    extends Object
    Extracted from Guava.

    Determines a true or false value for any Java char value, just as Predicate does for any Object. 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 value c for which this.matches(c) returns true".

    Warning: This class deals only with char values; it does not understand supplementary Unicode code points in the range 0x10000 to 0x10FFFF. Such logical characters are encoded into a String using surrogate pairs, and a CharMatcher treats these just as two separate characters.

    See the Guava User Guide article on CharMatcher .

    Author:
    Kevin Bourrillion
    • Constructor Detail

      • CharMatcher

        protected CharMatcher()
        Constructor for use by subclasses. When subclassing, you may want to override toString() to provide a useful description.
    • 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 a char matcher that matches any character except the one specified.

        To negate another CharMatcher, use negate().

        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 by Character.isISOControl(char).
        Returns:
        a CharMatcher that matches ISO control character
      • is

        public static CharMatcher is​(char match)
        Returns a char 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 a char 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 a char 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 this CharMatcher 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 and other.
        Parameters:
        other - the other instance
        Returns:
        new CharMatcher instance representing the logical and of this instance and the other instance
      • or

        public CharMatcher or​(CharMatcher other)
        Returns a matcher that matches any character matched by either this matcher or other.
        Parameters:
        other - the other instance
        Returns:
        new CharMatcher instance representing the logical and of this instance and the other instance
      • matchesAnyOf

        public boolean matchesAnyOf​(CharSequence sequence)
        Returns true 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 returns true 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)
        Returns true if a character sequence contains only matching characters.

        The default implementation iterates over the sequence, invoking matches(char) for each character, until this returns false 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)
        Returns true 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 returns true 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, calling matches(char) for each character.

        Parameters:
        sequence - the character sequence to examine
        start - the first index to examine; must be nonnegative and no greater than sequence.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 than sequence.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