Package net.loomchild.segment.srx
Class TextManager
- java.lang.Object
-
- net.loomchild.segment.srx.TextManager
-
public class TextManager extends java.lang.Object
Represents text manager. Responsible for managing current text, reading more text from the reader and checking if there is more text left.
-
-
Field Summary
Fields Modifier and Type Field Description private int
bufferLength
(package private) int
nextCharacter
private java.io.Reader
reader
private java.lang.CharSequence
text
-
Constructor Summary
Constructors Constructor Description TextManager(java.io.Reader reader, int bufferLength)
Creates text manager reading text from given reader.TextManager(java.lang.CharSequence text)
Creates text manager containing given text.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getBufferLength()
java.lang.CharSequence
getText()
boolean
hasMoreText()
private void
initText()
Reads initial text from reader if it has not been initialized yet.private java.lang.String
read(int amount)
Reads the given amount of characters and returns them as a string.private int
read(java.io.Reader reader, char[] buffer)
Reads specified amount of characters.void
readText(int amount)
Deletes given amount of characters from current character buffer and tries to read up to given amount of new characters and stores them in current character buffer.
-
-
-
Constructor Detail
-
TextManager
public TextManager(java.lang.CharSequence text)
Creates text manager containing given text. Reading more text is not possible when using this constructor.- Parameters:
text
-
-
TextManager
public TextManager(java.io.Reader reader, int bufferLength)
Creates text manager reading text from given reader. Only specified amount of memory for buffer will be used. Managed text will never be longer than given buffer size. Text is not actually read until required (lazy initialization).- Parameters:
reader
-bufferLength
- read buffer size
-
-
Method Detail
-
getBufferLength
public int getBufferLength()
-
getText
public java.lang.CharSequence getText()
- Returns:
- current text
-
hasMoreText
public boolean hasMoreText()
- Returns:
- true if more text can be read
-
readText
public void readText(int amount)
Deletes given amount of characters from current character buffer and tries to read up to given amount of new characters and stores them in current character buffer.- Parameters:
amount
- amount of characters to read- Throws:
java.lang.IllegalArgumentException
- ifhasMoreText()
returns false or amount is greater than buffer size
-
initText
private void initText()
Reads initial text from reader if it has not been initialized yet.
-
read
private java.lang.String read(int amount)
Reads the given amount of characters and returns them as a string. UpdatesnextCharacter
by reading one additional character.- Parameters:
amount
- amount to be read- Returns:
- read characters as a string
-
read
private int read(java.io.Reader reader, char[] buffer)
Reads specified amount of characters. It is needed because when reading from consoleReader.read(char[])
it returns after first end of line (probably it checks if characters are available).- Parameters:
reader
- inputbuffer
- buffer where read characters will be stored- Returns:
- number of read characters
-
-