public final class TextScanner extends Scanner
Scanner
implementation that scans text.
In order to use TextScanner
, consider the following example:
String string = "ABCDEF";
StringBuilder stringBuilder = new StringBuilder();
TextScanner textScanner = TextScanner.newInstance(string);
textScanner.nextRegexConcatenation("[A-C]{3}", "[D-F]{3}");
textScanner.consume(stringBuilder);
System.out.println(stringBuilder);
Modifier and Type | Field and Description |
---|---|
static char |
EOF
A
char that represents end of file. |
Modifier and Type | Method and Description |
---|---|
void |
error()
Causes a
ScannerException to be thrown with an implementation-specific detail message. |
void |
error(java.lang.String message)
Causes a
ScannerException to be thrown with the given detail message. |
boolean |
isRecordingRegexMatchingTime() |
static TextScanner |
newInstance(java.io.File file)
Returns a new
TextScanner instance given a File . |
static TextScanner |
newInstance(java.lang.String string)
Returns a new
TextScanner instance given a String . |
boolean |
next()
A simple method for advancing the index at the end of the next consumption.
|
boolean |
nextCharacter()
This method attempts to advance the index at the end of the next consumption by one
char . |
boolean |
nextCharacter(char character)
This method attempts to advance the index at the end of the next consumption by one
char , but only if that char is the same as character . |
boolean |
nextCharacterAlternation(char... characters)
This method attempts to advance the index at the end of the next consumption by one
char , but only if that char is one of the char s in characters . |
boolean |
nextCharacterConcatenation(char... characters)
This method attempts to advance the index at the end of the next consumption by
characters.length char s, but only if they can all match one char each. |
boolean |
nextRegex(java.util.regex.Pattern pattern)
This method attempts to advance the index at the end of the next consumption based on if it matches
pattern . |
boolean |
nextRegex(java.util.regex.Pattern pattern,
java.util.function.Consumer<java.util.regex.MatchResult> matchResultConsumer)
This method attempts to advance the index at the end of the next consumption based on if it matches
pattern . |
boolean |
nextRegex(java.lang.String regex)
This method attempts to advance the index at the end of the next consumption based on if it matches
Pattern.compile(regex) . |
boolean |
nextRegexAlternation(java.util.regex.Pattern... patterns)
This method attempts to advance the index at the end of the next consumption based on the first
Pattern in patterns that it matches. |
boolean |
nextRegexAlternation(java.lang.String... regexes)
This method attempts to advance the index at the end of the next consumption based on the first match made by
Pattern.compile(regex) for each String in regexes . |
boolean |
nextRegexConcatenation(java.util.regex.Pattern... patterns)
This method attempts to advance the index at the end of the next consumption by
patterns.length Pattern s, but only if they can all match one Pattern each. |
boolean |
nextRegexConcatenation(java.lang.String... regexes) |
boolean |
nextString(java.lang.String string) |
boolean |
nextStringAlternation(java.lang.String... strings) |
boolean |
nextStringConcatenation(java.lang.String... strings) |
char |
peekCharacter() |
char |
peekCharacter(int relativeIndex) |
java.lang.String |
peekString(int length) |
java.lang.String |
peekString(int relativeIndex,
int length) |
void |
printRegexMatchingTimes() |
void |
setRecordingRegexMatchingTime(boolean isRecordingRegexMatchingTime) |
boolean |
testNextCharacter() |
boolean |
testNextCharacter(char character) |
boolean |
testNextCharacterAlternation(char... characters) |
boolean |
testNextCharacterConcatenation(char... characters) |
boolean |
testNextRegex(java.util.regex.Pattern pattern) |
boolean |
testNextRegex(java.util.regex.Pattern pattern,
java.util.function.Consumer<java.util.regex.MatchResult> matchResultConsumer) |
boolean |
testNextRegex(java.lang.String regex) |
boolean |
testNextRegexAlternation(java.util.regex.Pattern... patterns) |
boolean |
testNextRegexAlternation(java.lang.String... regexes) |
boolean |
testNextRegexConcatenation(java.util.regex.Pattern... patterns) |
boolean |
testNextRegexConcatenation(java.lang.String... regexes) |
boolean |
testNextString(java.lang.String string) |
boolean |
testNextStringAlternation(java.lang.String... strings) |
boolean |
testNextStringConcatenation(java.lang.String... strings) |
java.lang.String |
toString()
Returns a
String representation of this TextScanner instance. |
java.lang.String |
toString(int absoluteIndex)
Returns a
String representation of the data at the given absolute index. |
consume, consume, consumption, getIndexAtBeginningOfConsumption, getIndexAtEndOfConsumption, mark, rewind, setIndexAtBeginningOfConsumption, setIndexAtEndOfConsumption, skip, testNext, unmark
public static final char EOF
char
that represents end of file.public boolean isRecordingRegexMatchingTime()
public boolean next()
If it succeeds, true
is returned, false
otherwise.
This method is implemented in terms of the nextCharacter()
method.
public boolean nextCharacter()
char
.
If it succeeds, true
is returned, false
otherwise.
true
if, and only if, this method succeeds, false
otherwisepublic boolean nextCharacter(char character)
char
, but only if that char
is the same as character
.
If it succeeds, true
is returned, false
otherwise.
character
- a char
true
if, and only if, the index at the end of the next consumption is advanced, false
otherwisepublic boolean nextCharacterAlternation(char... characters)
char
, but only if that char
is one of the char
s in characters
.
If it succeeds, true
is returned, false
otherwise.
If characters
is null
, a NullPointerException
will be thrown.
characters
- an array of char
strue
if, and only if, the index at the end of the next consumption is advanced, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, characters
is null
public boolean nextCharacterConcatenation(char... characters)
characters.length
char
s, but only if they can all match one char
each.
If it succeeds, true
is returned, false
otherwise.
If characters
is null
, a NullPointerException
will be thrown.
characters
- an array of char
strue
if, and only if, the index at the end of the next consumption is advanced, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, characters
is null
public boolean nextRegex(java.util.regex.Pattern pattern)
pattern
.
If it succeeds, true
is returned, false
otherwise.
If pattern
is null
, a NullPointerException
will be thrown.
pattern
- a Pattern
true
if, and only if, the index at the end of the next consumption is advanced, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, pattern
is null
public boolean nextRegex(java.util.regex.Pattern pattern, java.util.function.Consumer<java.util.regex.MatchResult> matchResultConsumer)
pattern
.
If a match is found, a MatchResult
will be handed to matchResultConsumer
.
If it succeeds, true
is returned, false
otherwise.
If pattern
is null
, a NullPointerException
will be thrown.
If matchResultConsumer
is null
, and a match was found, a NullPointerException
will be thrown.
pattern
- a Pattern
matchResultConsumer
- a Consumer
of MatchResult
strue
if, and only if, the index at the end of the next consumption is advanced, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, pattern
is null
, or matchResultConsumer
is null
and a match was foundpublic boolean nextRegex(java.lang.String regex)
Pattern.compile(regex)
.
If it succeeds, true
is returned, false
otherwise.
If regex
is null
, a NullPointerException
will be thrown.
regex
- a String
true
if, and only if, the index at the end of the next consumption is advanced, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, regex
is null
public boolean nextRegexAlternation(java.util.regex.Pattern... patterns)
Pattern
in patterns
that it matches.
If it succeeds, true
is returned, false
otherwise.
If patterns
or any element within patterns
are null
, a NullPointerException
will be thrown.
patterns
- an array of Pattern
strue
if, and only if, the index at the end of the next consumption is advanced, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, patterns
or any element within patterns
are null
public boolean nextRegexAlternation(java.lang.String... regexes)
Pattern.compile(regex)
for each String
in regexes
.
If it succeeds, true
is returned, false
otherwise.
If regexes
or any element within regexes
are null
, a NullPointerException
will be thrown.
regexes
- an array of String
strue
if, and only if, the index at the end of the next consumption is advanced, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, regexes
or any element within regexes
are null
public boolean nextRegexConcatenation(java.util.regex.Pattern... patterns)
patterns.length
Pattern
s, but only if they can all match one Pattern
each.
If it succeeds, true
is returned, false
otherwise.
If patterns
is null
, a NullPointerException
will be thrown.
patterns
- an array of Pattern
strue
if, and only if, the index at the end of the next consumption is advanced, false
otherwisejava.lang.NullPointerException
- thrown if, and only if, patterns
or any element within patterns
are null
public boolean nextRegexConcatenation(java.lang.String... regexes)
public boolean nextString(java.lang.String string)
public boolean nextStringAlternation(java.lang.String... strings)
public boolean nextStringConcatenation(java.lang.String... strings)
public boolean testNextCharacter()
public boolean testNextCharacter(char character)
public boolean testNextCharacterAlternation(char... characters)
public boolean testNextCharacterConcatenation(char... characters)
public boolean testNextRegex(java.util.regex.Pattern pattern)
public boolean testNextRegex(java.util.regex.Pattern pattern, java.util.function.Consumer<java.util.regex.MatchResult> matchResultConsumer)
public boolean testNextRegex(java.lang.String regex)
public boolean testNextRegexAlternation(java.util.regex.Pattern... patterns)
public boolean testNextRegexAlternation(java.lang.String... regexes)
public boolean testNextRegexConcatenation(java.util.regex.Pattern... patterns)
public boolean testNextRegexConcatenation(java.lang.String... regexes)
public boolean testNextString(java.lang.String string)
public boolean testNextStringAlternation(java.lang.String... strings)
public boolean testNextStringConcatenation(java.lang.String... strings)
public char peekCharacter()
public char peekCharacter(int relativeIndex)
public java.lang.String peekString(int length)
public java.lang.String peekString(int relativeIndex, int length)
public java.lang.String toString()
String
representation of this TextScanner
instance.toString
in class java.lang.Object
String
representation of this TextScanner
instancepublic java.lang.String toString(int absoluteIndex)
String
representation of the data at the given absolute index.
If absoluteIndex
is less than 0
, or greater than or equal to the capacity of the underlying Buffer
, an IllegalArgumentException
will be thrown.
toString
in class Scanner
absoluteIndex
- the absolute indexString
representation of the data at the given absolute indexjava.lang.IllegalArgumentException
- thrown if, and only if, absoluteIndex
is less than 0
, or greater than or equal to the capacity of the underlying Buffer
public void error()
ScannerException
to be thrown with an implementation-specific detail message.error
in class Scanner
ScannerException
- thrown if, and only if, this method is calledpublic void error(java.lang.String message)
ScannerException
to be thrown with the given detail message.
If message
is null
, a NullPointerException
will be thrown.
error
in class Scanner
message
- the detail message of the ScannerException
java.lang.NullPointerException
- thrown if, and only if, message
is null
ScannerException
- thrown if, and only if, this method is called and message
is not null
public void printRegexMatchingTimes()
public void setRecordingRegexMatchingTime(boolean isRecordingRegexMatchingTime)
public static TextScanner newInstance(java.io.File file)
TextScanner
instance given a File
.
If file
is null
, a NullPointerException
will be thrown.
If any IO-errors occur, an UncheckedIOException
will be thrown.
file
- the File
to readTextScanner
instance given a File
java.lang.NullPointerException
- thrown if, and only if, file
is null
java.io.UncheckedIOException
- thrown if, and only if, any IO-errors occurpublic static TextScanner newInstance(java.lang.String string)
TextScanner
instance given a String
.
If string
is null
, a NullPointerException
will be thrown.
string
- the String
to readTextScanner
instance given a String
java.lang.NullPointerException
- thrown if, and only if, string
is null