From 32f5da54c628408c96db09d279f3a7ef44b3bd19 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Mon, 12 Feb 2018 22:19:02 -0500 Subject: Update --- base/src/main/java/bjc/utils/cli/CommandMode.java | 2 +- .../java/bjc/utils/cli/objects/BlockReaderCLI.java | 116 ++++++++++++--------- .../main/java/bjc/utils/cli/objects/Command.java | 40 ++++--- .../main/java/bjc/utils/cli/objects/DefineCLI.java | 49 ++++++--- .../bjc/utils/components/ComponentDescription.java | 1 + base/src/main/java/bjc/utils/data/Either.java | 12 +-- .../java/bjc/utils/data/internals/BoundLazy.java | 1 + .../bjc/utils/data/internals/BoundLazyPair.java | 1 + .../bjc/utils/data/internals/BoundListHolder.java | 1 + .../src/main/java/bjc/utils/esodata/AbbrevMap.java | 55 +++++----- .../bjc/utils/ioutils/blocks/BoundBlockReader.java | 42 ++++++-- .../utils/ioutils/blocks/FilteredBlockReader.java | 14 +-- .../java/bjc/utils/ioutils/format/CLModifiers.java | 33 +++++- .../java/bjc/utils/ioutils/format/Directive.java | 29 +++++- .../bjc/utils/ioutils/format/EscapeException.java | 18 ++++ .../utils/ioutils/format/IterationDirective.java | 2 - base/src/main/java/bjc/utils/math/DualExpr.java | 84 ++++++++------- base/src/main/java/bjc/utils/math/NumberUtils.java | 106 ++++++++++--------- .../utils/parserutils/delims/DelimiterGroup.java | 6 +- 19 files changed, 372 insertions(+), 240 deletions(-) (limited to 'base/src') diff --git a/base/src/main/java/bjc/utils/cli/CommandMode.java b/base/src/main/java/bjc/utils/cli/CommandMode.java index 4107717..0415e27 100644 --- a/base/src/main/java/bjc/utils/cli/CommandMode.java +++ b/base/src/main/java/bjc/utils/cli/CommandMode.java @@ -19,7 +19,7 @@ public interface CommandMode extends Comparable { */ default boolean canHandle(final String command) { return false; - }; + } /** * Get the custom prompt for this mode. diff --git a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java index 94ee726..ac62c80 100644 --- a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java @@ -35,20 +35,20 @@ public class BlockReaderCLI { /** * All of the configured block readers. */ - public final Map readers; + public final Map readers; /** * All of the configured I/O sources. */ - public final Map sources; + public final Map sources; /** * Create a new set of state for the block reader. * * @param readers - * The set of configured block readers. + * The set of configured block readers. * * @param sources - * The set of configured I/O sources. + * The set of configured I/O sources. */ public BlockReaderState(Map readers, Map sources) { this.readers = readers; @@ -63,7 +63,7 @@ public class BlockReaderCLI { * Create a new CLI for configuring BlockReaders. * * @param srcs - * The container of initial I/O sources. + * The container of initial I/O sources. */ public BlockReaderCLI(Map srcs) { stat = new BlockReaderState(new HashMap<>(), srcs); @@ -73,7 +73,7 @@ public class BlockReaderCLI { * Create a new CLI for configuring BlockReaders. * * @param state - * The state object to use. + * The state object to use. */ public BlockReaderCLI(BlockReaderState state) { stat = state; @@ -84,12 +84,12 @@ public class BlockReaderCLI { * Run the command line interface * * @param args - * Ignored CLI args. + * Ignored CLI args. */ public static void main(String[] args) { /* Create/configure I/O sources. */ Map sources = new HashMap<>(); - BlockReaderCLI reader = new BlockReaderCLI(sources); + BlockReaderCLI reader = new BlockReaderCLI(sources); sources.put("stdio", new InputStreamReader(System.in)); @@ -102,13 +102,13 @@ public class BlockReaderCLI { * Run the CLI on an input source. * * @param input - * The place to read input from. + * The place to read input from. * * @param ioSource - * The name of the place to read input from. + * The name of the place to read input from. * * @param interactive - * Whether or not the source is interactive + * Whether or not the source is interactive */ public void run(Scanner input, String ioSource, boolean interactive) { int lno = 0; @@ -128,9 +128,9 @@ public class BlockReaderCLI { if(com == null) continue; /* Handle a command. */ - CommandStatus stat = handleCommand(com, interactive); + CommandStatus stt = handleCommand(com, interactive); /* Exit if we finished or encountered a fatal error. */ - if(stat == FINISH || stat == ERROR) { + if(stt == FINISH || stt == ERROR) { return; } @@ -146,36 +146,36 @@ public class BlockReaderCLI { * Handle a command. * * @param com - * The command to handle + * The command to handle * * @param interactive - * Whether the current input source is interactive or not. + * Whether the current input source is interactive or not. + * @return The status of the executed command. */ public CommandStatus handleCommand(Command com, boolean interactive) { switch(com.nameCommand) { - case "def-filtered": - return defFiltered(com); - case "def-layered": - return defLayered(com); - case "def-pushback": - return defPushback(com); - case "def-simple": - return defSimple(com); - case "def-serial": - return defSerial(com); - case "def-toggled": - return defToggled(com); - case "}": - case "end": - case "exit": - case "quit": - if(interactive) - System.out.printf("Exiting reader-conf, %d readers configured in %d commands\n", - stat.readers.size(), com.lineNo); - return FINISH; - default: - LOGGER.severe(com.error("Unknown command '%s'\n", com.nameCommand)); - return FAIL; + case "def-filtered": + return defFiltered(com); + case "def-layered": + return defLayered(com); + case "def-pushback": + return defPushback(com); + case "def-simple": + return defSimple(com); + case "def-serial": + return defSerial(com); + case "def-toggled": + return defToggled(com); + case "}": + case "end": + case "exit": + case "quit": + if(interactive) System.out.printf("Exiting reader-conf, %d readers configured in %d commands\n", + stat.readers.size(), com.lineNo); + return FINISH; + default: + LOGGER.severe(com.error("Unknown command '%s'\n", com.nameCommand)); + return FAIL; } } @@ -191,7 +191,7 @@ public class BlockReaderCLI { return FAIL; } String blockName = remn.substring(0, idx).trim(); - remn = remn.substring(idx).trim(); + remn = remn.substring(idx).trim(); /* * Check there isn't a reader already bound to this name. @@ -205,11 +205,11 @@ public class BlockReaderCLI { */ idx = remn.indexOf(' '); if(idx == -1) { - LOGGER.severe(com.error("No reader-name argument for def-filtered.\n")); + LOGGER.severe(com.error("No reader-name argument for def-filtered.\n")); return FAIL; } String readerName = remn.substring(0, idx).trim(); - remn = remn.substring(idx).trim(); + remn = remn.substring(idx).trim(); /* * Check there is a reader bound to that name. @@ -238,22 +238,26 @@ public class BlockReaderCLI { return mat.matches(); }; + @SuppressWarnings("resource") BlockReader reader = new FilteredBlockReader(stat.readers.get(readerName), pred); stat.readers.put(blockName, reader); - } catch (PatternSyntaxException psex) { - LOGGER.severe(com.error("Invalid regular expression '%s' for filter. (%s)\n", filter, psex.getMessage())); + } catch(PatternSyntaxException psex) { + LOGGER.severe(com.error("Invalid regular expression '%s' for filter. (%s)\n", filter, + psex.getMessage())); return FAIL; } return SUCCESS; } + @SuppressWarnings("resource") private CommandStatus defPushback(Command com) { String[] parts = com.remnCommand.split(" "); if(parts.length != 2) { - LOGGER.severe(com.error("Incorrect number of arguments to def-pushback. Requires a block name and a reader name\n")); + LOGGER.severe(com.error( + "Incorrect number of arguments to def-pushback. Requires a block name and a reader name\n")); return FAIL; } @@ -275,11 +279,13 @@ public class BlockReaderCLI { return SUCCESS; } + @SuppressWarnings("resource") private CommandStatus defToggled(Command com) { String[] parts = com.remnCommand.split(" "); if(parts.length != 3) { - LOGGER.severe(com.error("Incorrect number of arguments to def-toggled. Requires a block name and two reader names\n")); + LOGGER.severe(com.error( + "Incorrect number of arguments to def-toggled. Requires a block name and two reader names\n")); return FAIL; } @@ -310,11 +316,13 @@ public class BlockReaderCLI { return SUCCESS; } + @SuppressWarnings("resource") private CommandStatus defLayered(Command com) { String[] parts = com.remnCommand.split(" "); if(parts.length != 3) { - LOGGER.severe(com.error("Incorrect number of arguments to def-layered. Requires a block name and two reader names\n")); + LOGGER.severe(com.error( + "Incorrect number of arguments to def-layered. Requires a block name and two reader names\n")); return FAIL; } @@ -345,11 +353,13 @@ public class BlockReaderCLI { return SUCCESS; } + @SuppressWarnings("resource") private CommandStatus defSerial(Command com) { String[] parts = com.remnCommand.split(" "); if(parts.length < 2) { - LOGGER.severe(com.error("Not enough arguments to def-serial. Requires at least a block name and at least one reader name\n")); + LOGGER.severe(com.error( + "Not enough arguments to def-serial. Requires at least a block name and at least one reader name\n")); return FAIL; } @@ -401,7 +411,7 @@ public class BlockReaderCLI { return FAIL; } String blockName = remn.substring(0, idx).trim(); - remn = remn.substring(idx).trim(); + remn = remn.substring(idx).trim(); /* * Check there isn't a reader already bound to this name. @@ -415,11 +425,11 @@ public class BlockReaderCLI { */ idx = remn.indexOf(' '); if(idx == -1) { - LOGGER.severe(com.error("No source-name argument for def-simple.\n")); + LOGGER.severe(com.error("No source-name argument for def-simple.\n")); return FAIL; } String sourceName = remn.substring(0, idx).trim(); - remn = remn.substring(idx).trim(); + remn = remn.substring(idx).trim(); /* * Check there is a source bound to that name. @@ -440,11 +450,13 @@ public class BlockReaderCLI { String delim = remn; try { + @SuppressWarnings("resource") BlockReader reader = new SimpleBlockReader(delim, stat.sources.get(sourceName)); stat.readers.put(blockName, reader); - } catch (PatternSyntaxException psex) { - LOGGER.severe(com.error("Invalid regular expression '%s' for delimiter. (%s)\n", delim, psex.getMessage())); + } catch(PatternSyntaxException psex) { + LOGGER.severe(com.error("Invalid regular expression '%s' for delimiter. (%s)\n", delim, + psex.getMessage())); return FAIL; } diff --git a/base/src/main/java/bjc/utils/cli/objects/Command.java b/base/src/main/java/bjc/utils/cli/objects/Command.java index 3a7d452..04cdfd9 100644 --- a/base/src/main/java/bjc/utils/cli/objects/Command.java +++ b/base/src/main/java/bjc/utils/cli/objects/Command.java @@ -38,15 +38,15 @@ public class Command { /** * The full text of this command. */ - public final String fullCommand; + public final String fullCommand; /** * The text of this command without its name. */ - public final String remnCommand; + public final String remnCommand; /** * The name of this command. */ - public final String nameCommand; + public final String nameCommand; /** * The name of the I/O source this command was read from. @@ -57,13 +57,13 @@ public class Command { * Create a new command. * * @param ln - * The string to get the command from. + * The string to get the command from. * * @param lno - * The number of the line the command came from. + * The number of the line the command came from. * * @param ioSrc - * The name of where the I/O came from. + * The name of where the I/O came from. */ public Command(String ln, int lno, String ioSrc) { int idx = ln.indexOf(' '); @@ -87,17 +87,18 @@ public class Command { * through the line. * * @param ln - * The string to get the command from. + * The string to get the command from. * * @param lno - * The line number of the command. + * The line number of the command. * * @param ioSource - * The name of where the I/O came from. + * The name of where the I/O came from. + * @return The parsed command */ public static Command fromString(String ln, int lno, String ioSource) { /* Ignore blank lines and comments. */ - if(ln.equals("")) return null; + if(ln.equals("")) return null; if(ln.startsWith("#")) return null; /* Trim off comments part-way through the line. */ @@ -114,10 +115,11 @@ public class Command { * command. * * @param info - * The informational message. + * The informational message. * * @param parms - * The parameters for the informational message. + * The parameters for the informational message. + * @return The information message. */ public String info(String info, Object... parms) { String msg = String.format(info, parms); @@ -129,10 +131,12 @@ public class Command { * Warn about something in relation to this command. * * @param warning - * The warning message. + * The warning message. * * @param parms - * The parameters for the warning message. + * The parameters for the warning message. + * + * @return The formatted warning. */ public String warn(String warning, Object... parms) { String msg = String.format(warning, parms); @@ -143,11 +147,13 @@ public class Command { /** * Give an error about something in relation to this command. * - * @param error - * The error message. + * @param err + * The error message. * * @param parms - * The parameters for the error message. + * The parameters for the error message. + * + * @return The formatted error */ public String error(String err, Object... parms) { String msg = String.format(err, parms); diff --git a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java index 280afd0..aa9bcf3 100644 --- a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java @@ -23,11 +23,11 @@ import static bjc.utils.cli.objects.Command.CommandStatus.*; public class DefineCLI { private final Logger LOGGER = Logger.getLogger(DefineCLI.class.getName()); - public static class DefineState { + static class DefineState { public final Map> defines; - public final Map strings; - public final Map formats; + public final Map strings; + public final Map formats; public final Map patterns; @@ -48,14 +48,23 @@ public class DefineCLI { private DefineState stat; + /** + * Create a new Define CLI + */ public DefineCLI() { stat = new DefineState(); } + /** + * Main method + * + * @param args + * CLI args + */ public static void main(String[] args) { DefineCLI defin = new DefineCLI(); - try (Scanner scn = new Scanner(System.in)) { + try(Scanner scn = new Scanner(System.in)) { defin.run(scn, "console", true); } } @@ -64,25 +73,23 @@ public class DefineCLI { * Run the CLI on an input source. * * @param input - * The place to read input from. + * The place to read input from. * @param ioSource - * The name of the place to read input from. + * The name of the place to read input from. * @param interactive - * Whether or not the source is interactive + * Whether or not the source is interactive */ public void run(Scanner input, String ioSource, boolean interactive) { int lno = 0; - while (input.hasNextLine()) { - if (interactive) - System.out.printf("define-conf(%d)>", lno); + while(input.hasNextLine()) { + if(interactive) System.out.printf("define-conf(%d)>", lno); String ln = input.nextLine(); lno += 1; Command com = Command.fromString(ln, lno, ioSource); - if (com == null) - continue; + if(com == null) continue; handleCommand(com, interactive); } @@ -90,8 +97,16 @@ public class DefineCLI { input.close(); } + /** + * Handle a command + * + * @param com + * The command to handle + * @param interactive + * Whether or not our I/O stream is interactive + */ public void handleCommand(Command com, boolean interactive) { - switch (com.nameCommand) { + switch(com.nameCommand) { case "def-string": default: LOGGER.severe(com.error("Unknown command %s\n", com.nameCommand)); @@ -103,14 +118,14 @@ public class DefineCLI { String remn = com.remnCommand; int idx = remn.indexOf(' '); - if (idx == -1) { + if(idx == -1) { LOGGER.warning(com.warn("Binding empty string to name '%s'\n", remn)); idx = remn.length(); } String name = remn.substring(0, idx); String strang = remn.substring(idx); - if (stat.strings.containsKey(name)) { + if(stat.strings.containsKey(name)) { LOGGER.warning(com.warn("Shadowing string '%s'\n", name)); } @@ -123,14 +138,14 @@ public class DefineCLI { String remn = com.remnCommand; int idx = remn.indexOf(' '); - if (idx == -1) { + if(idx == -1) { LOGGER.warning(com.warn("Binding empty format to name '%s'\n", remn)); idx = remn.length(); } String name = remn.substring(0, idx); String fmt = remn.substring(idx); - if (stat.formats.containsKey(name)) { + if(stat.formats.containsKey(name)) { LOGGER.warning(com.warn("Shadowing format '%s'\n", name)); } diff --git a/base/src/main/java/bjc/utils/components/ComponentDescription.java b/base/src/main/java/bjc/utils/components/ComponentDescription.java index 8d44855..4f52ace 100644 --- a/base/src/main/java/bjc/utils/components/ComponentDescription.java +++ b/base/src/main/java/bjc/utils/components/ComponentDescription.java @@ -7,6 +7,7 @@ package bjc.utils.components; */ public class ComponentDescription implements IDescribedComponent { /* Check arguments are good. */ + @SuppressWarnings("unused") private static void sanityCheckArgs(final String name, final String author, final String description, final int version) { if (name == null) { diff --git a/base/src/main/java/bjc/utils/data/Either.java b/base/src/main/java/bjc/utils/data/Either.java index 2b64feb..20a06f5 100644 --- a/base/src/main/java/bjc/utils/data/Either.java +++ b/base/src/main/java/bjc/utils/data/Either.java @@ -119,13 +119,13 @@ public class Either implements IPair { return new Either<>(cLeft, null); }); - } else { - return otherPair.bind((otherLeft, otherRight) -> { - CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); - - return new Either<>(null, cRight); - }); } + + return otherPair.bind((otherLeft, otherRight) -> { + CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); + + return new Either<>(null, cRight); + }); } @Override diff --git a/base/src/main/java/bjc/utils/data/internals/BoundLazy.java b/base/src/main/java/bjc/utils/data/internals/BoundLazy.java index b160c0c..a1c2ea0 100644 --- a/base/src/main/java/bjc/utils/data/internals/BoundLazy.java +++ b/base/src/main/java/bjc/utils/data/internals/BoundLazy.java @@ -14,6 +14,7 @@ import bjc.utils.funcdata.IList; * * @author Ben Culkin */ +@SuppressWarnings("javadoc") public class BoundLazy implements IHolder { /* The old value. */ private final Supplier> oldSupplier; diff --git a/base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java b/base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java index df2f0d8..401f5d5 100644 --- a/base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java +++ b/base/src/main/java/bjc/utils/data/internals/BoundLazyPair.java @@ -14,6 +14,7 @@ import bjc.utils.data.LazyPair; * * @author Ben Culkin */ +@SuppressWarnings("javadoc") public class BoundLazyPair implements IPair { /* The supplier of the left value. */ private final Supplier leftSupplier; diff --git a/base/src/main/java/bjc/utils/data/internals/BoundListHolder.java b/base/src/main/java/bjc/utils/data/internals/BoundListHolder.java index 8f9e87f..31178da 100644 --- a/base/src/main/java/bjc/utils/data/internals/BoundListHolder.java +++ b/base/src/main/java/bjc/utils/data/internals/BoundListHolder.java @@ -12,6 +12,7 @@ import bjc.utils.funcdata.IList; * * @author Ben Culkin */ +@SuppressWarnings("javadoc") public class BoundListHolder implements IHolder { /* The list of contained holders. */ private final IList> heldHolders; diff --git a/base/src/main/java/bjc/utils/esodata/AbbrevMap.java b/base/src/main/java/bjc/utils/esodata/AbbrevMap.java index 5aa44fc..64f542a 100644 --- a/base/src/main/java/bjc/utils/esodata/AbbrevMap.java +++ b/base/src/main/java/bjc/utils/esodata/AbbrevMap.java @@ -35,7 +35,7 @@ public class AbbrevMap { * Create a new abbreviation map. * * @param words - * The initial set of words to put in the map. + * The initial set of words to put in the map. */ public AbbrevMap(final String... words) { wrds = new HashSet<>(Arrays.asList(words)); @@ -56,7 +56,7 @@ public class AbbrevMap { seen = new HashSet<>(); - for (final String word : wrds) { + for(final String word : wrds) { intAddWord(word); } } @@ -65,12 +65,12 @@ public class AbbrevMap { * Adds words to the abbreviation map. * * @param words - * The words to add to the abbreviation map. + * The words to add to the abbreviation map. */ public void addWords(final String... words) { wrds.addAll(Arrays.asList(words)); - for (final String word : words) { + for(final String word : words) { intAddWord(word); } } @@ -81,23 +81,23 @@ public class AbbrevMap { abbrevMap.put(word, word); /* Skip blank words. */ - if (word.equals("")) return; + if(word.equals("")) return; /* Handle each possible abbreviation. */ - for (int i = word.length(); i > 0; i--) { + for(int i = word.length(); i > 0; i--) { final String subword = word.substring(0, i); - if (seen.contains(subword)) { + if(seen.contains(subword)) { /* * Remove a mapping if its ambiguous and not a * whole word. */ - if (abbrevMap.containsKey(subword) && !wrds.contains(subword)) { + if(abbrevMap.containsKey(subword) && !wrds.contains(subword)) { final String oldword = abbrevMap.remove(subword); ambMap.put(subword, oldword); ambMap.put(subword, word); - } else if (!wrds.contains(subword)) { + } else if(!wrds.contains(subword)) { ambMap.put(subword, word); } } else { @@ -115,12 +115,12 @@ public class AbbrevMap { * the map. Use {@link AbbrevMap#recalculate()} to fix it if it occurs. * * @param words - * The words to remove. + * The words to remove. */ public void removeWords(final String... words) { wrds.removeAll(Arrays.asList(words)); - for (final String word : words) { + for(final String word : words) { intRemoveWord(word); } } @@ -128,22 +128,22 @@ public class AbbrevMap { /* Actually remove a word. */ private void intRemoveWord(final String word) { /* Skip blank words. */ - if (word.equals("")) return; + if(word.equals("")) return; /* Handle each possible abbreviation. */ - for (int i = word.length(); i > 0; i--) { + for(int i = word.length(); i > 0; i--) { final String subword = word.substring(0, i); - if (abbrevMap.containsKey(subword)) { + if(abbrevMap.containsKey(subword)) { abbrevMap.remove(subword); } else { ambMap.remove(subword, word); final Set possWords = ambMap.get(subword); - if (possWords.size() == 0) { + if(possWords.size() == 0) { seen.remove(subword); - } else if (possWords.size() == 1) { + } else if(possWords.size() == 1) { /* * An abbreviation went from ambiguous * to non-ambiguous. @@ -162,17 +162,16 @@ public class AbbrevMap { * into. * * @param abbrev - * The abbreviation to convert. + * The abbreviation to convert. * - * @return - * All the expansions for the provided abbreviation. + * @return All the expansions for the provided abbreviation. */ public String[] deabbrev(final String abbrev) { - if (abbrevMap.containsKey(abbrev)) { + if(abbrevMap.containsKey(abbrev)) { return new String[] { abbrevMap.get(abbrev) }; - } else { - return ambMap.get(abbrev).toArray(new String[0]); } + + return ambMap.get(abbrev).toArray(new String[0]); } @Override @@ -187,15 +186,15 @@ public class AbbrevMap { @Override public boolean equals(final Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (!(obj instanceof AbbrevMap)) return false; + if(this == obj) return true; + if(obj == null) return false; + if(!(obj instanceof AbbrevMap)) return false; final AbbrevMap other = (AbbrevMap) obj; - if (wrds == null) { - if (other.wrds != null) return false; - } else if (!wrds.equals(other.wrds)) return false; + if(wrds == null) { + if(other.wrds != null) return false; + } else if(!wrds.equals(other.wrds)) return false; return true; } 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 b1e82d7..36e0ac1 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/BoundBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/BoundBlockReader.java @@ -5,24 +5,52 @@ import java.io.IOException; import java.util.function.BooleanSupplier; 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 + * + */ @FunctionalInterface public interface Closer { + /** + * Close the I/O source this is attached to. + * + * @throws IOException + * If something goes wrong + */ public void close() throws IOException; } - private BooleanSupplier checker; - private Supplier getter; - private Closer closer; + private BooleanSupplier checker; + private Supplier getter; + private Closer closer; private Block current; private int blockNo; + /** + * Create a new bound block reader. + * + * @param blockChecker + * Predicate for checking if a block is available + * @param blockGetter + * Function to retrieve a block + * @param blockCloser + * Function to close a block source + */ public BoundBlockReader(BooleanSupplier blockChecker, Supplier blockGetter, Closer blockCloser) { checker = blockChecker; - getter = blockGetter; - closer = blockCloser; + getter = blockGetter; + closer = blockCloser; blockNo = 0; } @@ -40,11 +68,11 @@ public class BoundBlockReader implements BlockReader { @Override public boolean nextBlock() { if(checker.getAsBoolean()) { - current = getter.get(); + current = getter.get(); blockNo += 1; return true; - } + } return false; } 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 0b43f7a..c575f05 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/FilteredBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/FilteredBlockReader.java @@ -16,8 +16,8 @@ public class FilteredBlockReader implements BlockReader { * * Both have already been checked for the predicate. */ - private Block current; - private Block pending; + private Block current; + private Block pending; /* * Number of blocks that passed the predicate. @@ -39,8 +39,8 @@ public class FilteredBlockReader implements BlockReader { } public FilteredBlockReader(BlockReader src, Predicate predic, Consumer failAct) { - source = src; - pred = predic; + source = src; + pred = predic; failAction = failAct; blockNo = 0; @@ -60,11 +60,11 @@ public class FilteredBlockReader implements BlockReader { if(pred.test(pending)) { blockNo += 1; return true; - } else { - failAction.accept(pending); } + + failAction.accept(pending); } - + return false; } diff --git a/base/src/main/java/bjc/utils/ioutils/format/CLModifiers.java b/base/src/main/java/bjc/utils/ioutils/format/CLModifiers.java index a695f2f..a535697 100644 --- a/base/src/main/java/bjc/utils/ioutils/format/CLModifiers.java +++ b/base/src/main/java/bjc/utils/ioutils/format/CLModifiers.java @@ -1,18 +1,45 @@ package bjc.utils.ioutils.format; +/** + * A collection of the modifiers attached to a CL format directive. + * + * @author EVE + * + */ public class CLModifiers { - public final boolean atMod; - public final boolean colonMod; + /** + * Whether the at mod is on. + */ + public final boolean atMod; + /** + * Whether the colon mod is on. + */ + public final boolean colonMod; + /** + * Create a new set of CL modifiers. + * + * @param at + * The state of the at mod. + * @param colon + * The state of the colon mod. + */ public CLModifiers(boolean at, boolean colon) { atMod = at; colonMod = colon; } + /** + * Create a set of modifiers from a modifier string. + * + * @param modString + * The string to parse modifiers from. + * @return A set of modifiers matching the string. + */ public static CLModifiers fromString(String modString) { boolean atMod = false; boolean colonMod = false; - if (modString != null) { + if(modString != null) { atMod = modString.contains("@"); colonMod = modString.contains(":"); } diff --git a/base/src/main/java/bjc/utils/ioutils/format/Directive.java b/base/src/main/java/bjc/utils/ioutils/format/Directive.java index 0b1e889..fb03bbc 100644 --- a/base/src/main/java/bjc/utils/ioutils/format/Directive.java +++ b/base/src/main/java/bjc/utils/ioutils/format/Directive.java @@ -4,11 +4,32 @@ import java.util.regex.Matcher; import bjc.utils.esodata.Tape; +/** + * A CL format directive. + * + * @author EVE + * + */ @FunctionalInterface public interface Directive { - /* - * @TODO fill in parameters + /** + * Execute this format directive. + * + * @param sb + * The buffer the string is being output to. + * @param item + * The current parameter being passed + * @param mods + * The directive modifiers + * @param arrParams + * The prefix parameters to the directive + * @param tParams + * All of the provided format parameters + * @param dirMatcher + * The matcher for format directives + * @param fmt + * The formatter itself. */ - public void format(StringBuffer sb, Object item, CLModifiers mods, - CLParameters arrParams, Tape tParams, Matcher dirMatcher, CLFormatter fmt); + public void format(StringBuffer sb, Object item, CLModifiers mods, CLParameters arrParams, Tape tParams, + Matcher dirMatcher, CLFormatter fmt); } \ No newline at end of file diff --git a/base/src/main/java/bjc/utils/ioutils/format/EscapeException.java b/base/src/main/java/bjc/utils/ioutils/format/EscapeException.java index a1df55a..086f1cd 100644 --- a/base/src/main/java/bjc/utils/ioutils/format/EscapeException.java +++ b/base/src/main/java/bjc/utils/ioutils/format/EscapeException.java @@ -1,14 +1,32 @@ package bjc.utils.ioutils.format; +/** + * An exception thrown to escape CL iteration directives. + * + * @author EVE + * + */ public class EscapeException extends RuntimeException { private static final long serialVersionUID = -4552821131068559005L; + /** + * Whether or not this exception should end iteration. + */ public final boolean endIteration; + /** + * Create a new escape exception. + */ public EscapeException() { endIteration = false; } + /** + * Create a new escape exception. + * + * @param end + * Whether or not to end the iteration. + */ public EscapeException(boolean end) { endIteration = end; } diff --git a/base/src/main/java/bjc/utils/ioutils/format/IterationDirective.java b/base/src/main/java/bjc/utils/ioutils/format/IterationDirective.java index 353ff94..e13cb1c 100644 --- a/base/src/main/java/bjc/utils/ioutils/format/IterationDirective.java +++ b/base/src/main/java/bjc/utils/ioutils/format/IterationDirective.java @@ -2,9 +2,7 @@ package bjc.utils.ioutils.format; import bjc.utils.esodata.Tape; -import java.util.ArrayList; import java.util.IllegalFormatConversionException; -import java.util.List; import java.util.regex.Matcher; class IterationDirective implements Directive { diff --git a/base/src/main/java/bjc/utils/math/DualExpr.java b/base/src/main/java/bjc/utils/math/DualExpr.java index d0e9acf..8c73f34 100644 --- a/base/src/main/java/bjc/utils/math/DualExpr.java +++ b/base/src/main/java/bjc/utils/math/DualExpr.java @@ -1,4 +1,5 @@ package bjc.utils.math; + /** * Represents an expression using dual numbers. * @@ -68,11 +69,11 @@ public class DualExpr { /** * The left (or first) part of the expression. */ - public DualExpr left; + public DualExpr left; /** * The right (or second) part of the expression. */ - public DualExpr right; + public DualExpr right; /** * The power to use, for power operations. @@ -83,7 +84,7 @@ public class DualExpr { * Create a new constant dual number. * * @param num - * The value of the dual number. + * The value of the dual number. */ public DualExpr(Dual num) { this.type = ExprType.CONSTANT; @@ -95,9 +96,9 @@ public class DualExpr { * Create a new unary dual number. * * @param type - * The type of operation to perform. + * The type of operation to perform. * @param val - * The parameter to the value. + * The parameter to the value. */ public DualExpr(DualExpr.ExprType type, DualExpr val) { this.type = type; @@ -109,9 +110,11 @@ public class DualExpr { * Create a new binary dual number. * * @param type - * The type of operation to perform. - * @param val - * The parameter to the value. + * The type of operation to perform. + * @param left + * The left hand side of the expression. + * @param right + * The right hand side of the expression. */ public DualExpr(DualExpr.ExprType type, DualExpr left, DualExpr right) { this.type = type; @@ -124,9 +127,9 @@ public class DualExpr { * Create a new power expression. * * @param left - * The expression to raise. + * The expression to raise. * @param power - * The power to raise it by. + * The power to raise it by. */ public DualExpr(DualExpr left, int power) { this.type = ExprType.POWER; @@ -148,7 +151,7 @@ public class DualExpr { Dual lval, rval; /* Perform the right operation for each type. */ - switch (type) { + switch(type) { case CONSTANT: return number; case ADDITION: @@ -176,7 +179,7 @@ public class DualExpr { rval = right.evaluate(); { - if (rval.real == 0) { + if(rval.real == 0) { throw new IllegalArgumentException("ERROR: Attempted to divide by zero."); } @@ -206,16 +209,15 @@ public class DualExpr { case LOGARITHM: lval = left.evaluate(); - if (lval.real <= 0) { - throw new IllegalArgumentException( - "ERROR: Attempted to take non-positive log."); + if(lval.real <= 0) { + throw new IllegalArgumentException("ERROR: Attempted to take non-positive log."); } return new Dual(Math.log(lval.real), lval.dual / lval.real); case POWER: lval = left.evaluate(); - if (lval.real == 0) { + if(lval.real == 0) { throw new IllegalArgumentException("ERROR: Raising zero to a power."); } @@ -239,7 +241,7 @@ public class DualExpr { @Override public String toString() { - switch (type) { + switch(type) { case ABSOLUTE: return String.format("abs(%s)", left.toString()); case ADDITION: @@ -271,58 +273,54 @@ public class DualExpr { public int hashCode() { final int prime = 31; int result = 1; - + result = prime * result + ((left == null) ? 0 : left.hashCode()); result = prime * result + ((number == null) ? 0 : number.hashCode()); result = prime * result + power; result = prime * result + ((right == null) ? 0 : right.hashCode()); result = prime * result + ((type == null) ? 0 : type.hashCode()); - + return result; } @Override public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + DualExpr other = (DualExpr) obj; - if (type != other.type) { + if(type != other.type) { return false; } - - if (left == null) { - if (other.left != null) { + + if(left == null) { + if(other.left != null) { return false; } - } else if (!left.equals(other.left)) { + } else if(!left.equals(other.left)) { return false; } - - if (number == null) { - if (other.number != null) - return false; - } else if (!number.equals(other.number)) { + + if(number == null) { + if(other.number != null) return false; + } else if(!number.equals(other.number)) { return false; } - - if (power != other.power) { + + if(power != other.power) { return false; } - - if (right == null) { - if (other.right != null) { + + if(right == null) { + if(other.right != null) { return false; } - } else if (!right.equals(other.right)) { + } else if(!right.equals(other.right)) { return false; } - + return true; } } \ No newline at end of file diff --git a/base/src/main/java/bjc/utils/math/NumberUtils.java b/base/src/main/java/bjc/utils/math/NumberUtils.java index 9391040..ed58bb8 100644 --- a/base/src/main/java/bjc/utils/math/NumberUtils.java +++ b/base/src/main/java/bjc/utils/math/NumberUtils.java @@ -27,7 +27,7 @@ public class NumberUtils { } if(currNumber >= 1000) { - int numM = (int)(currNumber / 1000); + int numM = (int) (currNumber / 1000); currNumber = currNumber % 1000; for(int i = 0; i < numM; i++) { @@ -54,7 +54,7 @@ public class NumberUtils { } if(currNumber >= 100) { - int numC = (int)(currNumber / 100); + int numC = (int) (currNumber / 100); currNumber = currNumber % 100; for(int i = 0; i < numC; i++) { @@ -81,7 +81,7 @@ public class NumberUtils { } if(currNumber >= 10) { - int numX = (int)(currNumber / 10); + int numX = (int) (currNumber / 10); currNumber = currNumber % 10; for(int i = 0; i < numX; i++) { @@ -108,7 +108,7 @@ public class NumberUtils { } if(currNumber >= 1) { - int numI = (int)(currNumber / 1); + int numI = (int) (currNumber / 1); currNumber = currNumber % 1; for(int i = 0; i < numI; i++) { @@ -118,25 +118,23 @@ public class NumberUtils { return work.toString(); } - + public static String toCardinal(long number) { return toCardinal(number, null); } - private static String[] cardinals = new String[] { - "zero", "one", "two", "three", "four", "five", "six", "seven", - "eight", "nine", "ten", "eleven", "twelve", "thirteen", - "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", - "nineteen", "twenty", - }; + private static String[] cardinals = new String[] { "zero", "one", "two", "three", "four", "five", "six", + "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", + "sixteen", "seventeen", "eighteen", "nineteen", "twenty", }; public static class CardinalState { - public final Map customNumbers; - public final Map> customScales; + public final Map customNumbers; + public final Map> customScales; - public CardinalState(Map customNumbers, Map> customScales) { + public CardinalState(Map customNumbers, + Map> customScales) { this.customNumbers = customNumbers; - this.customScales = customScales; + this.customScales = customScales; } public String handleCustom(long number) { @@ -144,7 +142,8 @@ public class NumberUtils { return customNumbers.get(number); } - for(Entry> ent : customScales.entrySet()) { + for(Entry> ent : customScales + .entrySet()) { if(ent.getKey().test(number)) { return ent.getValue().apply(number, this); } @@ -163,11 +162,11 @@ public class NumberUtils { if(number < 0) return "negative " + toCardinal(number * -1, custom); - if(number <= 20) return cardinals[(int)number]; + if(number <= 20) return cardinals[(int) number]; if(number < 100) { if(number % 10 == 0) { - switch((int)number) { + switch((int) number) { case 30: return "thirty"; case 40: @@ -183,51 +182,52 @@ public class NumberUtils { case 90: return "ninety"; default: - /* + /* * Shouldn't happen. */ - assert(false); + assert (false); } } - long numTens = (long)(number / 10); + long numTens = (long) (number / 10); long numOnes = number % 10; return toCardinal(numTens, custom) + "-" + toCardinal(numOnes, custom); } if(number < 1000) { - long numHundreds = (long)(number / 100); - long rest = number % 100; + long numHundreds = (long) (number / 100); + long rest = number % 100; return toCardinal(numHundreds, custom) + " hundred and " + toCardinal(rest, custom); } - long MILLION = (long)(Math.pow(10, 6)); + long MILLION = (long) (Math.pow(10, 6)); if(number < MILLION) { - long numThousands = (long)(number / 1000); - long rest = number % 1000; + long numThousands = (long) (number / 1000); + long rest = number % 1000; return toCardinal(numThousands, custom) + " thousand, " + toCardinal(rest, custom); } - long BILLION = (long)(Math.pow(10, 9)); + long BILLION = (long) (Math.pow(10, 9)); if(number < BILLION) { - long numMillions = (long)(number / MILLION); - long rest = number % MILLION; + long numMillions = (long) (number / MILLION); + long rest = number % MILLION; return toCardinal(numMillions, custom) + " million, " + toCardinal(rest, custom); } - long TRILLION = (long)(Math.pow(10, 12)); + long TRILLION = (long) (Math.pow(10, 12)); if(number < TRILLION) { - long numBillions = (long)(number / BILLION); - long rest = number % BILLION; + long numBillions = (long) (number / BILLION); + long rest = number % BILLION; return toCardinal(numBillions, custom) + " billion, " + toCardinal(rest, custom); } - throw new IllegalArgumentException("Numbers greater than or equal to 1 trillion are not supported yet."); + throw new IllegalArgumentException( + "Numbers greater than or equal to 1 trillion are not supported yet."); } public static String toOrdinal(long number) { @@ -236,7 +236,7 @@ public class NumberUtils { } if(number < 20) { - switch((int)number) { + switch((int) number) { case 0: return "zeroth"; case 1: @@ -281,13 +281,13 @@ public class NumberUtils { /* * Shouldn't happen. */ - assert(false); + assert (false); } } if(number < 100) { if(number % 10 == 0) { - switch((int)number) { + switch((int) number) { case 20: return "twentieth"; case 30: @@ -304,6 +304,8 @@ public class NumberUtils { return "eightieth"; case 90: return "ninetieth"; + default: + throw new IllegalArgumentException(String.format("Illegal number %d", number)); } } @@ -311,15 +313,15 @@ public class NumberUtils { return toCardinal(number - numPostfix) + "-" + toOrdinal(numPostfix); } - long procNum = number % 100; - long tens = (long)(procNum / 10); - long ones = procNum % 10; + long procNum = number % 100; + long tens = (long) (procNum / 10); + long ones = procNum % 10; if(tens == 1) { return Long.toString(number) + "th"; - } + } - switch((int)ones) { + switch((int) ones) { case 1: return Long.toString(number) + "st"; case 2: @@ -330,33 +332,36 @@ public class NumberUtils { return Long.toString(number) + "th"; } } - + private static char[] radixChars = new char[62]; static { int idx = 0; for(char i = 0; i < 10; i++) { - radixChars[idx] = (char)('0' + i); + radixChars[idx] = (char) ('0' + i); idx += 1; } for(char i = 0; i < 26; i++) { - radixChars[idx] = (char)('A' + i); + radixChars[idx] = (char) ('A' + i); idx += 1; } for(char i = 0; i < 26; i++) { - radixChars[idx] = (char)('a' + i); + radixChars[idx] = (char) ('a' + i); idx += 1; } } - public static String toCommaString(long val, int mincols, char padchar, int commaInterval, char commaChar, boolean signed, int radix) { + public static String toCommaString(long val, int mincols, char padchar, int commaInterval, char commaChar, + boolean signed, int radix) { if(radix > radixChars.length) { - throw new IllegalArgumentException(String.format("Radix %d is larger than largest supported radix %d", radix, radixChars.length)); + throw new IllegalArgumentException( + String.format("Radix %d is larger than largest supported radix %d", radix, + radixChars.length)); } StringBuilder work = new StringBuilder(); @@ -376,15 +381,16 @@ public class NumberUtils { while(currVal != 0) { valCounter += 1; - int radDigit = (int)(currVal % radix); + int radDigit = (int) (currVal % radix); work.append(radixChars[radDigit]); - currVal = (long)(currVal / radix); + currVal = (long) (currVal / radix); if(commaInterval != 0 && valCounter % commaInterval == 0) work.append(commaChar); } } - if(isNeg) work.append("-"); + if(isNeg) + work.append("-"); else if(signed) work.append("+"); work.reverse(); diff --git a/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java index b1d8597..73c3473 100644 --- a/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java +++ b/base/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java @@ -181,13 +181,13 @@ public class DelimiterGroup { /** * Check if a group is excluded at the top level of this group. * - * @param groupName + * @param grupName * The group to check. * * @return Whether or not the provided group is excluded. */ - public boolean excludes(final T groupName) { - return topLevelExclusions.contains(groupName); + public boolean excludes(final T grupName) { + return topLevelExclusions.contains(grupName); } /** -- cgit v1.2.3