From 504ca816530efdff06bc202e0432ebd354aec304 Mon Sep 17 00:00:00 2001 From: EVE Date: Tue, 14 Mar 2017 12:07:14 -0400 Subject: Cleanup --- .../src/main/java/bjc/utils/cli/CLICommander.java | 36 +++---- .../main/java/bjc/utils/cli/DelegatingCommand.java | 4 +- .../main/java/bjc/utils/cli/GenericCommand.java | 10 +- .../java/bjc/utils/cli/GenericCommandMode.java | 109 ++++++++++----------- .../src/main/java/bjc/utils/cli/GenericHelp.java | 16 ++- .../src/main/java/bjc/utils/cli/ICommand.java | 10 +- .../main/java/bjc/utils/cli/ICommandHandler.java | 4 +- .../src/main/java/bjc/utils/cli/ICommandHelp.java | 8 +- .../src/main/java/bjc/utils/cli/ICommandMode.java | 14 +-- .../src/main/java/bjc/utils/cli/NullHelp.java | 2 +- .../src/main/java/bjc/utils/cli/package-info.java | 2 +- 11 files changed, 98 insertions(+), 117 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli') 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 22fb276..b732f01 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/CLICommander.java @@ -8,7 +8,7 @@ import java.util.Scanner; /** * Runs a CLI interface from the provided set of streams. - * + * * @author ben * */ @@ -16,9 +16,9 @@ public class CLICommander { /* * The streams used for input and normal/error output */ - private InputStream input; - private OutputStream output; - private OutputStream error; + private InputStream input; + private OutputStream output; + private OutputStream error; /* * The command mode to start execution in @@ -27,7 +27,7 @@ public class CLICommander { /** * Create a new CLI interface powered by streams. - * + * * @param input * The stream to get user input from. * @param output @@ -36,13 +36,11 @@ public class CLICommander { * The stream to send error output to. */ public CLICommander(InputStream input, OutputStream output, OutputStream error) { - if (input == null) { + if(input == null) throw new NullPointerException("Input stream must not be null"); - } else if (output == 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"); - } + else if(error == null) throw new NullPointerException("Error stream must not be null"); this.input = input; this.output = output; @@ -59,7 +57,7 @@ public class CLICommander { /* * Set up input streams. - * + * * We're suppressing the warning because we might use the input * stream multiple times */ @@ -68,18 +66,18 @@ public class CLICommander { /* * The mode currently being used to handle commands. - * + * * Used to preserve the initial mode */ ICommandMode currentMode = initialMode; // Process commands until we're told to stop - while (currentMode != null) { + while(currentMode != null) { /* * Print out the command prompt, using a custom prompt * if one is specified */ - if (currentMode.isCustomPromptEnabled()) { + if(currentMode.isCustomPromptEnabled()) { normalOutput.print(currentMode.getCustomPrompt()); } else { normalOutput.print(currentMode.getName() + ">> "); @@ -89,12 +87,12 @@ public class CLICommander { String currentLine = inputSource.nextLine(); // Handle commands we can handle - if (currentMode.canHandle(currentLine)) { + if(currentMode.canHandle(currentLine)) { String[] commandTokens = currentLine.split(" "); String[] commandArgs = null; // Parse args if they are present - if (commandTokens.length > 1) { + if(commandTokens.length > 1) { commandArgs = Arrays.copyOfRange(commandTokens, 1, commandTokens.length); } @@ -110,14 +108,12 @@ public class CLICommander { /** * Set the initial command mode to use - * + * * @param initialMode * The initial command mode to use */ public void setInitialCommandMode(ICommandMode initialMode) { - if (initialMode == null) { - throw new NullPointerException("Initial mode must be non-zero"); - } + if(initialMode == null) throw new NullPointerException("Initial mode must be non-zero"); this.initialMode = initialMode; } 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 7d2f807..ddad5e2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java @@ -2,7 +2,7 @@ package bjc.utils.cli; /** * A class for a command that delegates to another command - * + * * @author ben * */ @@ -12,7 +12,7 @@ class DelegatingCommand implements ICommand { /** * Create a new command that delegates to another command - * + * * @param delegate * The command to delegate to */ diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java index b0ceb3b..4dde938 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java @@ -2,7 +2,7 @@ package bjc.utils.cli; /** * Generic command implementation - * + * * @author ben * */ @@ -15,7 +15,7 @@ public class GenericCommand implements ICommand { /** * Create a new generic command - * + * * @param handler * The handler to use for the command * @param description @@ -24,13 +24,11 @@ public class GenericCommand implements ICommand { * The detailed help message for the command. May be null */ public GenericCommand(ICommandHandler handler, String description, 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/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java index 62d0008..982cf48 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -1,19 +1,19 @@ package bjc.utils.cli; +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IMap; + import java.util.TreeMap; import java.util.function.BiConsumer; import java.util.function.Consumer; -import bjc.utils.funcdata.FunctionalMap; -import bjc.utils.funcdata.IMap; - /** * A general command mode, with a customizable set of commands - * + * * There is a small set of commands which is handled by default. The first is * 'list', which lists all the commands the user can input. The second is * 'alias', which allows the user to bind a new name to a command - * + * * @author ben * */ @@ -21,8 +21,8 @@ public class GenericCommandMode implements ICommandMode { /* * Contains the commands this mode handles */ - private IMap commandHandlers; - private IMap defaultHandlers; + private IMap commandHandlers; + private IMap defaultHandlers; // Contains help topics without an associated command private IMap helpTopics; @@ -31,8 +31,8 @@ public class GenericCommandMode implements ICommandMode { private BiConsumer unknownCommandHandler; // The functions to use for input/output - private Consumer errorOutput; - private Consumer normalOutput; + private Consumer errorOutput; + private Consumer normalOutput; // The name of this command mode, or null if it is unnamed private String modeName; @@ -42,18 +42,16 @@ public class GenericCommandMode implements ICommandMode { /** * Create a new generic command mode - * + * * @param normalOutput * The function to use for normal output * @param errorOutput * The function to use for error output */ public GenericCommandMode(Consumer normalOutput, Consumer errorOutput) { - if (normalOutput == 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"); - } + else if(errorOutput == null) throw new NullPointerException("Error output source must be non-null"); this.normalOutput = normalOutput; this.errorOutput = errorOutput; @@ -69,31 +67,31 @@ public class GenericCommandMode implements ICommandMode { /** * Add an alias to an existing command - * + * * @param commandName * The name of the command to add an alias for * @param aliasName * 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 */ public void addCommandAlias(String commandName, 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) throw new NullPointerException("Alias name must not be null"); - } else if (!commandHandlers.containsKey(commandName) && !defaultHandlers.containsKey(commandName)) { + else if(!commandHandlers.containsKey(commandName) && !defaultHandlers.containsKey(commandName)) throw new IllegalArgumentException("Cannot alias non-existant command '" + commandName + "'"); - } else if (commandHandlers.containsKey(aliasName) || defaultHandlers.containsKey(aliasName)) { + else if(commandHandlers.containsKey(aliasName) || defaultHandlers.containsKey(aliasName)) throw new IllegalArgumentException( "Cannot bind alias '" + aliasName + "' to a command with a bound handler"); - } else { + else { ICommand aliasedCommand; - if (defaultHandlers.containsKey(commandName)) { + if(defaultHandlers.containsKey(commandName)) { aliasedCommand = defaultHandlers.get(commandName).aliased(); } else { aliasedCommand = commandHandlers.get(commandName).aliased(); @@ -105,31 +103,31 @@ public class GenericCommandMode implements ICommandMode { /** * Add a command to this command mode - * + * * @param command * The name of the command to add * @param handler * The handler to use for the specified command - * + * * @throws IllegalArgumentException * if the specified command already has a handler * registered */ public void addCommandHandler(String command, ICommand 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)) throw new IllegalArgumentException("Command " + command + " already has a handler registered"); - } else { + else { commandHandlers.put(command, handler); } } /** * Add a help topic to this command mode that isn't tied to a command - * + * * @param topicName * The name of the topic * @param topic @@ -187,7 +185,7 @@ public class GenericCommandMode implements ICommandMode { + " 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 { @@ -221,16 +219,16 @@ public class GenericCommandMode implements ICommandMode { */ private void doAliasCommands(String[] args) { - if (args.length != 2) { + if(args.length != 2) { errorOutput.accept("ERROR: Alias requires two arguments." + " The command name, and the alias for that command"); } else { String commandName = args[0]; String aliasName = args[1]; - if (!canHandle(commandName)) { + if(!canHandle(commandName)) { errorOutput.accept("ERROR: '" + commandName + "' is not a valid command."); - } else if (canHandle(aliasName)) { + } else if(canHandle(aliasName)) { errorOutput.accept("ERROR: Cannot overwrite command '" + aliasName + "'"); } else { addCommandAlias(commandName, aliasName); @@ -239,15 +237,15 @@ public class GenericCommandMode implements ICommandMode { } private void doHelpCommand(String commandName) { - if (commandHandlers.containsKey(commandName)) { + if(commandHandlers.containsKey(commandName)) { String desc = commandHandlers.get(commandName).getHelp().getDescription(); normalOutput.accept("\n" + desc); - } else if (defaultHandlers.containsKey(commandName)) { + } else if(defaultHandlers.containsKey(commandName)) { 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 { errorOutput.accept( @@ -258,9 +256,9 @@ public class GenericCommandMode implements ICommandMode { private void doHelpSummary() { normalOutput.accept("Help topics for this command mode are as follows:\n"); - if (commandHandlers.getSize() > 0) { + if(commandHandlers.getSize() > 0) { commandHandlers.forEachValue(command -> { - if (!command.isAlias()) { + if(!command.isAlias()) { normalOutput.accept("\t" + command.getHelp().getSummary() + "\n"); } }); @@ -269,9 +267,9 @@ public class GenericCommandMode implements ICommandMode { } normalOutput.accept("\nHelp topics available in all command modes are as follows\n"); - if (defaultHandlers.getSize() > 0) { + if(defaultHandlers.getSize() > 0) { defaultHandlers.forEachValue(command -> { - if (!command.isAlias()) { + if(!command.isAlias()) { normalOutput.accept("\t" + command.getHelp().getSummary() + "\n"); } }); @@ -280,7 +278,7 @@ public class GenericCommandMode implements ICommandMode { } normalOutput.accept("\nHelp topics not associated with a command are as follows\n"); - if (helpTopics.getSize() > 0) { + if(helpTopics.getSize() > 0) { helpTopics.forEachValue(topic -> { normalOutput.accept("\t" + topic.getSummary() + "\n"); }); @@ -306,18 +304,14 @@ public class GenericCommandMode implements ICommandMode { @Override public String getCustomPrompt() { - if (customPrompt != null) { - return customPrompt; - } + if(customPrompt != null) return customPrompt; return ICommandMode.super.getCustomPrompt(); } @Override public String getName() { - if (modeName != null) { - return modeName; - } + if(modeName != null) return modeName; return ICommandMode.super.getName(); } @@ -331,20 +325,19 @@ public class GenericCommandMode implements ICommandMode { public ICommandMode process(String command, 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) { + else { + if(args != null) { errorOutput.accept("ERROR: Unrecognized command " + command + String.join(" ", args)); } else { errorOutput.accept("ERROR: Unrecognized command " + command); } - if (unknownCommandHandler == null) { + if(unknownCommandHandler == null) throw new UnsupportedOperationException("Command " + command + " is invalid."); - } unknownCommandHandler.accept(command, args); } @@ -354,7 +347,7 @@ public class GenericCommandMode implements ICommandMode { /** * Set the custom prompt for this mode - * + * * @param prompt * The custom prompt for this mode, or null to disable * the custom prompt @@ -365,7 +358,7 @@ public class GenericCommandMode implements ICommandMode { /** * Set the name of this mode - * + * * @param name * The desired name of this mode, or null to use the * default name @@ -376,15 +369,13 @@ public class GenericCommandMode implements ICommandMode { /** * Set the handler to use for unknown commands - * + * * @param handler * The handler to use for unknown commands, or null to * throw on unknown commands */ public void setUnknownCommandHandler(BiConsumer 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; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java index 595d7d9..6fa367c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java @@ -2,18 +2,18 @@ package bjc.utils.cli; /** * Generic implementation of a help topic - * + * * @author ben * */ public class GenericHelp implements ICommandHelp { // The strings for this help topic - private String summary; - private String description; + private String summary; + private String description; /** * Create a new help topic - * + * * @param summary * The summary of this help topic * @param description @@ -21,9 +21,7 @@ public class GenericHelp implements ICommandHelp { * help topic doesn't have a more detailed description */ public GenericHelp(String summary, String description) { - if (summary == null) { - throw new NullPointerException("Help summary must be non-null"); - } + if(summary == null) throw new NullPointerException("Help summary must be non-null"); this.summary = summary; this.description = description; @@ -31,9 +29,7 @@ public class GenericHelp implements ICommandHelp { @Override public String getDescription() { - if (description == null) { - return summary; - } + if(description == null) return summary; return description; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java index a72ecdf..6703460 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java @@ -2,35 +2,35 @@ package bjc.utils.cli; /** * Represents a command that can be invoked from a {@link ICommandMode} - * + * * @author ben * */ public interface ICommand { /** * Create a command that serves as an alias to this one - * + * * @return A command that serves as an alias to this one */ public ICommand aliased(); /** * Get the handler that executes this command - * + * * @return The handler that executes this command */ public ICommandHandler getHandler(); /** * Get the help entry for this command - * + * * @return The help entry for this command */ public ICommandHelp getHelp(); /** * Check if this command is an alias of another command - * + * * @return Whether or not this command is an alias of another */ public default boolean isAlias() { diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHandler.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHandler.java index a774108..d105ce3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHandler.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHandler.java @@ -4,7 +4,7 @@ import java.util.function.Function; /** * A handler for a command - * + * * @author ben * */ @@ -12,7 +12,7 @@ import java.util.function.Function; public interface ICommandHandler extends Function { /** * Execute this command - * + * * @param args * The arguments for this command * @return The command mode to switch to after this command, or null to diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java index 472d6a3..9fc9388 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java @@ -2,25 +2,25 @@ package bjc.utils.cli; /** * Interface for the help entry for a command - * + * * @author ben * */ public interface ICommandHelp { /** * Get the description of a command. - * + * * @return The description of a command */ public String getDescription(); /** * Get the summary line for a command. - * + * * A summary line should consist of a string of the following format * "\t" where anything in angle brackets * should be filled in. - * + * * @return The summary line line for a command */ public String getSummary(); diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java index 56f7869..2bf9ccf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java @@ -3,14 +3,14 @@ package bjc.utils.cli; /** * A mode for determining the commands that are valid to enter, and then * handling those commands - * + * * @author ben * */ public interface ICommandMode { /** * Check to see if this mode can handle the specified command - * + * * @param command * The command to check * @return Whether or not this mode can handle the command. It is @@ -22,9 +22,9 @@ public interface ICommandMode { /** * Get the custom prompt for this mode - * + * * @return the custom prompt for this mode - * + * * @throws UnsupportedOperationException * if this mode doesn't support a custom prompt */ @@ -34,7 +34,7 @@ public interface ICommandMode { /** * Get the name of this command mode - * + * * @return The name of this command mode, which is the empty string by * default */ @@ -44,7 +44,7 @@ public interface ICommandMode { /** * Check if this mode uses a custom prompt - * + * * @return Whether or not this mode uses a custom prompt */ public default boolean isCustomPromptEnabled() { @@ -53,7 +53,7 @@ public interface ICommandMode { /** * Process a command in this mode - * + * * @param command * The command to process * @param args diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java index 3e016e2..94dee4d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java @@ -2,7 +2,7 @@ package bjc.utils.cli; /** * Implementation of a help topic that doesn't exist - * + * * @author ben * */ diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/package-info.java b/BJC-Utils2/src/main/java/bjc/utils/cli/package-info.java index 012ccc0..7446a3c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/package-info.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/package-info.java @@ -1,6 +1,6 @@ /** * Holds classes for easier CLI design - * + * * @author ben * */ -- cgit v1.2.3