diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-26 11:30:43 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-26 11:30:43 -0400 |
| commit | 98cdf435d4974f4cca8f7b4eb4026da2c88cbc4c (patch) | |
| tree | ce5edea29a4da719affe61afda30a50afae15e87 /BJC-Utils2/src/main/java/bjc/utils/ioutils | |
| parent | b53c1ef47c438fb1144b961d1939c37a4bfe9120 (diff) | |
Update
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/ioutils/Block.java | 87 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReader.java | 127 |
2 files changed, 121 insertions, 93 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/Block.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/Block.java new file mode 100644 index 0000000..19de1ae --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/Block.java @@ -0,0 +1,87 @@ +package bjc.utils.ioutils; + +/** + * Represents a block of text read in from a source. + * + * @author EVE + * + */ +public class Block { + /** + * The contents of this block. + */ + public final String contents; + + /** + * The line of the source this block started on. + */ + public final int startLine; + + /** + * The line of the source this block ended on. + */ + public final int endLine; + + /** + * The number of this block. + */ + public final int blockNo; + + /** + * Create a new block. + * + * @param blockNo + * The number of this block. + * @param contents + * The contents of this block. + * @param startLine + * The line this block started on. + * @param endLine + * The line this block ended. + */ + public Block(int blockNo, String contents, int startLine, int endLine) { + this.contents = contents; + this.startLine = startLine; + this.endLine = endLine; + this.blockNo = blockNo; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + + result = prime * result + blockNo; + result = prime * result + ((contents == null) ? 0 : contents.hashCode()); + result = prime * result + endLine; + result = prime * result + startLine; + + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(!(obj instanceof Block)) return false; + + Block other = (Block) obj; + + if(blockNo != other.blockNo) 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; + + return true; + } + + @Override + public String toString() { + return String.format("Block #%d (from lines %d to %d), length: %d characters", blockNo, startLine, + endLine, contents.length()); + } +}
\ No newline at end of file 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 33bbbd5..fed74fe 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/BlockReader.java @@ -2,6 +2,7 @@ package bjc.utils.ioutils; import java.io.LineNumberReader; import java.io.Reader; +import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Scanner; import java.util.function.Consumer; @@ -10,7 +11,7 @@ import java.util.regex.Pattern; /** * Implements reading numbered blocks from a source. * - * A block is a series of characters, seperated by some regular expression. + * 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. @@ -18,93 +19,7 @@ import java.util.regex.Pattern; * @author EVE * */ -public class BlockReader implements AutoCloseable { - /** - * Represents a block of text read in from a source. - * - * @author EVE - * - */ - public static class Block { - /** - * The contents of this block. - */ - public final String contents; - - /** - * The line of the source this block started on. - */ - public final int startLine; - - /** - * The line of the source this block ended on. - */ - public final int endLine; - - /** - * The number of this block. - */ - public final int blockNo; - - /** - * Create a new block. - * - * @param blockNo - * The number of this block. - * @param contents - * The contents of this block. - * @param startLine - * The line this block started on. - * @param endLine - * The line this block ended. - */ - public Block(int blockNo, String contents, int startLine, int endLine) { - this.contents = contents; - this.startLine = startLine; - this.endLine = endLine; - this.blockNo = blockNo; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - - result = prime * result + blockNo; - result = prime * result + ((contents == null) ? 0 : contents.hashCode()); - result = prime * result + endLine; - result = prime * result + startLine; - - return result; - } - - @Override - public boolean equals(Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof Block)) return false; - - Block other = (Block) obj; - - if(blockNo != other.blockNo) 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; - - return true; - } - - @Override - public String toString() { - return String.format("Block #%d (from lines %d to %d) length: %d characters", blockNo, startLine, - endLine, contents.length()); - } - } - +public class BlockReader implements AutoCloseable, Iterator<Block> { /* * I/O source for blocks. */ @@ -131,10 +46,10 @@ public class BlockReader implements AutoCloseable { 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); } @@ -159,7 +74,7 @@ public class BlockReader implements AutoCloseable { /** * Move to the next block. * - * @return Whether or not the next block was succesfully read. + * @return Whether or not the next block was successfully read. */ public boolean nextBlock() { try { @@ -171,7 +86,7 @@ public class BlockReader implements AutoCloseable { currBlock = new Block(blockNo, blockContents, blockStartLine, blockEndLine); return true; - } catch(NoSuchElementException nseex) { + } catch (NoSuchElementException nseex) { return false; } } @@ -183,7 +98,7 @@ public class BlockReader implements AutoCloseable { * The action to execute for each block */ public void forEachBlock(Consumer<Block> action) { - while(hasNextBlock()) { + while (hasNextBlock()) { nextBlock(); action.accept(currBlock); @@ -205,4 +120,30 @@ public class BlockReader implements AutoCloseable { 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); + } + + /* + * Iterator implementation. + */ + + @Override + public boolean hasNext() { + return blockReader.hasNext(); + } + + @Override + public Block next() { + nextBlock(); + + return getBlock(); + } } |
