public abstract class Scanner
extends java.lang.Object
It makes no assumptions about the data to scan. That's entirely up to the current implementation at hand.
All Scanner
s are built around a fixed length DataBuffer
and two indices. The indices being the indices at the beginning and end of the next consumption. The index at the beginning of the next consumption is inclusive, and the index at
the end of the next consumption is exclusive. A consumption essentially sets the value of the index at the beginning of the next consumption to the value of the index at the end of the next consumption.
In order to consume data, you need something to consume. That's where all methods starting with next(...)
comes in. The simplest of them is the next()
method of this Scanner
class. All the others are specific for a given
implementation. All of them returns true
on success and false
otherwise. This means that if you succeed, you have something to consume, and so, calling any of the consume(...)
methods should work.
The skip()
method essentially advances the index at the beginning of the next consumption and, if necessary, also the index at the end of the next consumption.
The consumption()
method returns a String
with the current consumption, without consuming it.
The mark()
method returns a key that can be used to access the current state again in the future. To access the current state again, simply provide the key to the rewind(int)
method. And, to remove the key without rewinding, you can
call the unmark(int)
method with the key. A key works only once though. The state represents the two indices only.
The error(...)
methods can be used to report errors in the syntax, or anything else for that matter.
Modifier | Constructor and Description |
---|---|
protected |
Scanner(DataBuffer dataBuffer)
Constructs a new
Scanner instance with a given DataBuffer . |
Modifier and Type | Method and Description |
---|---|
boolean |
consume()
A call to this method will attempt to consume the data between the two indices.
|
boolean |
consume(java.lang.StringBuilder stringBuilder)
A call to this method will attempt to consume the data between the two indices, while appending it to
stringBuilder
If the data is successfully consumed, true will be returned, false otherwise. |
java.lang.String |
consumption()
Returns a
String representation of the data that can currently be consumed. |
abstract void |
error()
Causes a
ScannerException to be thrown with an implementation-specific detail message. |
abstract void |
error(java.lang.String message)
Causes a
ScannerException to be thrown with the given detail message. |
int |
getIndexAtBeginningOfConsumption()
Returns the index at the beginning of the next consumption.
|
int |
getIndexAtEndOfConsumption()
Returns the index at the end of the next consumption.
|
int |
mark()
Marks the current state of this
Scanner and returns a key to that mark, such that you can rewind to it at a later time. |
abstract boolean |
next()
A simple method for advancing the index at the end of the next consumption.
|
boolean |
rewind(int key)
Rewinds the current state of this
Scanner to some previously marked state. |
void |
setIndexAtBeginningOfConsumption(int indexAtBeginningOfConsumption)
Sets the index at the beginning of the next consumption.
|
void |
setIndexAtEndOfConsumption(int indexAtEndOfConsumption)
Sets the index at the end of the next consumption.
|
boolean |
skip()
Skips the current data by advancing the index at the beginning of the next consumption with one.
|
boolean |
testNext()
A simple method for testing that
next() can be called. |
abstract java.lang.String |
toString(int absoluteIndex)
Returns a
String representation of the data at the given absolute index. |
boolean |
unmark(int key)
Unmarks a previous mark given its key.
|
protected Scanner(DataBuffer dataBuffer)
Scanner
instance with a given DataBuffer
.
If dataBuffer
is null
, a NullPointerException
will be thrown.
dataBuffer
- the DataBuffer
used by this Scanner
instancejava.lang.NullPointerException
- thrown if, and only if, dataBuffer
is null
public final boolean consume()
If the data is successfully consumed, true
will be returned, false
otherwise.
A consumption essentially sets the value of the index at the beginning of the next consumption to the value of the index at the end of the next consumption.
true
if, and only if, the data is successfully consumedpublic final boolean consume(java.lang.StringBuilder stringBuilder)
stringBuilder
If the data is successfully consumed, true
will be returned, false
otherwise.
A consumption essentially sets the value of the index at the beginning of the next consumption to the value of the index at the end of the next consumption.
If stringBuilder
is null
, a NullPointerException
will be thrown.
stringBuilder
- the StringBuilder
to append the data totrue
if, and only if, the data is successfully consumedjava.lang.NullPointerException
- thrown if, and only if, stringBuilder
is null
public abstract boolean next()
If it succeeds, true
is returned, false
otherwise.
The data that can be consumed by a successful call to this method is implementation-specific.
true
if, and only if, the index at the end of the next consumption is advanced, false
otherwisepublic final boolean rewind(int key)
Scanner
to some previously marked state.
If no state is associated with key
, nothing will happen.
If this Scanner
successfully rewinds to the previous state, true
will be returned, false
otherwise.
Note that a key can only be used once. In order to rewind again, you need to call the mark()
method again after this method has been called.
key
- the key of the state to rewind totrue
if, and only if, this Scanner
successfully rewinded to the state given the key, false
otherwisepublic final boolean skip()
The index at the end of the next consumption may be advanced, but only if it would be less than the new index at the beginning of the next consumption.
If data has been successfully skipped, true
will be returned, false
otherwise.
true
if, and only if, data has been successfully skipped, false
otherwisepublic final boolean testNext()
next()
can be called.
If it succeeds, true
is returned, false
otherwise.
No data can be consumed after a call to this method, as it will be rewinded.
true
if, and only if, the test that next()
can be called, false
otherwisepublic final boolean unmark(int key)
If the key does not exist, nothing will happen.
If the mark is successfully unmarked, true
will be returned, false
otherwise.
key
- the key to the marktrue
if, and only if, the mark was successfully unmarkedpublic final int getIndexAtBeginningOfConsumption()
public final int getIndexAtEndOfConsumption()
public final int mark()
Scanner
and returns a key to that mark, such that you can rewind to it at a later time.public final java.lang.String consumption()
String
representation of the data that can currently be consumed.
Calling this method will not consume the data.
String
representation of the data that can currently be consumedpublic abstract 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.
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 abstract void error()
ScannerException
to be thrown with an implementation-specific detail message.ScannerException
- thrown if, and only if, this method is calledpublic abstract void error(java.lang.String message)
ScannerException
to be thrown with the given detail message.
If message
is null
, a NullPointerException
will be thrown.
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 final void setIndexAtBeginningOfConsumption(int indexAtBeginningOfConsumption)
If indexAtBeginningOfConsumption
is less than 0
, an IllegalArgumentException
will be thrown.
indexAtBeginningOfConsumption
- the new index at the beginning of the next consumptionjava.lang.IllegalArgumentException
- thrown if, and only if, indexAtBeginningOfConsumption
is less than 0
public final void setIndexAtEndOfConsumption(int indexAtEndOfConsumption)
If indexAtEndOfConsumption
is less than getIndexAtBeginningOfConsumption()
, an IllegalArgumentException
will be thrown.
indexAtEndOfConsumption
- the new index at the end of the next consumptionjava.lang.IllegalArgumentException
- thrown if, and only if, indexAtEndOfConsumption
is less than getIndexAtBeginningOfConsumption()