From 0220a17f18899715664812540949196e91ef8d10 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 26 Mar 2017 22:55:16 -0400 Subject: 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. --- .../bjc/utils/ioutils/TriggeredBlockReader.java | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/ioutils/TriggeredBlockReader.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils/TriggeredBlockReader.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/TriggeredBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/TriggeredBlockReader.java new file mode 100644 index 0000000..dd89223 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/TriggeredBlockReader.java @@ -0,0 +1,62 @@ +package bjc.utils.ioutils; + +import java.io.IOException; + +/** + * A block reader that fires an action before a block is actually read. + * + * @author bjculkin + * + */ +public class TriggeredBlockReader implements BlockReader { + private BlockReader source; + + private Runnable action; + + /** + * Create a new triggered reader with the specified source/action. + * + * @param source + * The block reader to read blocks from. + * + * @param action + * The action to execute before reading a block. + */ + public TriggeredBlockReader(BlockReader source, Runnable action) { + super(); + this.source = source; + this.action = action; + } + + @Override + public boolean hasNextBlock() { + action.run(); + + return source.hasNextBlock(); + } + + @Override + public Block getBlock() { + return source.getBlock(); + } + + @Override + public boolean nextBlock() { + return source.nextBlock(); + } + + @Override + public int getBlockCount() { + return source.getBlockCount(); + } + + @Override + public void close() throws IOException { + source.close(); + } + + @Override + public String toString() { + return String.format("TriggeredBlockReader [source=%s]", source); + } +} \ No newline at end of file -- cgit v1.2.3