summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReader.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/BlockReader.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/BlockReader.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReader.java115
1 files changed, 17 insertions, 98 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReader.java
index fed74fe..f019e09 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReader.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReader.java
@@ -1,95 +1,37 @@
package bjc.utils.ioutils;
-import java.io.LineNumberReader;
-import java.io.Reader;
+import java.io.IOException;
import java.util.Iterator;
-import java.util.NoSuchElementException;
-import java.util.Scanner;
import java.util.function.Consumer;
-import java.util.regex.Pattern;
/**
- * Implements reading numbered blocks from a source.
+ * A source of blocks of characters, marked with line numbers as to block
+ * start/block end.
*
- * A block is a series of characters, separated by some regular expression.
- *
- * NOTE: The EOF marker is always treated as a delimiter. You are expected to
- * handle blocks that may be shorter than you expect.
- *
- * @author EVE
+ * @author bjculkin
*
*/
-public class BlockReader implements AutoCloseable, Iterator<Block> {
- /*
- * I/O source for blocks.
- */
- private LineNumberReader lnReader;
- private Scanner blockReader;
-
- /*
- * The current block.
- */
- private Block currBlock;
- private int blockNo;
-
- /**
- * Create a new block reader.
- *
- * @param blockDelim
- * The pattern that separates blocks. Note that the end
- * of file is always considered to end a block.
- *
- * @param source
- * The source to read blocks from.
- */
- public BlockReader(String blockDelim, Reader source) {
- lnReader = new LineNumberReader(source);
-
- blockReader = new Scanner(lnReader);
-
- String pattern = String.format("(?:%s)|\\Z", blockDelim);
- Pattern pt = Pattern.compile(pattern, Pattern.MULTILINE);
-
- blockReader.useDelimiter(pt);
- }
-
+public interface BlockReader extends AutoCloseable, Iterator<Block> {
/**
* Check if this reader has an available block.
*
* @return Whether or not another block is available.
*/
- public boolean hasNextBlock() {
- return blockReader.hasNext();
- }
+ boolean hasNextBlock();
/**
* Get the current block.
*
* @return The current block, or null if there is no current block.
*/
- public Block getBlock() {
- return currBlock;
- }
+ Block getBlock();
/**
* Move to the next block.
*
* @return Whether or not the next block was successfully read.
*/
- public boolean nextBlock() {
- try {
- int blockStartLine = lnReader.getLineNumber();
- String blockContents = blockReader.next();
- int blockEndLine = lnReader.getLineNumber();
- blockNo += 1;
-
- currBlock = new Block(blockNo, blockContents, blockStartLine, blockEndLine);
-
- return true;
- } catch (NoSuchElementException nseex) {
- return false;
- }
- }
+ boolean nextBlock();
/**
* Execute an action for each remaining block.
@@ -97,11 +39,9 @@ public class BlockReader implements AutoCloseable, Iterator<Block> {
* @param action
* The action to execute for each block
*/
- public void forEachBlock(Consumer<Block> action) {
- while (hasNextBlock()) {
- nextBlock();
-
- action.accept(currBlock);
+ default void forEachBlock(Consumer<Block> action) {
+ while (hasNext()) {
+ action.accept(next());
}
}
@@ -110,40 +50,19 @@ public class BlockReader implements AutoCloseable, Iterator<Block> {
*
* @return The number of blocks read so far.
*/
- public int getBlockCount() {
- return blockNo;
- }
-
- @Override
- public void close() throws Exception {
- blockReader.close();
-
- lnReader.close();
- }
-
- /**
- * Set the delimiter used to separate blocks.
- *
- * @param delim
- * The delimiter used to separate blocks.
- */
- public void setDelimiter(String delim) {
- blockReader.useDelimiter(delim);
- }
+ int getBlockCount();
- /*
- * Iterator implementation.
- */
+ void close() throws IOException;
@Override
- public boolean hasNext() {
- return blockReader.hasNext();
+ default boolean hasNext() {
+ return hasNextBlock();
}
@Override
- public Block next() {
+ default Block next() {
nextBlock();
return getBlock();
}
-}
+} \ No newline at end of file