diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-26 22:55:16 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-26 22:55:16 -0400 |
| commit | 0220a17f18899715664812540949196e91ef8d10 (patch) | |
| tree | 0fc3d419665582a0bdc5d11c913e607fdeb9fbf8 /BJC-Utils2/src/main/java/bjc/utils/ioutils/PushbackBlockReader.java | |
| parent | 98cdf435d4974f4cca8f7b4eb4026da2c88cbc4c (diff) | |
Make BlockReader abstract, with varying impls.
This allows cool things to be accomplished through chaining BlockReaders
together without having to handle the block plumbing yourself.
The current set of implementations are a simple one that reads blocks
from a scanner delimited by a pattern, one that has a queue of blocks it
will attempt to pull from before reading, and one that triggers an
action before a block is read.
As an example use, for FDS, a combo of simple -> triggered -> pushback
is used where the triggered is used for prompting the user, and the
pushback supports macros.
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils/PushbackBlockReader.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/ioutils/PushbackBlockReader.java | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/PushbackBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/PushbackBlockReader.java new file mode 100644 index 0000000..d67e01a --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/PushbackBlockReader.java @@ -0,0 +1,98 @@ +package bjc.utils.ioutils; + +import java.io.IOException; +import java.util.Deque; +import java.util.LinkedList; + +/** + * A block reader that supports pushing blocks onto the input queue so that they + * are provided before blocks read from an input source. + * + * @author bjculkin + * + */ +public class PushbackBlockReader implements BlockReader { + private BlockReader source; + + private Deque<Block> waiting; + + private Block curBlock; + + private int blockNo; + + /** + * Create a new pushback block reader. + * + * @param src + * The block reader to use when no blocks are queued. + */ + public PushbackBlockReader(BlockReader src) { + source = src; + + waiting = new LinkedList<>(); + } + + @Override + public boolean hasNextBlock() { + return !waiting.isEmpty() || source.hasNextBlock(); + } + + @Override + public Block getBlock() { + return curBlock; + } + + @Override + public boolean nextBlock() { + if (!waiting.isEmpty()) { + curBlock = waiting.pop(); + + blockNo += 1; + + return true; + } else { + boolean succ = source.nextBlock(); + curBlock = source.getBlock(); + + if (succ) blockNo += 1; + + return succ; + } + } + + @Override + public int getBlockCount() { + return blockNo; + } + + @Override + public void close() throws IOException { + source.close(); + } + + /** + * Insert a block at the back of the queue of pending blocks. + * + * @param blk + * The block to put at the back. + */ + public void addBlock(Block blk) { + waiting.add(blk); + } + + /** + * Insert a block at the front of the queue of pending blocks. + * + * @param blk + * The block to put at the front. + */ + public void pushBlock(Block blk) { + waiting.push(blk); + } + + @Override + public String toString() { + return String.format("PushbackBlockReader [waiting=%s, curBlock=%s, blockNo=%s]", waiting, curBlock, + blockNo); + } +}
\ No newline at end of file |
