summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/ioutils/TriggeredBlockReader.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-03-26 22:55:16 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-03-26 22:55:16 -0400
commit0220a17f18899715664812540949196e91ef8d10 (patch)
tree0fc3d419665582a0bdc5d11c913e607fdeb9fbf8 /BJC-Utils2/src/main/java/bjc/utils/ioutils/TriggeredBlockReader.java
parent98cdf435d4974f4cca8f7b4eb4026da2c88cbc4c (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/TriggeredBlockReader.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/TriggeredBlockReader.java62
1 files changed, 62 insertions, 0 deletions
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