Class TokenizerCharacterPolicy
- All Implemented Interfaces:
Predicate<CharSequence>
A token consists of one or more letters or digits. Each letter or digit may be followed by zero or more marks. A mark cannot start a token. The supplied sets express a caller's tokenization policy; this class does not infer Unicode character categories.
This predicate tests a complete candidate without finding token boundaries or changing its text.
- Since:
- 3.0.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final TokenizerCharacterPolicyprivate static final intprivate static final Stringprivate final CodePointSetprivate static final Stringprivate final CodePointSetprivate static final Stringprivate final CodePointSetprivate static final Stringprivate static final intprivate static final charprivate static final charprivate static final HexFormat -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateTokenizerCharacterPolicy(CodePointSet letters, CodePointSet digits, CodePointSet marks) -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidappendHex(StringBuilder text, int codePoint) AppendscodePointas upper-case hex with at leastMIN_HEX_DIGITSdigits.private static voidappendRanges(StringBuilder text, CodePointSet set) Appends the members ofsetas comma separated code points and ranges.static TokenizerCharacterPolicyascii()Returns a policy for ASCII letters and digits, with no marks.booleanReturns the immutable digit set.Returns the immutable letter set.getMarks()Returns the immutable mark set.inthashCode()private static IllegalArgumentExceptionReturns the exception for a text form that cannot be read.static TokenizerCharacterPolicyof(CodePointSet letters, CodePointSet digits, CodePointSet marks) Creates a policy from explicit, pairwise-disjoint code point sets.static TokenizerCharacterPolicyParses the text form written bytoString(): the three fieldsletters=,;digits=and;marks=in that order, each followed by zero or more comma separated hex code points (0041) or inclusive ranges (0041-005A).private static intReads the hex code point intext[from, to).private static CodePointSetparseRange(String text, int from, int to) Reads one code point or one inclusive range intext[from, to).private static CodePointSetparseRanges(String text, int from, int to) Reads the comma separated code points and ranges intext[from, to).private static voidrequireDisjoint(String firstName, CodePointSet first, String secondName, CodePointSet second) Throws iffirstandsecondshare a code point.booleantest(CharSequence input) toString()private static voidvalidateSet(String name, CodePointSet set) Throws ifsetcontains a surrogate code point orUnicodeWhitespacemember.
-
Field Details
-
CODE_POINT_FORMAT
- See Also:
-
LETTERS_FIELD
- See Also:
-
DIGITS_FIELD
- See Also:
-
MARKS_FIELD
- See Also:
-
RANGE_SEPARATOR
private static final char RANGE_SEPARATOR- See Also:
-
RANGE_DASH
private static final char RANGE_DASH- See Also:
-
UPPER_HEX
-
MIN_HEX_DIGITS
private static final int MIN_HEX_DIGITS- See Also:
-
BITS_PER_HEX_DIGIT
private static final int BITS_PER_HEX_DIGIT- See Also:
-
ASCII
-
letters
-
digits
-
marks
-
-
Constructor Details
-
TokenizerCharacterPolicy
-
-
Method Details
-
of
public static TokenizerCharacterPolicy of(CodePointSet letters, CodePointSet digits, CodePointSet marks) Creates a policy from explicit, pairwise-disjoint code point sets. Whitespace separates tokens and so cannot be part of one; every other non-surrogate code point is left to the caller.- Parameters:
letters- Code points treated as letters.digits- Code points treated as digits.marks- Code points permitted after a letter, a digit, or another mark.- Returns:
- The immutable policy.
- Throws:
IllegalArgumentException- Thrown if a set isnull,lettersanddigitsare both empty, a code point occurs in more than one set, or a set contains a surrogate code point orUnicodeWhitespacemember.
-
ascii
Returns a policy for ASCII letters and digits, with no marks.- Returns:
- a policy for ASCII letters and digits, with no marks
-
parse
Parses the text form written bytoString(): the three fieldsletters=,;digits=and;marks=in that order, each followed by zero or more comma separated hex code points (0041) or inclusive ranges (0041-005A). Hex digits may use either case; no prefix or whitespace is accepted.- Parameters:
text- The text form to read.- Returns:
- The policy the text describes.
- Throws:
IllegalArgumentException- Thrown iftextisnullor not of that form, or if the sets it describes are rejected byof(CodePointSet, CodePointSet, CodePointSet).
-
test
Returns
trueif the entire input is a non-empty token accepted by this policy. Malformed UTF-16 input (an unpaired or reversed surrogate) returnsfalse.- Specified by:
testin interfacePredicate<CharSequence>- Throws:
IllegalArgumentException- Thrown ifinputisnull.
-
getLetters
Returns the immutable letter set.- Returns:
- the immutable letter set
-
getDigits
Returns the immutable digit set.- Returns:
- the immutable digit set
-
getMarks
Returns the immutable mark set.- Returns:
- the immutable mark set
-
equals
Two policies are equal if their letter, digit and mark sets are equal.
-
hashCode
public int hashCode()Consistent with
equals(Object): computed from the letter, digit and mark sets. -
toString
Writes the text form read by
parse(String), for exampleletters=0041-005A,0061-007A;digits=0030-0039;marks=forascii(): runs of adjacent code points become ranges, and each code point is written with at least four upper-case hex digits. -
appendRanges
Appends the members ofsetas comma separated code points and ranges. -
appendHex
AppendscodePointas upper-case hex with at leastMIN_HEX_DIGITSdigits. -
parseRanges
Reads the comma separated code points and ranges intext[from, to). -
parseRange
Reads one code point or one inclusive range intext[from, to). -
parseHex
Reads the hex code point intext[from, to). -
malformed
Returns the exception for a text form that cannot be read.- Returns:
- the exception for a text form that cannot be read
-
validateSet
Throws ifsetcontains a surrogate code point orUnicodeWhitespacemember. -
requireDisjoint
private static void requireDisjoint(String firstName, CodePointSet first, String secondName, CodePointSet second) Throws iffirstandsecondshare a code point.
-