diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2018-02-12 22:45:04 -0500 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2018-02-12 22:45:04 -0500 |
| commit | df94066e3af02ff02d5ab4d033a3d603f743234c (patch) | |
| tree | 168a1edaf58d386c175ffb601e9d4da8e13d31e2 /base/src/main/java/bjc/utils/cli | |
| parent | ae51c587c53f7ca311e556e3cbd0c5566d6c2843 (diff) | |
Formatting pass
Diffstat (limited to 'base/src/main/java/bjc/utils/cli')
9 files changed, 124 insertions, 137 deletions
diff --git a/base/src/main/java/bjc/utils/cli/CLICommander.java b/base/src/main/java/bjc/utils/cli/CLICommander.java index 1504002..ca41c98 100644 --- a/base/src/main/java/bjc/utils/cli/CLICommander.java +++ b/base/src/main/java/bjc/utils/cli/CLICommander.java @@ -24,18 +24,20 @@ public class CLICommander { * Create a new CLI interface powered by streams. * * @param input - * The stream to get user input from. + * The stream to get user input from. * * @param output - * The stream to send normal output to. + * The stream to send normal output to. * * @param error - * The stream to send error output to. + * The stream to send error output to. */ public CLICommander(final InputStream input, final OutputStream output, final 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.output = output; @@ -46,7 +48,7 @@ public class CLICommander { public void runCommands() { /* Setup output streams. */ final PrintStream normalOutput = new PrintStream(output); - final PrintStream errorOutput = new PrintStream(error); + final PrintStream errorOutput = new PrintStream(error); /* * Set up input streams. @@ -70,15 +72,15 @@ public class CLICommander { int comno = 1; /* * Process commands until we're told to stop, by the mode being - * set to null. + * set to null. */ - while (currentMode != null) { + while(currentMode != null) { /* * Print out the command prompt. * * Use a custom prompt if one is specified. */ - if (currentMode.isCustomPromptEnabled()) { + if(currentMode.isCustomPromptEnabled()) { normalOutput.print(currentMode.getCustomPrompt()); } else { normalOutput.printf("%s (%d)>> ", currentMode.getName(), comno); @@ -90,22 +92,21 @@ public class CLICommander { final String currentLine = inputSource.nextLine(); /* Handle commands we can handle in this mode. */ - if (currentMode.canHandle(currentLine)) { + if(currentMode.canHandle(currentLine)) { final String[] commandTokens = currentLine.split(" "); - String[] commandArgs = null; + String[] commandArgs = null; final int argCount = commandTokens.length; /* Parse args if they are present. */ - if (argCount > 1) { + if(argCount > 1) { commandArgs = Arrays.copyOfRange(commandTokens, 1, argCount); } /* Process command. */ currentMode = currentMode.process(commandTokens[0], commandArgs); } else { - errorOutput.printf("Error: Unrecognized command '%s' (no. %d)\n", - currentLine, comno); + errorOutput.printf("Error: Unrecognized command '%s' (no. %d)\n", currentLine, comno); } } @@ -116,10 +117,10 @@ public class CLICommander { * Set the initial command mode to use. * * @param initialMode - * The initial command mode to use. + * The initial command mode to use. */ public void setInitialCommandMode(final CommandMode initialMode) { - if (initialMode == null) throw new NullPointerException("Initial mode must be non-null"); + if(initialMode == null) throw new NullPointerException("Initial mode must be non-null"); this.initialMode = initialMode; } diff --git a/base/src/main/java/bjc/utils/cli/Command.java b/base/src/main/java/bjc/utils/cli/Command.java index 5969298..7451cf7 100644 --- a/base/src/main/java/bjc/utils/cli/Command.java +++ b/base/src/main/java/bjc/utils/cli/Command.java @@ -9,32 +9,28 @@ public interface Command { /** * Create a command that serves as an alias to this one * - * @return - * A command that serves as an alias to this one + * @return A command that serves as an alias to this one */ Command aliased(); /** * Get the handler that executes this command * - * @return - * The handler that executes this command + * @return The handler that executes this command */ CommandHandler getHandler(); /** * Get the help entry for this command * - * @return - * The help entry for this command + * @return The help entry for this command */ CommandHelp getHelp(); /** * Check if this command is an alias of another command * - * @return - * Whether or not this command is an alias of another + * @return Whether or not this command is an alias of another */ default boolean isAlias() { return false; diff --git a/base/src/main/java/bjc/utils/cli/CommandHandler.java b/base/src/main/java/bjc/utils/cli/CommandHandler.java index fd40aa3..6cc2d68 100644 --- a/base/src/main/java/bjc/utils/cli/CommandHandler.java +++ b/base/src/main/java/bjc/utils/cli/CommandHandler.java @@ -13,11 +13,10 @@ public interface CommandHandler extends Function<String[], CommandMode> { * Execute this command. * * @param args - * The arguments for this command. + * The arguments for this command. * - * @return - * The command mode to switch to after this command, or null to - * stop executing commands. + * @return The command mode to switch to after this command, or null to + * stop executing commands. */ default CommandMode handle(final String[] args) { return this.apply(args); diff --git a/base/src/main/java/bjc/utils/cli/CommandHelp.java b/base/src/main/java/bjc/utils/cli/CommandHelp.java index 90ee404..97d7dad 100644 --- a/base/src/main/java/bjc/utils/cli/CommandHelp.java +++ b/base/src/main/java/bjc/utils/cli/CommandHelp.java @@ -9,8 +9,7 @@ public interface CommandHelp { /** * Get the description of a command. * - * @return - * The description of a command + * @return The description of a command */ String getDescription(); @@ -25,8 +24,7 @@ public interface CommandHelp { * * where anything in angle brackets should be filled in. * - * @return - * The summary line line for a command + * @return The summary line line for a command */ String getSummary(); } diff --git a/base/src/main/java/bjc/utils/cli/CommandMode.java b/base/src/main/java/bjc/utils/cli/CommandMode.java index 0415e27..b33077b 100644 --- a/base/src/main/java/bjc/utils/cli/CommandMode.java +++ b/base/src/main/java/bjc/utils/cli/CommandMode.java @@ -11,11 +11,10 @@ public interface CommandMode extends Comparable<CommandMode> { * Check to see if this mode can handle the specified command. * * @param command - * The command to check. + * The command to check. * - * @return - * Whether or not this mode can handle the command. It is - * assumed not by default. + * @return Whether or not this mode can handle the command. It is + * assumed not by default. */ default boolean canHandle(final String command) { return false; @@ -24,11 +23,10 @@ public interface CommandMode extends Comparable<CommandMode> { /** * Get the custom prompt for this mode. * - * @return - * The custom prompt for this mode. + * @return The custom prompt for this mode. * * @throws UnsupportedOperationException - * If this mode doesn't support a custom prompt. + * If this mode doesn't support a custom prompt. */ default String getCustomPrompt() { throw new UnsupportedOperationException("This mode doesn't support a custom prompt"); @@ -37,9 +35,8 @@ public interface CommandMode extends Comparable<CommandMode> { /** * Get the name of this command mode. * - * @return - * The name of this command mode, or a default string if one isn't - * specified. + * @return The name of this command mode, or a default string if one + * isn't specified. */ public default String getName() { return "(anonymous)"; @@ -48,8 +45,7 @@ public interface CommandMode extends Comparable<CommandMode> { /** * Check if this mode uses a custom prompt. * - * @return - * Whether or not this mode uses a custom prompt. + * @return Whether or not this mode uses a custom prompt. */ default boolean isCustomPromptEnabled() { return false; @@ -59,14 +55,13 @@ public interface CommandMode extends Comparable<CommandMode> { * Process a command in this mode.. * * @param command - * The command to process. + * The command to process. * * @param args - * A list of arguments to the command. + * A list of arguments to the command. * - * @return - * The command mode to use for the next command. Defaults to doing - * nothing, and staying in the current mode. + * @return The command mode to use for the next command. Defaults to + * doing nothing, and staying in the current mode. */ default CommandMode process(final String command, final String[] args) { return this; diff --git a/base/src/main/java/bjc/utils/cli/DelegatingCommand.java b/base/src/main/java/bjc/utils/cli/DelegatingCommand.java index 9e882c2..ff981bd 100644 --- a/base/src/main/java/bjc/utils/cli/DelegatingCommand.java +++ b/base/src/main/java/bjc/utils/cli/DelegatingCommand.java @@ -13,7 +13,7 @@ class DelegatingCommand implements Command { * Create a new command that delegates to another command. * * @param delegate - * The command to delegate to. + * The command to delegate to. */ public DelegatingCommand(final Command delegate) { this.delegate = delegate; diff --git a/base/src/main/java/bjc/utils/cli/GenericCommand.java b/base/src/main/java/bjc/utils/cli/GenericCommand.java index 89539a4..bb624cd 100644 --- a/base/src/main/java/bjc/utils/cli/GenericCommand.java +++ b/base/src/main/java/bjc/utils/cli/GenericCommand.java @@ -16,23 +16,22 @@ public class GenericCommand implements Command { * Create a new generic command. * * @param handler - * The handler to use for the command. + * The handler to use for the command. * * @param description - * The description of the command. May be null, in which - * case a default is provided. + * The description of the command. May be null, in which case a + * default is provided. * * @param help - * The detailed help message for the command. May be - * null, in which case the description is repeated for - * the detailed help. + * The detailed help message for the command. May be null, in + * which case the description is repeated for the detailed help. */ public GenericCommand(final CommandHandler handler, final String description, final String help) { - if (handler == null) throw new NullPointerException("Command handler must not be null"); + if(handler == null) throw new NullPointerException("Command handler must not be null"); this.handler = handler; - if (description == null) { + if(description == null) { this.help = new NullHelp(); } else { this.help = new GenericHelp(description, help); diff --git a/base/src/main/java/bjc/utils/cli/GenericCommandMode.java b/base/src/main/java/bjc/utils/cli/GenericCommandMode.java index e24a17b..a642fe8 100644 --- a/base/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/base/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -29,8 +29,8 @@ public class GenericCommandMode implements CommandMode { private BiConsumer<String, String[]> unknownCommandHandler; /* The functions to use for input/output */ - private final Consumer<String> errorOutput; - private final Consumer<String> normalOutput; + private final Consumer<String> errorOutput; + private final Consumer<String> normalOutput; /* The name of this command mode, or null if it is unnamed */ private String modeName; @@ -42,22 +42,23 @@ public class GenericCommandMode implements CommandMode { * Create a new generic command mode * * @param normalOutput - * The function to use for normal output. + * The function to use for normal output. * * @param errorOutput - * The function to use for error output. + * The function to use for error output. */ public GenericCommandMode(final Consumer<String> normalOutput, final Consumer<String> errorOutput) { - 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"); + 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; + this.errorOutput = errorOutput; /* Initialize 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<>()); /* Setup default commands. */ setupDefaultCommands(); @@ -67,25 +68,25 @@ public class GenericCommandMode implements CommandMode { * Add an alias to an existing command. * * @param commandName - * The name of the command to add an alias for. + * The name of the command to add an alias for. * * @param aliasName - * The new alias for the command. + * The new alias for the command. * * @throws IllegalArgumentException - * If the specified command doesn't have a bound handler, or if the - * alias name already has a bound value. + * If the specified command doesn't have a bound handler, or if + * the alias name already has a bound value. */ public void addCommandAlias(final String commandName, final String aliasName) { - if (commandName == null) { + if(commandName == null) { throw new NullPointerException("Command name must not be null"); - } else if (aliasName == null) { + } else if(aliasName == null) { String msg = "Alias name must not be null"; throw new NullPointerException(msg); - } else if (!commandHandlers.containsKey(commandName) && !defaultHandlers.containsKey(commandName)) { + } else if(!commandHandlers.containsKey(commandName) && !defaultHandlers.containsKey(commandName)) { String msg = String.format("Cannot alias non-existant command '%s'", commandName); throw new IllegalArgumentException(msg); - } else if (commandHandlers.containsKey(aliasName) || defaultHandlers.containsKey(aliasName)) { + } else if(commandHandlers.containsKey(aliasName) || defaultHandlers.containsKey(aliasName)) { String msg = String.format("Cannot bind alias '%s' to an already bound command.", aliasName); throw new IllegalArgumentException(msg); } else { @@ -93,7 +94,7 @@ public class GenericCommandMode implements CommandMode { Command aliasedCommand; /* Get the alias. */ - if (defaultHandlers.containsKey(commandName)) { + if(defaultHandlers.containsKey(commandName)) { aliasedCommand = defaultHandlers.get(commandName).aliased(); } else { aliasedCommand = commandHandlers.get(commandName).aliased(); @@ -107,20 +108,20 @@ public class GenericCommandMode implements CommandMode { * Add a command to this command mode. * * @param command - * The name of the command to add. + * The name of the command to add. * * @param handler - * The handler to use for the specified command. + * The handler to use for the specified command. * * @throws IllegalArgumentException - * If the specified command already has a handler registered. + * If the specified command already has a handler registered. */ public void addCommandHandler(final String command, final Command handler) { - if (command == null) { + if(command == null) { throw new NullPointerException("Command must not be null"); - } else if (handler == null) { + } else if(handler == null) { throw new NullPointerException("Handler must not be null"); - } else if (canHandle(command)) { + } else if(canHandle(command)) { String msg = String.format("Command '%s' already has a registered handler"); throw new IllegalArgumentException(msg); } else { @@ -132,10 +133,10 @@ public class GenericCommandMode implements CommandMode { * Add a help topic to this command mode that isn't tied to a command. * * @param topicName - * The name of the topic. + * The name of the topic. * * @param topic - * The contents of the topic. + * The contents of the topic. */ public void addHelpTopic(final String topicName, final CommandHelp topic) { helpTopics.put(topicName, topic); @@ -143,9 +144,8 @@ public class GenericCommandMode implements CommandMode { /* Default command builders */ /* - * @TODO 10/09/17 Ben Culkin :CommandExtraction - * These command messages should be extracted into some kind of - * file-based abstraction. + * @TODO 10/09/17 Ben Culkin :CommandExtraction These command messages + * should be extracted into some kind of file-based abstraction. */ private GenericCommand buildAliasCommand() { final String aliasShortHelp = "alias\tAlias one command to another"; @@ -191,7 +191,7 @@ public class GenericCommandMode implements CommandMode { + " 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) { + if(args == null || args.length == 0) { /* Invoke general help */ doHelpSummary(); } else { @@ -222,18 +222,19 @@ public class GenericCommandMode implements CommandMode { /* Implement default commands */ private void doAliasCommands(final String[] args) { - if (args.length != 2) { - String msg = String.format("ERROR: Alias requires two arguments. The command name, and the alias for that command. "); + if(args.length != 2) { + String msg = String.format( + "ERROR: Alias requires two arguments. The command name, and the alias for that command. "); errorOutput.accept(msg); } else { final String commandName = args[0]; final String aliasName = args[1]; - if (!canHandle(commandName)) { + if(!canHandle(commandName)) { String msg = String.format("ERROR: '%s' is not a valid command.", commandName); errorOutput.accept(msg); - } else if (canHandle(aliasName)) { + } else if(canHandle(aliasName)) { String msg = String.format("ERROR: Cannot overwrite command '%s'", aliasName); errorOutput.accept(msg); @@ -244,15 +245,15 @@ public class GenericCommandMode implements CommandMode { } private void doHelpCommand(final String commandName) { - if (commandHandlers.containsKey(commandName)) { + if(commandHandlers.containsKey(commandName)) { final String desc = commandHandlers.get(commandName).getHelp().getDescription(); normalOutput.accept("\n" + desc); - } else if (defaultHandlers.containsKey(commandName)) { + } else if(defaultHandlers.containsKey(commandName)) { final String desc = defaultHandlers.get(commandName).getHelp().getDescription(); normalOutput.accept("\n" + desc); - } else if (helpTopics.containsKey(commandName)) { + } else if(helpTopics.containsKey(commandName)) { normalOutput.accept("\n" + helpTopics.get(commandName).getDescription()); } else { String msg = String.format("ERROR: No help available for '%s'", commandName); @@ -264,11 +265,11 @@ public class GenericCommandMode implements CommandMode { private void doHelpSummary() { normalOutput.accept("Help topics for this command mode are as follows:\n"); - if (commandHandlers.size() > 0) { + if(commandHandlers.size() > 0) { commandHandlers.forEachValue(command -> { - if (!command.isAlias()) { + if(!command.isAlias()) { String comLine = command.getHelp().getSummary(); - String msg = String.format("\t%s\n", comLine); + String msg = String.format("\t%s\n", comLine); normalOutput.accept(msg); } @@ -278,16 +279,15 @@ public class GenericCommandMode implements CommandMode { } normalOutput.accept("\nHelp topics available in all command modes are as follows\n"); - if (defaultHandlers.size() > 0) { + if(defaultHandlers.size() > 0) { /* - * @NOTE - * This block here should be abstracted out into a - * method. + * @NOTE This block here should be abstracted out into a + * method. */ defaultHandlers.forEachValue(command -> { - if (!command.isAlias()) { + if(!command.isAlias()) { String comLine = command.getHelp().getSummary(); - String msg = String.format("\t%s\n", comLine); + String msg = String.format("\t%s\n", comLine); normalOutput.accept(msg); } @@ -297,7 +297,7 @@ public class GenericCommandMode implements CommandMode { } normalOutput.accept("\nHelp topics not associated with a command are as follows\n"); - if (helpTopics.size() > 0) { + if(helpTopics.size() > 0) { helpTopics.forEachValue(topic -> { String msg = String.format("\t%s\n", topic.getSummary()); @@ -329,14 +329,14 @@ public class GenericCommandMode implements CommandMode { @Override public String getCustomPrompt() { - if (customPrompt != null) return customPrompt; + if(customPrompt != null) return customPrompt; return CommandMode.super.getCustomPrompt(); } @Override public String getName() { - if (modeName != null) return modeName; + if(modeName != null) return modeName; return CommandMode.super.getName(); } @@ -350,15 +350,15 @@ public class GenericCommandMode implements CommandMode { public CommandMode process(final String command, final String[] args) { normalOutput.accept("\n"); - if (defaultHandlers.containsKey(command)) + if(defaultHandlers.containsKey(command)) return defaultHandlers.get(command).getHandler().handle(args); - else if (commandHandlers.containsKey(command)) + else if(commandHandlers.containsKey(command)) return commandHandlers.get(command).getHandler().handle(args); else { - if (args != null) { + if(args != null) { String argString = String.join(", ", args); - String msg = String.format("ERROR: Unrecognized command %s (arguments %s)", - command, argString); + String msg = String.format("ERROR: Unrecognized command %s (arguments %s)", command, + argString); errorOutput.accept(msg); } else { @@ -367,7 +367,7 @@ public class GenericCommandMode implements CommandMode { errorOutput.accept(msg); } - if (unknownCommandHandler == null) { + if(unknownCommandHandler == null) { String msg = String.format("Command %s is invalid", command); throw new UnsupportedOperationException(msg); @@ -383,8 +383,8 @@ public class GenericCommandMode implements CommandMode { * Set the custom prompt for this mode * * @param prompt - * The custom prompt for this mode, or null to disable the custom - * prompt + * The custom prompt for this mode, or null to disable the custom + * prompt */ public void setCustomPrompt(final String prompt) { customPrompt = prompt; @@ -394,7 +394,7 @@ public class GenericCommandMode implements CommandMode { * Set the name of this mode * * @param name - * The desired name of this mode, or null to use the default name + * The desired name of this mode, or null to use the default name */ public void setModeName(final String name) { modeName = name; @@ -404,29 +404,28 @@ public class GenericCommandMode implements CommandMode { * Set the handler to use for unknown commands * * @param handler - * The handler to use for unknown commands, or null to throw on - * unknown commands + * The handler to use for unknown commands, or null to throw on + * unknown commands */ public void setUnknownCommandHandler(final BiConsumer<String, String[]> handler) { - if (handler == null) throw new NullPointerException("Handler must not be null"); + if(handler == null) throw new NullPointerException("Handler must not be null"); unknownCommandHandler = handler; } /* Setup default commands. */ private void setupDefaultCommands() { - defaultHandlers.put("list", buildListCommand()); + defaultHandlers.put("list", buildListCommand()); defaultHandlers.put("alias", buildAliasCommand()); - defaultHandlers.put("help", buildHelpCommand()); + defaultHandlers.put("help", buildHelpCommand()); /* Help unix people :) */ addCommandAlias("help", "man"); /* Add commands handled in a upper layer. */ /* - * @NOTE - * Figure out a place to put commands that apply across all - * modes, but only to a specific application. + * @NOTE Figure out a place to put commands that apply across + * all modes, but only to a specific application. */ defaultHandlers.put("clear", buildClearCommands()); defaultHandlers.put("exit", buildExitCommand()); @@ -442,30 +441,30 @@ public class GenericCommandMode implements CommandMode { final StringBuilder builder = new StringBuilder(); builder.append("GenericCommandMode ["); - if (commandHandlers != null) { + if(commandHandlers != null) { builder.append("commandHandlers="); builder.append(commandHandlers); } - if (defaultHandlers != null) { + if(defaultHandlers != null) { builder.append(", "); builder.append("defaultHandlers="); builder.append(defaultHandlers); } - if (helpTopics != null) { + if(helpTopics != null) { builder.append(", "); builder.append("helpTopics="); builder.append(helpTopics); } - if (modeName != null) { + if(modeName != null) { builder.append(", "); builder.append("modeName="); builder.append(modeName); } - if (customPrompt != null) { + if(customPrompt != null) { builder.append(", "); builder.append("customPrompt="); builder.append(customPrompt); diff --git a/base/src/main/java/bjc/utils/cli/GenericHelp.java b/base/src/main/java/bjc/utils/cli/GenericHelp.java index 92c1eef..9fca3a9 100644 --- a/base/src/main/java/bjc/utils/cli/GenericHelp.java +++ b/base/src/main/java/bjc/utils/cli/GenericHelp.java @@ -14,24 +14,24 @@ public class GenericHelp implements CommandHelp { * Create a new help topic. * * @param summary - * The summary of this help topic. + * The summary of this help topic. * * @param description - * The description of this help topic, or null if this help topic - * doesn't have a more detailed description. + * The description of this help topic, or null if this help topic + * doesn't have a more detailed description. */ public GenericHelp(final String summary, final String description) { - if (summary == null) { + if(summary == null) { throw new NullPointerException("Help summary must be non-null"); } - this.summary = summary; + this.summary = summary; this.description = description; } @Override public String getDescription() { - if (description == null) { + if(description == null) { return summary; } @@ -49,12 +49,12 @@ public class GenericHelp implements CommandHelp { builder.append("GenericHelp ["); - if (summary != null) { + if(summary != null) { builder.append("summary="); builder.append(summary); } - if (description != null) { + if(description != null) { builder.append(", "); builder.append("description="); builder.append(description); |
