diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-17 08:52:13 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2017-03-17 08:52:13 -0400 |
| commit | 7f59d0b9de4536705b3122cb5a85d9c9f85846a3 (patch) | |
| tree | 8aeed52ab4a18385f63dae2f51c792b88da669bb /BJC-Utils2/src/main/java/bjc/utils/cli | |
| parent | 9d89261fedf23c11b684eb66cefdd86a9378ad20 (diff) | |
Add toString/equals/hashCode/compareTo part 1
Adds utility methods to classes that need them.
This covers the cli & component packages.
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli')
10 files changed, 157 insertions, 49 deletions
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 b6f7a87..99d7e43 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/DelegatingCommand.java @@ -8,7 +8,7 @@ package bjc.utils.cli; */ class DelegatingCommand implements ICommand { /* - * The command to delegate to. + * The command to delegate to. */ private ICommand delegate; @@ -41,4 +41,24 @@ class DelegatingCommand implements ICommand { public boolean isAlias() { return true; } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("DelegatingCommand ["); + + if(delegate != null) { + builder.append("delegate="); + builder.append(delegate); + } + + builder.append("]"); + + return builder.toString(); + } } 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 a365135..ea10108 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java @@ -8,12 +8,12 @@ package bjc.utils.cli; */ public class GenericCommand implements ICommand { /* - * The behavior for invoking the command. + * The behavior for invoking the command. */ private ICommandHandler handler; /* - * The help for the command. + * The help for the command. */ private ICommandHelp help; @@ -23,11 +23,12 @@ public class GenericCommand implements ICommand { * @param handler * 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(ICommandHandler handler, String description, String help) { if(handler == null) throw new NullPointerException("Command handler must not be null"); @@ -60,4 +61,24 @@ public class GenericCommand implements ICommand { public boolean isAlias() { return false; } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("GenericCommand ["); + + if(help != null) { + builder.append("help="); + builder.append(help); + } + + builder.append("]"); + + return builder.toString(); + } } 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 f2dae7d..ee2bdbb 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -25,28 +25,28 @@ public class GenericCommandMode implements ICommandMode { 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; @@ -59,21 +59,22 @@ 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; + this.errorOutput = errorOutput; /* - * Initialize handler maps so that they sort in alphabetical + * Initialize handler maps so that they sort in alphabetical */ /* - * order + * order */ commandHandlers = new FunctionalMap<>(new TreeMap<>()); defaultHandlers = new FunctionalMap<>(new TreeMap<>()); - helpTopics = new FunctionalMap<>(new TreeMap<>()); + helpTopics = new FunctionalMap<>(new TreeMap<>()); setupDefaultCommands(); } @@ -157,8 +158,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); @@ -181,7 +182,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"); @@ -193,19 +194,19 @@ 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]); } @@ -217,7 +218,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(); @@ -405,19 +406,64 @@ public class GenericCommandMode implements ICommandMode { addCommandAlias("help", "man"); /* - * Add commands handled in a upper layer. + * Add commands handled in a upper layer. */ /* - * @TODO figure out a place to put commands that apply across + * @TODO figure out a place to put commands that apply across */ /* - * all + * all */ /* - * modes, but only apply to a specific application + * modes, but only apply to a specific application */ defaultHandlers.put("clear", buildClearCommands()); defaultHandlers.put("exit", buildExitCommand()); } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("GenericCommandMode ["); + + if(commandHandlers != null) { + builder.append("commandHandlers="); + builder.append(commandHandlers); + } + + if(defaultHandlers != null) { + builder.append(", "); + builder.append("defaultHandlers="); + builder.append(defaultHandlers); + } + + if(helpTopics != null) { + builder.append(", "); + builder.append("helpTopics="); + builder.append(helpTopics); + } + + if(modeName != null) { + builder.append(", "); + builder.append("modeName="); + builder.append(modeName); + } + + if(customPrompt != null) { + builder.append(", "); + builder.append("customPrompt="); + builder.append(customPrompt); + } + + builder.append("]"); + + return builder.toString(); + } + } 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 6fa367c..bbdd1fc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java @@ -39,4 +39,25 @@ public class GenericHelp implements ICommandHelp { return summary; } + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("GenericHelp ["); + + if(summary != null) { + builder.append("summary="); + builder.append(summary); + } + + if(description != null) { + builder.append(", "); + builder.append("description="); + builder.append(description); + } + + builder.append("]"); + + return builder.toString(); + } } 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 6703460..6d30c6a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java @@ -12,28 +12,28 @@ public interface ICommand { * * @return A command that serves as an alias to this one */ - public ICommand aliased(); + ICommand aliased(); /** * Get the handler that executes this command * * @return The handler that executes this command */ - public ICommandHandler getHandler(); + ICommandHandler getHandler(); /** * Get the help entry for this command * * @return The help entry for this command */ - public ICommandHelp getHelp(); + 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() { + default boolean isAlias() { return false; } } 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 d105ce3..d1f7f77 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHandler.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHandler.java @@ -18,7 +18,7 @@ public interface ICommandHandler extends Function<String[], ICommandMode> { * @return The command mode to switch to after this command, or null to * stop executing commands */ - public default ICommandMode handle(String[] args) { + default ICommandMode handle(String[] args) { return this.apply(args); } } 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 9fc9388..f267594 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java @@ -12,7 +12,7 @@ public interface ICommandHelp { * * @return The description of a command */ - public String getDescription(); + String getDescription(); /** * Get the summary line for a command. @@ -23,5 +23,5 @@ public interface ICommandHelp { * * @return The summary line line for a command */ - public String getSummary(); + 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 2bf9ccf..431a8cf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandMode.java @@ -7,7 +7,7 @@ package bjc.utils.cli; * @author ben * */ -public interface ICommandMode { +public interface ICommandMode extends Comparable<ICommandMode> { /** * Check to see if this mode can handle the specified command * @@ -16,7 +16,7 @@ public interface ICommandMode { * @return Whether or not this mode can handle the command. It is * assumed not by default */ - public default boolean canHandle(String command) { + default boolean canHandle(String command) { return false; }; @@ -28,7 +28,7 @@ public interface ICommandMode { * @throws UnsupportedOperationException * if this mode doesn't support a custom prompt */ - public default String getCustomPrompt() { + default String getCustomPrompt() { throw new UnsupportedOperationException("This mode doesn't support a custom prompt"); } @@ -47,7 +47,7 @@ public interface ICommandMode { * * @return Whether or not this mode uses a custom prompt */ - public default boolean isCustomPromptEnabled() { + default boolean isCustomPromptEnabled() { return false; } @@ -61,7 +61,12 @@ public interface ICommandMode { * @return The command mode to use for the next command. Defaults to * returning this, and doing nothing else */ - public default ICommandMode process(String command, String[] args) { + default ICommandMode process(String command, String[] args) { return this; } + + @Override + default int compareTo(ICommandMode o) { + return getName().compareTo(o.getName()); + } } 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 94dee4d..0d511a4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java @@ -16,4 +16,6 @@ public class NullHelp implements ICommandHelp { public String getSummary() { return "No summary provided"; } + + } 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 deleted file mode 100644 index 7446a3c..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/package-info.java +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Holds classes for easier CLI design - * - * @author ben - * - */ -package bjc.utils.cli; |
