diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils')
13 files changed, 244 insertions, 236 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/Prompter.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/Prompter.java index eed53ae..a6ec4c0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/Prompter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/Prompter.java @@ -1,30 +1,30 @@ package bjc.utils.ioutils; -import bjc.utils.ioutils.blocks.TriggeredBlockReader; - import java.io.PrintStream; +import bjc.utils.ioutils.blocks.TriggeredBlockReader; + /** * A runnable for use with {@link TriggeredBlockReader} to prompt the user for * input. - * + * * @author bjculkin * */ public final class Prompter implements Runnable { - private String promt; - private PrintStream printer; + private String promt; + private final PrintStream printer; /** * Create a new prompter using the specified prompt. - * + * * @param prompt * The prompt to present. - * + * * @param output * The stream to print the prompt on. */ - public Prompter(String prompt, PrintStream output) { + public Prompter(final String prompt, final PrintStream output) { promt = prompt; printer = output; @@ -32,11 +32,11 @@ public final class Prompter implements Runnable { /** * Set the prompt this prompter uses. - * + * * @param prompt * The prompt this prompter uses. */ - public void setPrompt(String prompt) { + public void setPrompt(final String prompt) { promt = prompt; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java index 4f66a99..ee1e2ea 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java @@ -1,19 +1,19 @@ package bjc.utils.ioutils; +import java.util.function.BiFunction; +import java.util.function.UnaryOperator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import bjc.utils.data.Toggle; import bjc.utils.data.ValueToggle; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IList; import bjc.utils.functypes.ID; -import java.util.function.BiFunction; -import java.util.function.UnaryOperator; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - /** * Editor methods for strings based off the command language for the Sam editor. - * + * * @author EVE * */ @@ -23,65 +23,66 @@ public class RegexStringEditor { /** * Replace every occurrence of the pattern with the result of applying * the action to the string matched by the pattern. - * + * * @param input * The input string to process. - * + * * @param patt * The pattern to match the string against. - * + * * @param action * The action to transform matches with. - * + * * @return The string, with matches replaced with the action. */ - public static String onOccurances(String input, Pattern patt, UnaryOperator<String> action) { + public static String onOccurances(final String input, final Pattern patt, final UnaryOperator<String> action) { return reduceOccurances(input, patt, SID, action); } /** * Replace every occurrence between the patterns with the result of * applying the action to the strings between the patterns. - * + * * @param input * The input string to process. - * + * * @param patt * The pattern to match the string against. - * + * * @param action * The action to transform matches with. - * + * * @return The string, with strings between the matches replaced with * the action. */ - public static String betweenOccurances(String input, Pattern patt, UnaryOperator<String> action) { + public static String betweenOccurances(final String input, final Pattern patt, + final UnaryOperator<String> action) { return reduceOccurances(input, patt, action, SID); } /** * Execute actions between and on matches of a regular expression. - * + * * @param input * The input string. - * + * * @param rPatt * The pattern to match against the string. - * + * * @param betweenAction * The function to execute between matches of the string. - * + * * @param onAction * The function to execute on matches of the string. - * + * * @return The string, with both actions applied. */ - public static String reduceOccurances(String input, Pattern rPatt, UnaryOperator<String> betweenAction, - UnaryOperator<String> onAction) { - IList<String> occurances = listOccurances(input, rPatt); + public static String reduceOccurances(final String input, final Pattern rPatt, + final UnaryOperator<String> betweenAction, final UnaryOperator<String> onAction) { + final IList<String> occurances = listOccurances(input, rPatt); - Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction); - BiFunction<String, StringBuilder, StringBuilder> reducer = (strang, state) -> { + final Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction); + final BiFunction<String, StringBuilder, StringBuilder> reducer = (strang, state) -> { return state.append(actions.get().apply(strang)); }; @@ -90,50 +91,50 @@ public class RegexStringEditor { /** * Execute actions between and on matches of a regular expression. - * + * * @param input * The input string. - * + * * @param rPatt * The pattern to match against the string. - * + * * @param betweenAction * The function to execute between matches of the string. - * + * * @param onAction * The function to execute on matches of the string. - * + * * @return The string, with both actions applied. */ - public static IList<String> mapOccurances(String input, Pattern rPatt, UnaryOperator<String> betweenAction, - UnaryOperator<String> onAction) { - IList<String> occurances = listOccurances(input, rPatt); - Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction); + public static IList<String> mapOccurances(final String input, final Pattern rPatt, + final UnaryOperator<String> betweenAction, final UnaryOperator<String> onAction) { + final IList<String> occurances = listOccurances(input, rPatt); + final Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction); return occurances.map(strang -> actions.get().apply(strang)); } /** * Separate a string into match/non-match segments. - * + * * @param input * The string to separate. - * + * * @param rPatt * The pattern to use for separation. - * + * * @return The string, as a list of match/non-match segments, * starting/ending with a non-match segment. */ - public static IList<String> listOccurances(String input, Pattern rPatt) { - IList<String> res = new FunctionalList<>(); + public static IList<String> listOccurances(final String input, final Pattern rPatt) { + final IList<String> res = new FunctionalList<>(); - Matcher matcher = rPatt.matcher(input); + final Matcher matcher = rPatt.matcher(input); StringBuffer work = new StringBuffer(); - while(matcher.find()) { - String match = matcher.group(); + while (matcher.find()) { + final String match = matcher.group(); matcher.appendReplacement(work, ""); @@ -152,50 +153,46 @@ public class RegexStringEditor { /** * Apply an operation to a string if it matches a regular expression. - * + * * @param input * The input string. - * + * * @param patt * The pattern to match against it. - * + * * @param action * The action to execute if it matches. - * + * * @return The string, modified by the action if the pattern matched. */ - public static String ifMatches(String input, Pattern patt, UnaryOperator<String> action) { - Matcher matcher = patt.matcher(input); + public static String ifMatches(final String input, final Pattern patt, final UnaryOperator<String> action) { + final Matcher matcher = patt.matcher(input); - if(matcher.matches()) { + if (matcher.matches()) return action.apply(input); - } else { - return input; - } + else return input; } /** * Apply an operation to a string if it matches a regular expression. - * + * * @param input * The input string. - * + * * @param patt * The pattern to match against it. - * + * * @param action * The action to execute if it doesn't match. - * + * * @return The string, modified by the action if the pattern didn't * match. */ - public static String ifNotMatches(String input, Pattern patt, UnaryOperator<String> action) { - Matcher matcher = patt.matcher(input); + public static String ifNotMatches(final String input, final Pattern patt, final UnaryOperator<String> action) { + final Matcher matcher = patt.matcher(input); - if(matcher.matches()) { + if (matcher.matches()) return input; - } else { - return action.apply(input); - } + else return action.apply(input); } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java index b4d56a1..4216544 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java @@ -1,5 +1,11 @@ package bjc.utils.ioutils; +import java.io.InputStream; +import java.util.InputMismatchException; +import java.util.Scanner; +import java.util.function.BiConsumer; +import java.util.function.Consumer; + import bjc.utils.data.IHolder; import bjc.utils.data.IPair; import bjc.utils.data.Identity; @@ -9,12 +15,6 @@ import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.FunctionalStringTokenizer; import bjc.utils.funcdata.IMap; -import java.io.InputStream; -import java.util.InputMismatchException; -import java.util.Scanner; -import java.util.function.BiConsumer; -import java.util.function.Consumer; - /** * This class parses a rules based config file, and uses it to drive a provided * set of actions @@ -41,7 +41,7 @@ public class RuleBasedConfigReader<E> { // Map of pragma names to pragma actions // Pragma actions are functions taking a tokenizer and application state - private IMap<String, BiConsumer<FunctionalStringTokenizer, E>> pragmas; + private final IMap<String, BiConsumer<FunctionalStringTokenizer, E>> pragmas; /** * Create a new rule-based config reader @@ -53,8 +53,8 @@ public class RuleBasedConfigReader<E> { * @param end * The action to fire when ending a rule */ - public RuleBasedConfigReader(BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start, - BiConsumer<FunctionalStringTokenizer, E> continueRule, Consumer<E> end) { + public RuleBasedConfigReader(final BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start, + final BiConsumer<FunctionalStringTokenizer, E> continueRule, final Consumer<E> end) { this.start = start; this.continueRule = continueRule; this.end = end; @@ -70,33 +70,33 @@ public class RuleBasedConfigReader<E> { * @param action * The function to execute when this pragma is read */ - public void addPragma(String name, BiConsumer<FunctionalStringTokenizer, E> action) { - if(name == null) + public void addPragma(final String name, final BiConsumer<FunctionalStringTokenizer, E> action) { + if (name == null) throw new NullPointerException("Pragma name must not be null"); - else if(action == null) throw new NullPointerException("Pragma action must not be null"); + else if (action == null) throw new NullPointerException("Pragma action must not be null"); pragmas.put(name, action); } - private void continueRule(E state, boolean isRuleOpen, String line) { + private void continueRule(final E state, final boolean isRuleOpen, final String line) { // Make sure our input is correct - if(isRuleOpen == false) + if (isRuleOpen == false) throw new InputMismatchException("Cannot continue rule with no rule open"); - else if(continueRule == null) + else if (continueRule == null) throw new InputMismatchException("Rule continuation not supported for current grammar"); // Accept the rule continueRule.accept(new FunctionalStringTokenizer(line.substring(1), " "), state); } - private boolean endRule(E state, boolean isRuleOpen) { + private boolean endRule(final E state, final boolean isRuleOpen) { // Ignore blank line without an open rule - if(isRuleOpen == false) + if (isRuleOpen == false) // Do nothing return false; else { // Nothing happens on rule end - if(end != null) { + if (end != null) { // Process the rule ending end.accept(state); } @@ -115,28 +115,28 @@ public class RuleBasedConfigReader<E> { * The initial state of the reader * @return The final state of the reader */ - public E fromStream(InputStream input, E initialState) { - if(input == null) throw new NullPointerException("Input stream must not be null"); + public E fromStream(final InputStream input, final E initialState) { + if (input == null) throw new NullPointerException("Input stream must not be null"); // Application state: We're giving this back later - E state = initialState; + final E state = initialState; // Prepare our input source - try(Scanner source = new Scanner(input)) { + try (Scanner source = new Scanner(input)) { source.useDelimiter("\n"); // This is true when a rule's open - IHolder<Boolean> isRuleOpen = new Identity<>(false); + final IHolder<Boolean> isRuleOpen = new Identity<>(false); // Do something for every line of the file source.forEachRemaining((line) -> { // Skip comment lines - if(line.startsWith("#") || line.startsWith("//")) + if (line.startsWith("#") || line.startsWith("//")) // It's a comment return; - else if(line.equals("")) { + else if (line.equals("")) { // End the rule isRuleOpen.replace(endRule(state, isRuleOpen.getValue())); - } else if(line.startsWith("\t")) { + } else if (line.startsWith("\t")) { // Continue the rule continueRule(state, isRuleOpen.getValue(), line); } else { @@ -157,7 +157,7 @@ public class RuleBasedConfigReader<E> { * @param continueRule * The action to execute on continuation of a rule */ - public void setContinueRule(BiConsumer<FunctionalStringTokenizer, E> continueRule) { + public void setContinueRule(final BiConsumer<FunctionalStringTokenizer, E> continueRule) { this.continueRule = continueRule; } @@ -167,7 +167,7 @@ public class RuleBasedConfigReader<E> { * @param end * The action to execute on ending of a rule */ - public void setEndRule(Consumer<E> end) { + public void setEndRule(final Consumer<E> end) { this.end = end; } @@ -177,23 +177,23 @@ public class RuleBasedConfigReader<E> { * @param start * The action to execute on starting of a rule */ - public void setStartRule(BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start) { - if(start == null) throw new NullPointerException("Action on rule start must be non-null"); + public void setStartRule(final BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start) { + if (start == null) throw new NullPointerException("Action on rule start must be non-null"); this.start = start; } - private boolean startRule(E state, boolean isRuleOpen, String line) { + private boolean startRule(final E state, boolean isRuleOpen, final String line) { // Create the line tokenizer - FunctionalStringTokenizer tokenizer = new FunctionalStringTokenizer(line, " "); + final FunctionalStringTokenizer tokenizer = new FunctionalStringTokenizer(line, " "); // Get the initial token - String nextToken = tokenizer.nextToken(); + final String nextToken = tokenizer.nextToken(); // Handle pragmas - if(nextToken.equals("pragma")) { + if (nextToken.equals("pragma")) { // Get the pragma name - String token = tokenizer.nextToken(); + final String token = tokenizer.nextToken(); // Handle pragmas pragmas.getOrDefault(token, (tokenzer, stat) -> { @@ -201,7 +201,7 @@ public class RuleBasedConfigReader<E> { }).accept(tokenizer, state); } else { // Make sure input is correct - if(isRuleOpen == true) + if (isRuleOpen == true) throw new InputMismatchException("Nested rules are currently not supported"); // Start a rule diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java index f19eb2c..ccb73a4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java @@ -1,11 +1,11 @@ package bjc.utils.ioutils; +import java.util.function.BiConsumer; + import bjc.utils.exceptions.PragmaFormatException; import bjc.utils.funcdata.FunctionalStringTokenizer; import bjc.utils.funcutils.ListUtils; -import java.util.function.BiConsumer; - /** * Contains factory methods for common pragma types * @@ -25,22 +25,22 @@ public class RuleBasedReaderPragmas { * The function to invoke with the parsed integer * @return A pragma that functions as described above. */ - public static <StateType> BiConsumer<FunctionalStringTokenizer, StateType> buildInteger(String name, - BiConsumer<Integer, StateType> consumer) { + public static <StateType> BiConsumer<FunctionalStringTokenizer, StateType> buildInteger(final String name, + final BiConsumer<Integer, StateType> consumer) { return (tokenizer, state) -> { // Check our input is correct - if(!tokenizer.hasMoreTokens()) + if (!tokenizer.hasMoreTokens()) throw new PragmaFormatException("Pragma " + name + " requires one integer argument"); // Read the argument - String token = tokenizer.nextToken(); + final String token = tokenizer.nextToken(); try { // Run the pragma consumer.accept(Integer.parseInt(token), state); - } catch(NumberFormatException nfex) { + } catch (final NumberFormatException nfex) { // Tell the user their argument isn't correct - PragmaFormatException pfex = new PragmaFormatException( + final PragmaFormatException pfex = new PragmaFormatException( "Argument " + token + " to " + name + " pragma isn't a valid integer. " + "This pragma requires a integer argument"); @@ -63,15 +63,15 @@ public class RuleBasedReaderPragmas { * The function to invoke with the parsed string * @return A pragma that functions as described above. */ - public static <StateType> BiConsumer<FunctionalStringTokenizer, StateType> buildStringCollapser(String name, - BiConsumer<String, StateType> consumer) { + public static <StateType> BiConsumer<FunctionalStringTokenizer, StateType> buildStringCollapser( + final String name, final BiConsumer<String, StateType> consumer) { return (tokenizer, state) -> { // Check our input - if(!tokenizer.hasMoreTokens()) throw new PragmaFormatException( + if (!tokenizer.hasMoreTokens()) throw new PragmaFormatException( "Pragma " + name + " requires one or more string arguments"); // Build our argument - String collapsed = ListUtils.collapseTokens(tokenizer.toList()); + final String collapsed = ListUtils.collapseTokens(tokenizer.toList()); // Run the pragma consumer.accept(collapsed, state); diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java index f00f245..e142ea3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java @@ -10,12 +10,12 @@ import java.util.Set; /** * Simple file based properties. - * + * * @author EVE * */ public class SimpleProperties implements Map<String, String> { - private Map<String, String> props; + private final Map<String, String> props; /** * Create a new set of simple properties. @@ -26,42 +26,46 @@ public class SimpleProperties implements Map<String, String> { /** * Load properties from the provided input stream. - * + * * The format is the name, a space, then the body. - * + * * All leading/trailing spaces from the name & body are removed. - * + * * @param is * The stream to read from. - * + * * @param allowDuplicates * Whether or not duplicate keys should be allowed. */ - public void loadFrom(InputStream is, boolean allowDuplicates) { + public void loadFrom(final InputStream is, final boolean allowDuplicates) { try (Scanner scn = new Scanner(is)) { while (scn.hasNextLine()) { - String ln = scn.nextLine().trim(); + final String ln = scn.nextLine().trim(); /* * Skip blank lines/comments */ - if (ln.equals("")) continue; - if (ln.startsWith("#")) continue; + if (ln.equals("")) { + continue; + } + if (ln.startsWith("#")) { + continue; + } - int sepIdx = ln.indexOf(' '); + final int sepIdx = ln.indexOf(' '); if (sepIdx == -1) { - String fmt = "Properties must be a name, a space, then the body.\n\tOffending line is '%s'"; - String msg = String.format(fmt, ln); + final String fmt = "Properties must be a name, a space, then the body.\n\tOffending line is '%s'"; + final String msg = String.format(fmt, ln); throw new NoSuchElementException(msg); } - String name = ln.substring(0, sepIdx).trim(); - String body = ln.substring(sepIdx).trim(); + final String name = ln.substring(0, sepIdx).trim(); + final String body = ln.substring(sepIdx).trim(); if (!allowDuplicates && containsKey(name)) { - String msg = String.format("Duplicate key '%s'", name); + final String msg = String.format("Duplicate key '%s'", name); throw new IllegalStateException(msg); } @@ -77,7 +81,7 @@ public class SimpleProperties implements Map<String, String> { public void outputProperties() { System.out.println("Read properties:"); - for (Entry<String, String> entry : entrySet()) { + for (final Entry<String, String> entry : entrySet()) { System.out.printf("\t'%s'\t'%s'\n", entry.getKey(), entry.getValue()); } @@ -96,35 +100,35 @@ public class SimpleProperties implements Map<String, String> { @SuppressWarnings("unlikely-arg-type") @Override - public boolean containsKey(Object key) { + public boolean containsKey(final Object key) { return props.containsKey(key); } @SuppressWarnings("unlikely-arg-type") @Override - public boolean containsValue(Object value) { + public boolean containsValue(final Object value) { return props.containsValue(value); } @SuppressWarnings("unlikely-arg-type") @Override - public String get(Object key) { + public String get(final Object key) { return props.get(key); } @Override - public String put(String key, String value) { + public String put(final String key, final String value) { return props.put(key, value); } @SuppressWarnings("unlikely-arg-type") @Override - public String remove(Object key) { + public String remove(final Object key) { return props.remove(key); } @Override - public void putAll(Map<? extends String, ? extends String> m) { + public void putAll(final Map<? extends String, ? extends String> m) { props.putAll(m); } @@ -149,7 +153,7 @@ public class SimpleProperties implements Map<String, String> { } @Override - public boolean equals(Object o) { + public boolean equals(final Object o) { return props.equals(o); } 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; |
