summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/Block.java30
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java15
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java36
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/LayeredBlockReader.java24
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java24
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java18
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java30
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/TriggeredBlockReader.java12
8 files changed, 98 insertions, 91 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/Block.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/Block.java
index e92644e..b514d17 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/Block.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/Block.java
@@ -2,7 +2,7 @@ package bjc.utils.ioutils.blocks;
/**
* Represents a block of text read in from a source.
- *
+ *
* @author EVE
*
*/
@@ -29,7 +29,7 @@ public class Block {
/**
* Create a new block.
- *
+ *
* @param blockNo
* The number of this block.
* @param contents
@@ -39,7 +39,7 @@ public class Block {
* @param endLine
* The line this block ended.
*/
- public Block(int blockNo, String contents, int startLine, 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;
@@ -52,7 +52,7 @@ public class Block {
int result = 1;
result = prime * result + blockNo;
- result = prime * result + ((contents == null) ? 0 : contents.hashCode());
+ result = prime * result + (contents == null ? 0 : contents.hashCode());
result = prime * result + endLine;
result = prime * result + startLine;
@@ -60,21 +60,21 @@ public class Block {
}
@Override
- public boolean equals(Object obj) {
- if(this == obj) return true;
- if(obj == null) return false;
- if(!(obj instanceof Block)) return false;
+ public boolean equals(final Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (!(obj instanceof Block)) return false;
- Block other = (Block) obj;
+ final Block other = (Block) obj;
- if(blockNo != other.blockNo) return false;
+ if (blockNo != other.blockNo) return false;
- if(contents == null) {
- if(other.contents != null) return false;
- } else if(!contents.equals(other.contents)) return false;
+ if (contents == null) {
+ if (other.contents != null) return false;
+ } else if (!contents.equals(other.contents)) return false;
- if(endLine != other.endLine) return false;
- if(startLine != other.startLine) return false;
+ if (endLine != other.endLine) return false;
+ if (startLine != other.startLine) return false;
return true;
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java
index d45a4f3..dac535e 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java
@@ -7,39 +7,39 @@ import java.util.function.Consumer;
/**
* A source of blocks of characters, marked with line numbers as to block
* start/block end.
- *
+ *
* @author bjculkin
*
*/
public interface BlockReader extends AutoCloseable, Iterator<Block> {
/**
* Check if this reader has an available block.
- *
+ *
* @return Whether or not another block is available.
*/
boolean hasNextBlock();
/**
* Get the current block.
- *
+ *
* @return The current block, or null if there is no current block.
*/
Block getBlock();
/**
* Move to the next block.
- *
+ *
* @return Whether or not the next block was successfully read.
*/
boolean nextBlock();
/**
* Execute an action for each remaining block.
- *
+ *
* @param action
* The action to execute for each block
*/
- default void forEachBlock(Consumer<Block> action) {
+ default void forEachBlock(final Consumer<Block> action) {
while (hasNext()) {
action.accept(next());
}
@@ -47,11 +47,12 @@ public interface BlockReader extends AutoCloseable, Iterator<Block> {
/**
* Retrieve the number of blocks that have been read so far.
- *
+ *
* @return The number of blocks read so far.
*/
int getBlockCount();
+ @Override
void close() throws IOException;
@Override
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java
index ca82b51..8bbb89c 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java
@@ -4,50 +4,50 @@ import java.io.Reader;
/**
* Utility methods for constructing instances of {@link BlockReader}
- *
+ *
* @author bjculkin
*
*/
public class BlockReaders {
/**
* Create a new simple block reader that works off a regex.
- *
+ *
* @param blockDelim
* The regex that separates blocks.
- *
+ *
* @param source
* The reader to get blocks from.
- *
+ *
* @return A configured simple reader.
*/
- public static SimpleBlockReader simple(String blockDelim, Reader source) {
+ public static SimpleBlockReader simple(final String blockDelim, final Reader source) {
return new SimpleBlockReader(blockDelim, source);
}
/**
* Create a new pushback block reader.
- *
+ *
* @param src
* The block reader to read blocks from.
- *
+ *
* @return A configured pushback reader.
*/
- public static PushbackBlockReader pushback(BlockReader src) {
+ public static PushbackBlockReader pushback(final BlockReader src) {
return new PushbackBlockReader(src);
}
/**
* Create a new triggered block reader.
- *
+ *
* @param source
* The block reader to read blocks from.
- *
+ *
* @param action
* The action to execute before reading a block.
- *
+ *
* @return A configured triggered block reader.
*/
- public static BlockReader trigger(BlockReader source, Runnable action) {
+ public static BlockReader trigger(final BlockReader source, final Runnable action) {
return new TriggeredBlockReader(source, action);
}
@@ -56,26 +56,26 @@ public class BlockReaders {
*
* @param primary
* The first source to read blocks from.
- *
+ *
* @param secondary
* The second source to read blocks from.
- *
+ *
* @return A configured layered block reader.
*/
- public static BlockReader layered(BlockReader primary, BlockReader secondary) {
+ public static BlockReader layered(final BlockReader primary, final BlockReader secondary) {
return new LayeredBlockReader(primary, secondary);
}
/**
* Create a new serial block reader.
- *
+ *
* @param readers
* The readers to pull from, in the order to pull from
* them.
- *
+ *
* @return A configured serial block reader.
*/
- public static BlockReader serial(BlockReader... readers) {
+ public static BlockReader serial(final BlockReader... readers) {
return new SerialBlockReader(readers);
}
} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/LayeredBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/LayeredBlockReader.java
index 9ece6df..54010fe 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/LayeredBlockReader.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/LayeredBlockReader.java
@@ -5,30 +5,30 @@ import java.io.IOException;
/**
* A block reader that supports draining all the blocks from one reading before
* swapping to another.
- *
+ *
* This is more a 'prioritize blocks from one over the other', than a 'read all
* the blocks from one, then all the blocks from the other'. If you need that,
* look at {@link SerialBlockReader}.
- *
+ *
* @author bjculkin
*
*/
public class LayeredBlockReader implements BlockReader {
- private BlockReader first;
- private BlockReader second;
+ private final BlockReader first;
+ private final BlockReader second;
private int blockNo;
/**
* Create a new layered block reader.
- *
+ *
* @param primary
* The first source to read blocks from.
- *
+ *
* @param secondary
* The second source to read blocks from.
*/
- public LayeredBlockReader(BlockReader primary, BlockReader secondary) {
+ public LayeredBlockReader(final BlockReader primary, final BlockReader secondary) {
first = primary;
second = secondary;
}
@@ -40,18 +40,20 @@ public class LayeredBlockReader implements BlockReader {
@Override
public Block getBlock() {
- Block firstBlock = first.getBlock();
+ final Block firstBlock = first.getBlock();
return firstBlock == null ? second.getBlock() : firstBlock;
}
@Override
public boolean nextBlock() {
- boolean gotFirst = first.nextBlock();
+ final boolean gotFirst = first.nextBlock();
- boolean succ = gotFirst ? gotFirst : second.nextBlock();
+ final boolean succ = gotFirst ? gotFirst : second.nextBlock();
- if (succ) blockNo += 1;
+ if (succ) {
+ blockNo += 1;
+ }
return succ;
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java
index 96906ae..d7ba247 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java
@@ -7,14 +7,14 @@ import java.util.LinkedList;
/**
* A block reader that supports pushing blocks onto the input queue so that they
* are provided before blocks read from an input source.
- *
+ *
* @author bjculkin
*
*/
public class PushbackBlockReader implements BlockReader {
- private BlockReader source;
+ private final BlockReader source;
- private Deque<Block> waiting;
+ private final Deque<Block> waiting;
private Block curBlock;
@@ -22,11 +22,11 @@ public class PushbackBlockReader implements BlockReader {
/**
* Create a new pushback block reader.
- *
+ *
* @param src
* The block reader to use when no blocks are queued.
*/
- public PushbackBlockReader(BlockReader src) {
+ public PushbackBlockReader(final BlockReader src) {
source = src;
waiting = new LinkedList<>();
@@ -51,10 +51,12 @@ public class PushbackBlockReader implements BlockReader {
return true;
} else {
- boolean succ = source.nextBlock();
+ final boolean succ = source.nextBlock();
curBlock = source.getBlock();
- if (succ) blockNo += 1;
+ if (succ) {
+ blockNo += 1;
+ }
return succ;
}
@@ -72,21 +74,21 @@ 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.
*/
- public void addBlock(Block blk) {
+ public void addBlock(final Block blk) {
waiting.add(blk);
}
/**
* Insert a block at the front of the queue of pending blocks.
- *
+ *
* @param blk
* The block to put at the front.
*/
- public void pushBlock(Block blk) {
+ public void pushBlock(final Block blk) {
waiting.push(blk);
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java
index 2363468..7735981 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/SerialBlockReader.java
@@ -5,7 +5,7 @@ import java.util.Deque;
/**
* Provides a means of concatenating two block readers.
- *
+ *
* @author bjculkin
*
*/
@@ -16,13 +16,13 @@ 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.
*/
- public SerialBlockReader(BlockReader... readers) {
- for (BlockReader reader : readers) {
+ public SerialBlockReader(final BlockReader... readers) {
+ for (final BlockReader reader : readers) {
readerQueue.add(reader);
}
}
@@ -38,7 +38,7 @@ public class SerialBlockReader implements BlockReader {
while (!cont) {
try {
readerQueue.pop().close();
- } catch (IOException ioex) {
+ } catch (final IOException ioex) {
throw new IllegalStateException("Exception thrown by discarded reader", ioex);
}
@@ -68,7 +68,7 @@ public class SerialBlockReader implements BlockReader {
while (!cont) {
try {
readerQueue.pop().close();
- } catch (IOException ioex) {
+ } catch (final IOException ioex) {
throw new IllegalStateException("Exception thrown by discarded reader", ioex);
}
@@ -77,7 +77,9 @@ public class SerialBlockReader implements BlockReader {
cont = gotBlock || readerQueue.isEmpty();
}
- if (cont) blockNo += 1;
+ if (cont) {
+ blockNo += 1;
+ }
return cont;
}
@@ -90,7 +92,7 @@ public class SerialBlockReader implements BlockReader {
@Override
public void close() throws IOException {
while (!readerQueue.isEmpty()) {
- BlockReader reader = readerQueue.pop();
+ final BlockReader reader = readerQueue.pop();
reader.close();
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java
index 6ee1d57..87083d1 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java
@@ -9,10 +9,10 @@ import java.util.regex.Pattern;
/**
* Simple implementation of {@link BlockReader}
- *
+ *
* 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
*
*/
@@ -20,8 +20,8 @@ public class SimpleBlockReader implements BlockReader {
/*
* I/O source for blocks.
*/
- private LineNumberReader lnReader;
- private Scanner blockReader;
+ private final LineNumberReader lnReader;
+ private final Scanner blockReader;
/*
* The current block.
@@ -31,21 +31,21 @@ 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.
- *
+ *
* @param source
* The source to read blocks from.
*/
- public SimpleBlockReader(String blockDelim, Reader source) {
+ public SimpleBlockReader(final String blockDelim, final Reader source) {
lnReader = new LineNumberReader(source);
blockReader = new Scanner(lnReader);
- String pattern = String.format("(?:%s)|\\Z", blockDelim);
- Pattern pt = Pattern.compile(pattern, Pattern.MULTILINE);
+ final String pattern = String.format("(?:%s)|\\Z", blockDelim);
+ final Pattern pt = Pattern.compile(pattern, Pattern.MULTILINE);
blockReader.useDelimiter(pt);
}
@@ -63,15 +63,15 @@ public class SimpleBlockReader implements BlockReader {
@Override
public boolean nextBlock() {
try {
- int blockStartLine = lnReader.getLineNumber();
- String blockContents = blockReader.next();
- int blockEndLine = lnReader.getLineNumber();
+ final int blockStartLine = lnReader.getLineNumber();
+ final String blockContents = blockReader.next();
+ final int blockEndLine = lnReader.getLineNumber();
blockNo += 1;
currBlock = new Block(blockNo, blockContents, blockStartLine, blockEndLine);
return true;
- } catch (NoSuchElementException nseex) {
+ } catch (final NoSuchElementException nseex) {
currBlock = null;
return false;
@@ -92,11 +92,11 @@ public class SimpleBlockReader implements BlockReader {
/**
* Set the delimiter used to separate blocks.
- *
+ *
* @param delim
* The delimiter used to separate blocks.
*/
- public void setDelimiter(String delim) {
+ public void setDelimiter(final String delim) {
blockReader.useDelimiter(delim);
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/TriggeredBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/TriggeredBlockReader.java
index cfe72c2..0e50ad6 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/TriggeredBlockReader.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/blocks/TriggeredBlockReader.java
@@ -4,25 +4,25 @@ 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 final BlockReader source;
- private Runnable action;
+ private final 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) {
+ public TriggeredBlockReader(final BlockReader source, final Runnable action) {
super();
this.source = source;
this.action = action;