Package repast.simphony.util
Class Scanner
- java.lang.Object
-
- repast.simphony.util.Scanner
-
- Direct Known Subclasses:
RandomAccessScanner
public class Scanner extends Object implements Iterator<String>
A class that will take in an a Readable object and will perform scanning functions on it. This provides the ability to grab a String of characters up to a delimiter, following a pattern, or a certain length.
This is somewhat a replacement for java's built inScanner
class, but isn't final and provides some extra methods, while excluding the next[data type] type methods.- Author:
- Jerry Vos
- See Also:
RandomAccessScanner
-
-
Constructor Summary
Constructors Constructor Description Scanner(Readable source)
Scanner(String source)
Scanner(ReadableByteChannel channel)
Scanner(ReadableByteChannel channel, String charset)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
buffer()
protected void
bufferMore()
Pattern
delimiter()
Returns the delimiter pattern used fornext()
andgetNextDelimited()
.String
getNextDelimited()
Fetches the next String of characters up to the current delimiter.String
getNextLength(int length)
Retrieves the next n characters from the stream, where n is the specified length.String
getNextPattern(String pattern)
Retrieves a sequence of characters that matches the specified pattern.protected void
handleIOException(IOException e)
boolean
hasNext()
Returns true if there are some characters left to be returned, otherwise false.IOException
ioException()
Returns the last IOException that occurred.String
next()
Same asgetNextDelimited()
.void
remove()
Currently unsupported.Pattern
useDelimiter(String delimiter)
Sets the delimiter pattern used fornext()
andgetNextDelimited()
.Pattern
useDelimiter(Pattern delimiterPattern)
Sets the delimiter pattern used fornext()
andgetNextDelimited()
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Iterator
forEachRemaining
-
-
-
-
Field Detail
-
BUF_SIZE
public static final int BUF_SIZE
- See Also:
- Constant Field Values
-
buf
protected CharBuffer buf
-
eof
protected boolean eof
-
source
protected Readable source
-
-
Constructor Detail
-
Scanner
public Scanner(String source)
-
Scanner
public Scanner(Readable source)
-
Scanner
public Scanner(ReadableByteChannel channel)
-
Scanner
public Scanner(ReadableByteChannel channel, String charset)
-
-
Method Detail
-
buffer
protected void buffer()
-
handleIOException
protected void handleIOException(IOException e)
-
bufferMore
protected void bufferMore()
-
hasNext
public boolean hasNext()
Returns true if there are some characters left to be returned, otherwise false.
-
next
public String next()
Same asgetNextDelimited()
.- Specified by:
next
in interfaceIterator<String>
- See Also:
getNextDelimited()
-
getNextDelimited
public String getNextDelimited()
Fetches the next String of characters up to the current delimiter. If the end of the buffer is reached while trying to find the delimiter, the String of characters from the current position to the end of the stream will be returned. If we're at the end of the stream a NoSuchElementException will be thrown.- Returns:
- the next String of characters up to the delimiter or end of the stream, whichever comes first
- See Also:
hasNext()
-
getNextPattern
public String getNextPattern(String pattern)
Retrieves a sequence of characters that matches the specified pattern. This sequence must begin with the next character in the stream. If the pattern isn't matched (or isn't matched including the first character) an InputMismatchException will be thrown.- Parameters:
pattern
- the pattern to fetch based off of- Returns:
- a String that matches the specified pattern
- See Also:
InputMismatchException
-
getNextLength
public String getNextLength(int length)
Retrieves the next n characters from the stream, where n is the specified length. If there are not enough characters left an exception will be thrown.- Parameters:
length
- how many characters to fetch- Returns:
- a string of the specified number of characters
- See Also:
BufferUnderflowException
-
useDelimiter
public Pattern useDelimiter(String delimiter)
Sets the delimiter pattern used fornext()
andgetNextDelimited()
. This is a regular expression and will be converted to a Pattern.- Returns:
- the previous delimiter pattern
- See Also:
Pattern
-
useDelimiter
public Pattern useDelimiter(Pattern delimiterPattern)
Sets the delimiter pattern used fornext()
andgetNextDelimited()
.- Returns:
- the previous delimiter pattern
-
delimiter
public Pattern delimiter()
Returns the delimiter pattern used fornext()
andgetNextDelimited()
.- Returns:
- the current delimiter pattern
- See Also:
useDelimiter(Pattern)
,useDelimiter(String)
-
remove
public void remove()
Currently unsupported. Throws anUnsupportedOperationException
.
-
ioException
public IOException ioException()
Returns the last IOException that occurred.- Returns:
- the last IOException that occurred
-
-