de.unkrig.commons.text.scanner
Class AbstractScanner<TT extends java.lang.Enum<TT>>

java.lang.Object
  extended by de.unkrig.commons.text.scanner.AbstractScanner<TT>
Type Parameters:
TT - Enumerates the scanner-specific token types
All Implemented Interfaces:
ProducerWhichThrows<AbstractScanner.Token<TT>,ScanException>, StringScanner<TT>
Direct Known Subclasses:
StatefulScanner, StatelessScanner

public abstract class AbstractScanner<TT extends java.lang.Enum<TT>>
extends java.lang.Object
implements StringScanner<TT>

A scanner that produces AbstractScanner.Tokens.


Nested Class Summary
static class AbstractScanner.Token<TT extends java.lang.Enum<TT>>
          Representation of a scanned token.
 
Field Summary
protected  java.lang.CharSequence cs
          The string currently being scanned; typically one line from an input document.
protected  int offset
          The position within cs of the next token to be scanned.
protected  int previousTokenOffset
          The position within cs of the previously scanned token.
 
Constructor Summary
AbstractScanner()
           
 
Method Summary
 int getOffset()
           
 int getPreviousTokenOffset()
           
abstract  AbstractScanner.Token<TT> produce()
          Before setInput(CharSequence) is called, this method returns null.
 AbstractScanner<TT> setInput(java.lang.CharSequence cs)
           
 java.lang.String toString()
          Returns a human-readable text which describes the origin of the previously produced element, e.g. file name, line number and column number
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

cs

protected java.lang.CharSequence cs
The string currently being scanned; typically one line from an input document.


offset

protected int offset
The position within cs of the next token to be scanned.


previousTokenOffset

protected int previousTokenOffset
The position within cs of the previously scanned token.

Constructor Detail

AbstractScanner

public AbstractScanner()
Method Detail

setInput

public AbstractScanner<TT> setInput(java.lang.CharSequence cs)
Specified by:
setInput in interface StringScanner<TT extends java.lang.Enum<TT>>
Parameters:
cs - The CharSequence from which following calls to ProducerWhichThrows.produce() will scan tokens
Returns:
This scanner

produce

@Nullable
public abstract AbstractScanner.Token<TT> produce()
                                                                      throws ScanException
Before setInput(CharSequence) is called, this method returns null. After setInput(CharSequence) was called, this method breaks the input char character sequence up into tokens and returns them one by one. When the input char sequence is exhausted, null is returned until setInput(CharSequence) is called again.

Specified by:
produce in interface ProducerWhichThrows<AbstractScanner.Token<TT extends java.lang.Enum<TT>>,ScanException>
Throws:
ScanException

getOffset

public int getOffset()
Specified by:
getOffset in interface StringScanner<TT extends java.lang.Enum<TT>>
Returns:
The offset within the input string set through StringScanner.setInput(CharSequence) where the previously scanned token ends, or 0 after the call to StringScanner.setInput(CharSequence) and before the first call to ProducerWhichThrows.produce()

getPreviousTokenOffset

public int getPreviousTokenOffset()
Specified by:
getPreviousTokenOffset in interface StringScanner<TT extends java.lang.Enum<TT>>
Returns:
The offset within the input string set through StringScanner.setInput(CharSequence) where the previously scanned token begins, or -1 after the call to StringScanner.setInput(CharSequence) and before the first call to ProducerWhichThrows.produce()

toString

public java.lang.String toString()
Description copied from interface: ProducerWhichThrows
Returns a human-readable text which describes the origin of the previously produced element, e.g. file name, line number and column number

Specified by:
toString in interface ProducerWhichThrows<AbstractScanner.Token<TT extends java.lang.Enum<TT>>,ScanException>
Overrides:
toString in class java.lang.Object