diff options
Diffstat (limited to 'BJC-Utils2')
3 files changed, 104 insertions, 8 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java b/BJC-Utils2/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java index 115f782..5a87a9a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java @@ -46,7 +46,7 @@ public class BlockReaderCLI { BlockReaderCLI reader = new BlockReaderCLI(sources); - reader.run(new Scanner(System.in), "console"); + reader.run(new Scanner(System.in), "console", true); } /** @@ -56,11 +56,15 @@ public class BlockReaderCLI { * The place to read input from. * @param ioSource * The name of the place to read input from. + * @param interactive + * Whether or not the source is interactive */ - public void run(Scanner input, String ioSource) { + public void run(Scanner input, String ioSource, boolean interactive) { int lno = 0; while(input.hasNextLine()) { - System.out.printf("reader-conf(%d)>", lno); + if(interactive) + System.out.printf("reader-conf(%d)>", lno); + String ln = input.nextLine(); lno += 1; @@ -68,7 +72,7 @@ public class BlockReaderCLI { Command com = Command.fromString(ln, lno, ioSource); if(com == null) continue; - handleCommand(com); + handleCommand(com, true); } input.close(); @@ -77,7 +81,7 @@ public class BlockReaderCLI { /* * Handle a command. */ - public void handleCommand(Command com) { + public void handleCommand(Command com, boolean interactive) { switch(com.nameCommand) { case "def-filtered": defFiltered(com); @@ -96,8 +100,9 @@ public class BlockReaderCLI { break; case "exit": case "quit": - System.out.printf("Exiting reader-conf, %d readers configured in %d commands\n", - readers.size(), com.lineNo); + if(interactive) + System.out.printf("Exiting reader-conf, %d readers configured in %d commands\n", + readers.size(), com.lineNo); break; default: System.err.print(com.error("Unknown command '%s'\n", com.nameCommand)); diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java new file mode 100644 index 0000000..f4d8439 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java @@ -0,0 +1,86 @@ +package bjc.utils.ioutils.blocks; + +import java.io.IOException; + +import java.util.Iterator; +import java.util.List; +import java.util.function.Function; +import java.util.function.UnaryOperator; + +/** + * A block reader that supports applying a flatmap operation to blocks. + * + * The use-case in mind for this was tokenizing blocks. + * + * @author Benjamin Culkin + */ +public class FlatMappedBlockReader implements BlockReader { + /* + * The source reader. + */ + private BlockReader reader; + + /* + * The current block, and any blocks pending from the last source block. + */ + private Iterator<Block> pending; + private Block current; + + /* + * The operator to open blocks with. + */ + private Function<Block, List<Block>> transform; + + /* + * The current block number. + */ + private int blockNo; + + public FlatMappedBlockReader(BlockReader source, Function<Block, List<Block>> trans) { + reader = source; + transform = trans; + + blockNo = 0; + } + + @Override + public boolean hasNextBlock() { + return pending.hasNext() || reader.hasNextBlock(); + } + + @Override + public Block getBlock() { + return current; + } + + @Override + public boolean nextBlock() { + /* + * Attempt to get a new pending list if the one we have isn't + * valid. + */ + while(pending == null || !pending.hasNext()) { + if(!reader.hasNext()) return false; + + pending = transform.apply(reader.next()).iterator(); + } + + /* + * Advance the iterator. + */ + current = pending.next(); + blockNo += 1; + + return true; + } + + @Override + public int getBlockCount() { + return blockNo; + } + + @Override + public void close() throws IOException { + reader.close(); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java index 1996421..a9cfa57 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java @@ -29,7 +29,7 @@ public class MappedBlockReader implements BlockReader { @Override public boolean nextBlock() { if(hasNextBlock()) { - current = trans.apply(reader.next()); + current = transform.apply(reader.next()); return true; } @@ -41,4 +41,9 @@ public class MappedBlockReader implements BlockReader { public int getBlockCount() { return reader.getBlockCount(); } + + @Override + public void close() throws IOException { + reader.close(); + } } |
