diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:40:41 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:40:41 -0400 |
| commit | d4ca769e542b2489b1e23cfcbdc3a0b7275b87cd (patch) | |
| tree | 1653a7399f97fb0c63ce62e3f60fd830d5c37f70 /base/src/main/java/bjc/utils/ioutils/blocks | |
| parent | 2ac2e31a56ae59ee582e43a90c3495f86dd9ee7a (diff) | |
Cleanup pass
Cleanup pass to uniformize things
Diffstat (limited to 'base/src/main/java/bjc/utils/ioutils/blocks')
13 files changed, 123 insertions, 111 deletions
diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/Block.java b/base/src/main/java/bjc/utils/ioutils/blocks/Block.java index bf0257e..b8c611b 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/Block.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/Block.java @@ -29,10 +29,10 @@ public class Block { /** * The line offset number for this block. - * - * Essentially, this is the absolute number of lines that this block is - * into whatever source it came from, where as startLine and endLine are - * relative to the BlockReader this Block is from. + * + * Essentially, this is the absolute number of lines that this block is into + * whatever source it came from, where as startLine and endLine are relative to + * the BlockReader this Block is from. */ public int lineOffset; @@ -40,15 +40,16 @@ public class Block { * Create a new block. * * @param blockNo - * The number of this block. + * The number of this block. * @param contents - * The contents of this block. + * The contents of this block. * @param startLine - * The line this block started on. + * The line this block started on. * @param endLine - * The line this block ended. + * The line this block ended. */ - public Block(final int blockNo, final String contents, final int startLine, final int endLine) { + public Block(final int blockNo, final String contents, final int startLine, + final int endLine) { this.contents = contents; this.startLine = startLine; this.endLine = endLine; @@ -58,12 +59,13 @@ public class Block { @Override public String toString() { if (lineOffset != -1) { - String fmt = "Block #%d (from lines %d (%d) to %d (%d)), length: %d characters"; + String fmt + = "Block #%d (from lines %d (%d) to %d (%d)), length: %d characters"; - return String.format(fmt, blockNo, startLine + lineOffset, startLine, endLine + lineOffset, - endLine + lineOffset, contents.length()); + return String.format(fmt, blockNo, startLine + lineOffset, startLine, + endLine + lineOffset, endLine + lineOffset, contents.length()); } - + String fmt = "Block #%d (from lines %d to %d), length: %d characters"; return String.format(fmt, blockNo, startLine, endLine, contents.length()); diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java index c6d709c..f111244 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java @@ -51,10 +51,10 @@ public interface BlockReader extends AutoCloseable, Iterator<Block>, Iterable<Bl * Execute an action for each remaining block. * * @param action - * The action to execute for each block + * The action to execute for each block */ default void forEachBlock(final Consumer<Block> action) { - while(hasNext()) { + while (hasNext()) { action.accept(next()); } } diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java b/base/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java index f1dfc3c..615af85 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java @@ -13,10 +13,10 @@ public class BlockReaders { * Create a new simple block reader that works off a regex. * * @param blockDelim - * The regex that separates blocks. + * The regex that separates blocks. * * @param source - * The reader to get blocks from. + * The reader to get blocks from. * * @return A configured simple reader. */ @@ -28,7 +28,7 @@ public class BlockReaders { * Create a new pushback block reader. * * @param src - * The block reader to read blocks from. + * The block reader to read blocks from. * * @return A configured pushback reader. */ @@ -40,10 +40,10 @@ public class BlockReaders { * Create a new triggered block reader. * * @param source - * The block reader to read blocks from. + * The block reader to read blocks from. * * @param action - * The action to execute before reading a block. + * The action to execute before reading a block. * * @return A configured triggered block reader. */ @@ -55,14 +55,15 @@ public class BlockReaders { * Create a new layered block reader. * * @param primary - * The first source to read blocks from. + * The first source to read blocks from. * * @param secondary - * The second source to read blocks from. + * The second source to read blocks from. * * @return A configured layered block reader. */ - public static BlockReader layered(final BlockReader primary, final BlockReader secondary) { + public static BlockReader layered(final BlockReader primary, + final BlockReader secondary) { return new LayeredBlockReader(primary, secondary); } @@ -70,7 +71,7 @@ public class BlockReaders { * Create a new serial block reader. * * @param readers - * The readers to pull from, in the order to pull from them. + * The readers to pull from, in the order to pull from them. * * @return A configured serial block reader. */ diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/BoundBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/BoundBlockReader.java index ba2c7ab..0bd0991 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/BoundBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/BoundBlockReader.java @@ -6,14 +6,14 @@ import java.util.function.Supplier; /** * A block reader composed from functions. - * + * * @author EVE * */ public class BoundBlockReader implements BlockReader { /** * A function that serves to close an I/O source. - * + * * @author EVE * */ @@ -21,9 +21,9 @@ public class BoundBlockReader implements BlockReader { public interface Closer { /** * Close the I/O source this is attached to. - * + * * @throws IOException - * If something goes wrong + * If something goes wrong */ public void close() throws IOException; } @@ -38,15 +38,16 @@ public class BoundBlockReader implements BlockReader { /** * Create a new bound block reader. - * + * * @param blockChecker - * Predicate for checking if a block is available + * Predicate for checking if a block is available * @param blockGetter - * Function to retrieve a block + * Function to retrieve a block * @param blockCloser - * Function to close a block source + * Function to close a block source */ - public BoundBlockReader(BooleanSupplier blockChecker, Supplier<Block> blockGetter, Closer blockCloser) { + public BoundBlockReader(BooleanSupplier blockChecker, Supplier<Block> blockGetter, + Closer blockCloser) { checker = blockChecker; getter = blockGetter; closer = blockCloser; @@ -66,7 +67,7 @@ public class BoundBlockReader implements BlockReader { @Override public boolean nextBlock() { - if(checker.getAsBoolean()) { + if (checker.getAsBoolean()) { current = getter.get(); blockNo += 1; diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/FilteredBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/FilteredBlockReader.java index 7a6eddc..5520f1f 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/FilteredBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/FilteredBlockReader.java @@ -6,7 +6,7 @@ import java.util.function.Predicate; /** * A block reader that only yields blocks that pass a predicate. - * + * * @author EVE * */ @@ -41,28 +41,29 @@ public class FilteredBlockReader implements BlockReader { /** * Create a new filtered block reader with a given filter. - * + * * @param src - * The place to read blocks from. + * The place to read blocks from. * @param predic - * The predicate to use to pass blocks. + * The predicate to use to pass blocks. */ public FilteredBlockReader(BlockReader src, Predicate<Block> predic) { this(src, predic, null); } /** - * Create a new filtered block reader with a given filter that executes - * a specific action when a block fails. - * + * Create a new filtered block reader with a given filter that executes a + * specific action when a block fails. + * * @param src - * The place to read blocks from. + * The place to read blocks from. * @param predic - * The predicate to use to pass blocks. + * The predicate to use to pass blocks. * @param failAct - * The action to take when a block fails. + * The action to take when a block fails. */ - public FilteredBlockReader(BlockReader src, Predicate<Block> predic, Consumer<Block> failAct) { + public FilteredBlockReader(BlockReader src, Predicate<Block> predic, + Consumer<Block> failAct) { source = src; pred = predic; failAction = failAct; @@ -72,16 +73,16 @@ public class FilteredBlockReader implements BlockReader { @Override public boolean hasNextBlock() { - if(pending != null) return true; + if (pending != null) + return true; - while(source.hasNextBlock()) { + while (source.hasNextBlock()) { /* - * Only say we have a next block if the next block would - * pass the predicate. + * Only say we have a next block if the next block would pass the predicate. */ pending = source.next(); - if(pred.test(pending)) { + if (pred.test(pending)) { blockNo += 1; return true; } @@ -99,7 +100,7 @@ public class FilteredBlockReader implements BlockReader { @Override public boolean nextBlock() { - if(pending != null || hasNextBlock()) { + if (pending != null || hasNextBlock()) { current = pending; pending = null; diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java index 9c1bcd5..e08d360 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java @@ -36,11 +36,11 @@ public class FlatMappedBlockReader implements BlockReader { /** * Create a new flat-mapping block reader. - * + * * @param source - * The source to read blocks from + * The source to read blocks from * @param trans - * The transform to use. + * The transform to use. */ public FlatMappedBlockReader(BlockReader source, Function<Block, List<Block>> trans) { reader = source; @@ -62,11 +62,11 @@ public class FlatMappedBlockReader implements BlockReader { @Override public boolean nextBlock() { /* - * Attempt to get a new pending list if the one we have isn't - * valid. + * 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; + while (pending == null || !pending.hasNext()) { + if (!reader.hasNext()) + return false; pending = transform.apply(reader.next()).iterator(); } diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/LayeredBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/LayeredBlockReader.java index 48c4963..847e298 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/LayeredBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/LayeredBlockReader.java @@ -29,10 +29,10 @@ public class LayeredBlockReader implements BlockReader { * Create a new layered block reader. * * @param primary - * The first source to read blocks from. + * The first source to read blocks from. * * @param secondary - * The second source to read blocks from. + * The second source to read blocks from. */ public LayeredBlockReader(final BlockReader primary, final BlockReader secondary) { first = primary; @@ -49,8 +49,8 @@ public class LayeredBlockReader implements BlockReader { final Block firstBlock = first.getBlock(); /* - * Only drain a block from the second reader if none are - * available in the first reader. + * Only drain a block from the second reader if none are available in the first + * reader. */ return firstBlock == null ? second.getBlock() : firstBlock; } @@ -60,7 +60,7 @@ public class LayeredBlockReader implements BlockReader { final boolean gotFirst = first.nextBlock(); final boolean succ = gotFirst ? gotFirst : second.nextBlock(); - if(succ) { + if (succ) { blockNo += 1; } diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java index 4439da2..8b3e0c5 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java @@ -5,7 +5,7 @@ import java.util.function.UnaryOperator; /** * A block reader that applies a transform to each block. - * + * * @author EVE * */ @@ -20,11 +20,11 @@ public class MappedBlockReader implements BlockReader { /** * Create a new mapped block reader. - * + * * @param source - * The source for blocks + * The source for blocks * @param trans - * The transform to apply. + * The transform to apply. */ public MappedBlockReader(BlockReader source, UnaryOperator<Block> trans) { reader = source; @@ -45,7 +45,7 @@ public class MappedBlockReader implements BlockReader { @Override public boolean nextBlock() { - if(hasNextBlock()) { + if (hasNextBlock()) { current = transform.apply(reader.next()); blockNo += 1; diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java index 924df39..cdd6760 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java @@ -27,7 +27,7 @@ public class PushbackBlockReader implements BlockReader { * Create a new pushback block reader. * * @param src - * The block reader to use when no blocks are queued. + * The block reader to use when no blocks are queued. */ public PushbackBlockReader(final BlockReader src) { source = src; @@ -50,7 +50,7 @@ public class PushbackBlockReader implements BlockReader { /* * Drain pushed-back blocks first. */ - if(!waiting.isEmpty()) { + if (!waiting.isEmpty()) { curBlock = waiting.pop(); blockNo += 1; @@ -61,7 +61,7 @@ public class PushbackBlockReader implements BlockReader { final boolean succ = source.nextBlock(); curBlock = source.getBlock(); - if(succ) { + if (succ) { blockNo += 1; } @@ -82,7 +82,7 @@ public class PushbackBlockReader implements BlockReader { * Insert a block at the back of the queue of pending blocks. * * @param blk - * The block to put at the back. + * The block to put at the back. */ public void addBlock(final Block blk) { waiting.add(blk); @@ -92,7 +92,7 @@ public class PushbackBlockReader implements BlockReader { * Insert a block at the front of the queue of pending blocks. * * @param blk - * The block to put at the front. + * The block to put at the front. */ public void pushBlock(final Block blk) { waiting.push(blk); @@ -100,7 +100,7 @@ public class PushbackBlockReader implements BlockReader { @Override public String toString() { - return String.format("PushbackBlockReader [waiting=%s, curBlock=%s, blockNo=%s]", waiting, curBlock, - blockNo); + return String.format("PushbackBlockReader [waiting=%s, curBlock=%s, blockNo=%s]", + waiting, curBlock, blockNo); } } diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java index 66ebd66..265a781 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java @@ -19,19 +19,20 @@ public class SerialBlockReader implements BlockReader { * Create a new serial block reader. * * @param readers - * The readers to pull from, in the order to pull from them. + * The readers to pull from, in the order to pull from them. */ public SerialBlockReader(final BlockReader... readers) { readerQueue = new LinkedList<>(); - - for(final BlockReader reader : readers) { + + for (final BlockReader reader : readers) { readerQueue.add(reader); } } @Override public boolean hasNextBlock() { - if(readerQueue.isEmpty()) return false; + if (readerQueue.isEmpty()) + return false; /* * Attempt to get a block from the first reader. @@ -42,11 +43,12 @@ public class SerialBlockReader implements BlockReader { /* * Close/dispose of readers until we get an open one. */ - while(!cont) { + while (!cont) { try { readerQueue.pop().close(); - } catch(final IOException ioex) { - throw new IllegalStateException("Exception thrown by discarded reader", ioex); + } catch (final IOException ioex) { + throw new IllegalStateException("Exception thrown by discarded reader", + ioex); } hasBlock = readerQueue.peek().hasNextBlock(); @@ -58,7 +60,7 @@ public class SerialBlockReader implements BlockReader { @Override public Block getBlock() { - if(readerQueue.isEmpty()) { + if (readerQueue.isEmpty()) { return null; } @@ -67,23 +69,25 @@ public class SerialBlockReader implements BlockReader { @Override public boolean nextBlock() { - if(readerQueue.isEmpty()) return false; + if (readerQueue.isEmpty()) + return false; boolean gotBlock = readerQueue.peek().nextBlock(); boolean cont = gotBlock || readerQueue.isEmpty(); - while(!cont) { + while (!cont) { try { readerQueue.pop().close(); - } catch(final IOException ioex) { - throw new IllegalStateException("Exception thrown by discarded reader", ioex); + } catch (final IOException ioex) { + throw new IllegalStateException("Exception thrown by discarded reader", + ioex); } gotBlock = readerQueue.peek().nextBlock(); cont = gotBlock || readerQueue.isEmpty(); } - if(cont) { + if (cont) { blockNo += 1; } @@ -97,7 +101,7 @@ public class SerialBlockReader implements BlockReader { @Override public void close() throws IOException { - while(!readerQueue.isEmpty()) { + while (!readerQueue.isEmpty()) { final BlockReader reader = readerQueue.pop(); reader.close(); diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java index ac77f97..94405c9 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java @@ -38,11 +38,11 @@ public class SimpleBlockReader implements BlockReader { * 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. + * 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. + * The source to read blocks from. */ public SimpleBlockReader(final String blockDelim, final Reader source) { blockReader = new Scanner(source); @@ -59,12 +59,12 @@ public class SimpleBlockReader implements BlockReader { * 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. + * 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. - * NOTE: This does modify the provided scanner. + * The source to read blocks from. NOTE: This does modify the + * provided scanner. */ public SimpleBlockReader(final String blockDelim, final Scanner source) { blockReader = source; @@ -96,7 +96,8 @@ public class SimpleBlockReader implements BlockReader { final String blockContents = blockReader.next(); final int blockStartLine = lineNo; - final int blockEndLine = lineNo + StringUtils.countMatches(blockContents, "\\R"); + final int blockEndLine + = lineNo + StringUtils.countMatches(blockContents, "\\R"); lineNo = blockEndLine; blockNo += 1; @@ -104,10 +105,10 @@ public class SimpleBlockReader implements BlockReader { currBlock = new Block(blockNo, blockContents, blockStartLine, blockEndLine); return true; - } catch(final NoSuchElementException nseex) { + } catch (final NoSuchElementException nseex) { // Don't null out the current block, let it be the last // one - //currBlock = null; + // currBlock = null; return false; } @@ -127,7 +128,7 @@ public class SimpleBlockReader implements BlockReader { * Set the delimiter used to separate blocks. * * @param delim - * The delimiter used to separate blocks. + * The delimiter used to separate blocks. */ public void setDelimiter(final String delim) { blockReader.useDelimiter(delim); @@ -135,6 +136,7 @@ public class SimpleBlockReader implements BlockReader { @Override public String toString() { - return String.format("SimpleBlockReader [currBlock=%s, blockNo=%s]", currBlock, blockNo); + return String.format("SimpleBlockReader [currBlock=%s, blockNo=%s]", currBlock, + blockNo); } } diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/ToggledBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/ToggledBlockReader.java index 456a445..12568c8 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/ToggledBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/ToggledBlockReader.java @@ -6,7 +6,7 @@ import bjc.data.BooleanToggle; /** * A block reader that toggles between two sources. - * + * * @author EVE * */ @@ -21,11 +21,11 @@ public class ToggledBlockReader implements BlockReader { /** * Create a new toggling block reader. - * + * * @param left - * The first block reader to use. + * The first block reader to use. * @param right - * The second block reader to use. + * The second block reader to use. */ public ToggledBlockReader(BlockReader left, BlockReader right) { leftSource = left; @@ -38,7 +38,7 @@ public class ToggledBlockReader implements BlockReader { @Override public boolean hasNextBlock() { - if(leftToggle.peek()) { + if (leftToggle.peek()) { return leftSource.hasNextBlock(); } @@ -47,7 +47,7 @@ public class ToggledBlockReader implements BlockReader { @Override public Block getBlock() { - if(leftToggle.peek()) { + if (leftToggle.peek()) { return leftSource.getBlock(); } @@ -58,13 +58,14 @@ public class ToggledBlockReader implements BlockReader { public boolean nextBlock() { boolean succ; - if(leftToggle.get()) { + if (leftToggle.get()) { succ = leftSource.nextBlock(); } else { succ = rightSource.nextBlock(); } - if(succ) blockNo += 1; + if (succ) + blockNo += 1; return succ; } diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/TriggeredBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/TriggeredBlockReader.java index a066f9c..b4accec 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/TriggeredBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/TriggeredBlockReader.java @@ -22,10 +22,10 @@ public class TriggeredBlockReader implements BlockReader { * Create a new triggered reader with the specified source/action. * * @param source - * The block reader to read blocks from. + * The block reader to read blocks from. * * @param action - * The action to execute before reading a block. + * The action to execute before reading a block. */ public TriggeredBlockReader(final BlockReader source, final Runnable action) { this.source = source; |
