diff options
10 files changed, 166 insertions, 119 deletions
diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java index 1e0a1df..5e42b8f 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/BinarySearchTest.java @@ -61,13 +61,10 @@ public class BinarySearchTest { */ public static void main(String[] args) { Scanner input = new Scanner(System.in); - System.out.println("Binary Tree Constructor/Searcher"); - - char command = ' '; - BinarySearchTree<Character> tree = new BinarySearchTree<>((o1, o2) -> o1 - o2); + char command = ' '; while(command != 'e') { System.out.print("Enter a command (m for help): "); command = input.nextLine().charAt(0); diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java index e228765..5f84d6b 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/ShuntTest.java @@ -26,6 +26,7 @@ public class ShuntTest { String line = inputSource.nextLine(); ShuntingYard<String> yard = new ShuntingYard<>(true); + IList<String> preTokens = new FunctionalStringTokenizer(line).toList(strang -> strang); IList<String> shuntedTokens = yard.postfix(preTokens, strang -> strang); diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java index 58a6a5c..27a2c3e 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/parsing/TreeConstructTest.java @@ -29,8 +29,7 @@ public class TreeConstructTest { private static final class OperatorPicker implements Predicate<String> { @Override public boolean test(String token) { - if(StringUtils.containsOnly(token, "\\[")) - return true; + if(StringUtils.containsOnly(token, "\\[")) return true; else if(StringUtils.containsOnly(token, "\\]")) return true; switch(token) { @@ -87,14 +86,14 @@ public class TreeConstructTest { System.out.println("Shunted: " + shuntedTokens.toString()); Predicate<String> specialPicker = (operator) -> { - if(StringUtils.containsOnly(operator, "\\[")) - return true; + if(StringUtils.containsOnly(operator, "\\[")) return true; else if(StringUtils.containsOnly(operator, "\\]")) return true; return false; }; - IMap<String, Function<Deque<ITree<String>>, ITree<String>>> operators = new FunctionalMap<>(); + IMap<String, Function<Deque<ITree<String>>, ITree<String>>> operators + = new FunctionalMap<>(); operators.put("[", (queuedTrees) -> { return null; @@ -120,8 +119,8 @@ public class TreeConstructTest { return arrayTree; }); - ITree<String> constructedTree = TreeConstructor.constructTree(shuntedTokens, new OperatorPicker(), - specialPicker, operators::get); + ITree<String> constructedTree = TreeConstructor.constructTree(shuntedTokens, + new OperatorPicker(), specialPicker, operators::get); System.out.println("AST: " + constructedTree.toString()); diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java index c83c86e..2c84bcc 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java @@ -14,7 +14,8 @@ public class DiabloItemGen { private static WeightedGrammar<String> rules = new WeightedGrammar<>(); private static void addCase(String ruleName, int probability, String ruleParts) { - IList<String> parts = FunctionalStringTokenizer.fromString(ruleParts).toList(strang -> strang); + IList<String> parts = FunctionalStringTokenizer.fromString(ruleParts) + .toList(strang -> strang); rules.addCase(ruleName, probability, parts); } diff --git a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java index c490107..8546d2e 100644 --- a/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java +++ b/BJC-Utils2/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java @@ -18,7 +18,8 @@ public class RandomStringExamples { IList<IList<String>> cses = new FunctionalList<>(); for(String strang : cases) { - IList<String> lst = FunctionalStringTokenizer.fromString(strang).toList(s -> s); + IList<String> lst = FunctionalStringTokenizer.fromString(strang) + .toList(s -> s); cses.add(lst); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java b/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java index b732f01..9fff1ac 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java @@ -14,14 +14,14 @@ import java.util.Scanner; */ public class CLICommander { /* - * The streams used for input and normal/error output + * The streams used for input and normal/error output. */ private InputStream input; private OutputStream output; private OutputStream error; /* - * The command mode to start execution in + * The command mode to start execution in. */ private ICommandMode initialMode; @@ -36,30 +36,30 @@ public class CLICommander { * The stream to send error output to. */ public CLICommander(InputStream input, OutputStream output, OutputStream error) { - if(input == null) - throw new NullPointerException("Input stream must not be null"); - else if(output == null) - throw new NullPointerException("Output stream must not be null"); - else if(error == null) throw new NullPointerException("Error stream must not be null"); + if(input == null) throw new NullPointerException("Input stream must not be null"); + else if(output == null) throw new NullPointerException("Output stream must not be null"); + else if(error == null) throw new NullPointerException("Error stream must not be null"); - this.input = input; + this.input = input; this.output = output; - this.error = error; + this.error = error; } /** * Start handling commands from the given input stream. */ public void runCommands() { - // Setup output streams + /* + * Setup output streams. + */ PrintStream normalOutput = new PrintStream(output); - PrintStream errorOutput = new PrintStream(error); + PrintStream errorOutput = new PrintStream(error); /* * Set up input streams. * * We're suppressing the warning because we might use the input - * stream multiple times + * stream multiple times. */ @SuppressWarnings("resource") Scanner inputSource = new Scanner(input); @@ -67,15 +67,17 @@ public class CLICommander { /* * The mode currently being used to handle commands. * - * Used to preserve the initial mode + * Used to preserve the initial mode. */ ICommandMode currentMode = initialMode; - // Process commands until we're told to stop + /* + * Process commands until we're told to stop. + */ while(currentMode != null) { /* * Print out the command prompt, using a custom prompt - * if one is specified + * if one is specified. */ if(currentMode.isCustomPromptEnabled()) { normalOutput.print(currentMode.getCustomPrompt()); @@ -83,20 +85,29 @@ public class CLICommander { normalOutput.print(currentMode.getName() + ">> "); } - // Read in a command + /* + * Read in a command. + */ String currentLine = inputSource.nextLine(); - // Handle commands we can handle + /* + * Handle commands we can handle. + */ if(currentMode.canHandle(currentLine)) { String[] commandTokens = currentLine.split(" "); - String[] commandArgs = null; - - // Parse args if they are present - if(commandTokens.length > 1) { - commandArgs = Arrays.copyOfRange(commandTokens, 1, commandTokens.length); + String[] commandArgs = null; + int argCount = commandTokens.length; + + /* + * Parse args if they are present. + */ + if(argCount > 1) { + commandArgs = Arrays.copyOfRange(commandTokens, 1, argCount); } - // Process command + /* + * Process command. + */ currentMode = currentMode.process(commandTokens[0], commandArgs); } else { errorOutput.print("Error: Unrecognized command " + currentLine); @@ -107,10 +118,10 @@ public class CLICommander { } /** - * Set the initial command mode to use + * Set the initial command mode to use. * * @param initialMode - * The initial command mode to use + * The initial command mode to use. */ public void setInitialCommandMode(ICommandMode initialMode) { if(initialMode == null) throw new NullPointerException("Initial mode must be non-zero"); diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java index ddad5e2..b6f7a87 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java @@ -1,20 +1,22 @@ package bjc.utils.cli; /** - * A class for a command that delegates to another command + * A class for a command that delegates to another command. * * @author ben * */ class DelegatingCommand implements ICommand { - // The command to delegate to + /* + * The command to delegate to. + */ private ICommand delegate; /** - * Create a new command that delegates to another command + * Create a new command that delegates to another command. * * @param delegate - * The command to delegate to + * The command to delegate to. */ public DelegatingCommand(ICommand delegate) { this.delegate = delegate; diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java index 982cf48..f2dae7d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -24,20 +24,30 @@ public class GenericCommandMode implements ICommandMode { private IMap<String, ICommand> commandHandlers; private IMap<String, ICommand> defaultHandlers; - // Contains help topics without an associated command + /* + * Contains help topics without an associated command + */ private IMap<String, ICommandHelp> helpTopics; - // The action to execute upon encountering an unknown command + /* + * The action to execute upon encountering an unknown command + */ private BiConsumer<String, String[]> unknownCommandHandler; - // The functions to use for input/output + /* + * The functions to use for input/output + */ private Consumer<String> errorOutput; private Consumer<String> normalOutput; - // The name of this command mode, or null if it is unnamed + /* + * The name of this command mode, or null if it is unnamed + */ private String modeName; - // The custom prompt to use, or null if none is specified + /* + * The custom prompt to use, or null if none is specified + */ private String customPrompt; /** @@ -49,18 +59,21 @@ public class GenericCommandMode implements ICommandMode { * The function to use for error output */ public GenericCommandMode(Consumer<String> normalOutput, Consumer<String> errorOutput) { - if(normalOutput == null) - throw new NullPointerException("Normal output source must be non-null"); + if(normalOutput == null) throw new NullPointerException("Normal output source must be non-null"); else if(errorOutput == null) throw new NullPointerException("Error output source must be non-null"); this.normalOutput = normalOutput; - this.errorOutput = errorOutput; - - // Initialize handler maps so that they sort in alphabetical - // order + this.errorOutput = errorOutput; + + /* + * Initialize handler maps so that they sort in alphabetical + */ + /* + * order + */ commandHandlers = new FunctionalMap<>(new TreeMap<>()); defaultHandlers = new FunctionalMap<>(new TreeMap<>()); - helpTopics = new FunctionalMap<>(new TreeMap<>()); + helpTopics = new FunctionalMap<>(new TreeMap<>()); setupDefaultCommands(); } @@ -144,8 +157,8 @@ public class GenericCommandMode implements ICommandMode { private GenericCommand buildAliasCommand() { String aliasShortHelp = "alias\tAlias one command to another"; String aliasLongHelp = "Gives a command another name it can be invoked by." - + " Invoke with two arguments: the name of the command to alias" - + "followed by the name of the alias to give that command."; + + " Invoke with two arguments: the name of the command to alias" + + "followed by the name of the alias to give that command."; return new GenericCommand((args) -> { doAliasCommands(args); @@ -168,7 +181,7 @@ public class GenericCommandMode implements ICommandMode { private GenericCommand buildExitCommand() { String exitShortHelp = "exit\tExit the console"; String exitLongHelp = "First prompts the user to make sure they want to" - + " exit, then quits if they say they do"; + + " exit, then quits if they say they do"; return new GenericCommand((args) -> { errorOutput.accept("ERROR: This console doesn't support auto-exiting"); @@ -180,16 +193,20 @@ public class GenericCommandMode implements ICommandMode { private GenericCommand buildHelpCommand() { String helpShortHelp = "help\tConsult the help system"; String helpLongHelp = "Consults the internal help system." - + " Invoked in two different ways. Invoking with no arguments" - + " causes all the topics you can ask for details on to be list," - + " while invoking with the name of a topic will print the entry" + " for that topic"; + + " Invoked in two different ways. Invoking with no arguments" + + " causes all the topics you can ask for details on to be list," + + " while invoking with the name of a topic will print the entry" + " for that topic"; return new GenericCommand((args) -> { if(args == null || args.length == 0) { - // Invoke general help + /* + * Invoke general help + */ doHelpSummary(); } else { - // Invoke help for a command + /* + * Invoke help for a command + */ doHelpCommand(args[0]); } @@ -200,7 +217,7 @@ public class GenericCommandMode implements ICommandMode { private GenericCommand buildListCommand() { String listShortHelp = "list\tList available commands"; String listLongHelp = "Lists all of the commands available in this mode," - + " as well as commands available in any mode"; + + " as well as commands available in any mode"; return new GenericCommand((args) -> { doListCommands(); @@ -387,11 +404,19 @@ public class GenericCommandMode implements ICommandMode { addCommandAlias("help", "man"); - // Add commands handled in a upper layer. - - // @TODO figure out a place to put commands that apply across - // all - // modes, but only apply to a specific application + /* + * Add commands handled in a upper layer. + */ + + /* + * @TODO figure out a place to put commands that apply across + */ + /* + * all + */ + /* + * modes, but only apply to a specific application + */ defaultHandlers.put("clear", buildClearCommands()); defaultHandlers.put("exit", buildExitCommand()); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/DoubleMatcher.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/DoubleMatcher.java index 8d0715d..03227ed 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/DoubleMatcher.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/DoubleMatcher.java @@ -17,56 +17,55 @@ class DoubleMatcher { */ private static final String Exp = "[eE][+-]?" + Digits; - private static final String fpRegex = "[\\x00-\\x20]*" + // Optional - // leading - // "whitespace" - "[+-]?(" + // Optional sign character - "NaN|" + // "NaN" string - "Infinity|" + // "Infinity" string + private static final String fpRegex = + "[\\x00-\\x20]*" // Optional leading "whitespace" + + "[+-]?(" + // Optional sign character + "NaN|" + // "NaN" string + "Infinity|" + // "Infinity" string - /* - * A decimal floating-point string representing a finite - * positive number without a leading sign has at most - * five basic pieces: Digits . Digits ExponentPart - * FloatTypeSuffix - * - * Since this method allows integer-only strings as - * input in addition to strings of floating-point - * literals, the two sub-patterns below are - * simplifications of the grammar productions from - * section 3.10.2 of The Java™ Language Specification. - */ + /* + * A decimal floating-point string representing a finite + * positive number without a leading sign has at most + * five basic pieces: Digits . Digits ExponentPart + * FloatTypeSuffix + * + * Since this method allows integer-only strings as + * input in addition to strings of floating-point + * literals, the two sub-patterns below are + * simplifications of the grammar productions from + * section 3.10.2 of The Java™ Language Specification. + */ - /* - * Digits ._opt Digits_opt ExponentPart_opt - * FloatTypeSuffix_opt - */ - "(((" + Digits + "(\\.)?(" + Digits + "?)(" + Exp + ")?)|" + + /* + * Digits ._opt Digits_opt ExponentPart_opt + * FloatTypeSuffix_opt + */ + "(((" + Digits + "(\\.)?(" + Digits + "?)(" + Exp + ")?)|" + - /* - * . Digits ExponentPart_opt FloatTypeSuffix_opt - */ - "(\\.(" + Digits + ")(" + Exp + ")?)|" + + /* + * . Digits ExponentPart_opt FloatTypeSuffix_opt + */ + "(\\.(" + Digits + ")(" + Exp + ")?)|" + - /* - * Hexadecimal strings - */ - "((" + - /* - * 0[xX] HexDigits ._opt BinaryExponent - * FloatTypeSuffix_opt - */ - "(0[xX]" + HexDigits + "(\\.)?)|" + + /* + * Hexadecimal strings + */ + "((" + + /* + * 0[xX] HexDigits ._opt BinaryExponent + * FloatTypeSuffix_opt + */ + "(0[xX]" + HexDigits + "(\\.)?)|" + - /* - * 0[xX] HexDigits_opt . HexDigits BinaryExponent - * FloatTypeSuffix_opt - */ - "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + + /* + * 0[xX] HexDigits_opt . HexDigits BinaryExponent + * FloatTypeSuffix_opt + */ + "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + - ")[pP][+-]?" + Digits + "))" + "[fFdD]?))" + "[\\x00-\\x20]*"; // Optional - // trailing - // "whitespace" + ")[pP][+-]?" + Digits + "))" + "[fFdD]?))" + "[\\x00-\\x20]*"; // Optional + // trailing + // "whitespace" public static final Pattern floatingLiteral = Pattern.compile("\\A" + fpRegex + "\\Z"); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/NeoTokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/NeoTokenSplitter.java index 25b1e03..9da457e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/NeoTokenSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/NeoTokenSplitter.java @@ -11,8 +11,12 @@ public class NeoTokenSplitter { /* * This string is a format template for the delimiter matching regex * - * It does two things 1. Match the provided delimiter by positive - * lookahead 2. Match the provided delimiter by positive lookbehind + * It does two things: + * + * <ol> + * <li> Match to the left of the provided delimiter by positive lookahead </li> + * <li> Match to the right of the provided delimiter by positive lookbehind </li> + * </ol> * * Thus, it will only match in places where the delimiter is, but won't * actually match the delimiter, leaving split to put it into the stream @@ -29,9 +33,17 @@ public class NeoTokenSplitter { */ private static String WITH_MULTI_DELIM = "((?<=%1$s+)(?!%1$s)|(?<!%1$s)(?=%1$s+))"; + /* + * These represent the internal state of the splitter. + */ private StringBuilder currPatt; private StringBuilder currExclusionPatt; + /* + * These represent the external state of the splitter. + * + * Compilation causes internal to become external. + */ private Pattern compPatt; private Pattern exclusionPatt; @@ -78,10 +90,10 @@ public class NeoTokenSplitter { */ public void addDelimiter(String delim) { String quoteDelim = Pattern.quote(delim); - String delimPat = String.format(WITH_DELIM, quoteDelim); + String delimPat = String.format(WITH_DELIM, quoteDelim); if(currPatt == null) { - currPatt = new StringBuilder(); + currPatt = new StringBuilder(); currExclusionPatt = new StringBuilder(); currPatt.append("(?:" + delimPat + ")"); @@ -105,7 +117,7 @@ public class NeoTokenSplitter { String delimPat = String.format(WITH_MULTI_DELIM, "(?:" + delim + ")"); if(currPatt == null) { - currPatt = new StringBuilder(); + currPatt = new StringBuilder(); currExclusionPatt = new StringBuilder(); currPatt.append("(?:" + delimPat + ")"); @@ -133,14 +145,13 @@ public class NeoTokenSplitter { currExclusionPatt.append("|(?:" + delim + ")"); } } - /** * Compiles the current set of delimiters to a pattern. * * Makes this splitter ready to use. */ public void compile() { - compPatt = Pattern.compile(currPatt.toString()); + compPatt = Pattern.compile(currPatt.toString()); exclusionPatt = Pattern.compile(currExclusionPatt.toString()); } } |
