From 7f59d0b9de4536705b3122cb5a85d9c9f85846a3 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Fri, 17 Mar 2017 08:52:13 -0400 Subject: Add toString/equals/hashCode/compareTo part 1 Adds utility methods to classes that need them. This covers the cli & component packages. --- .../main/java/bjc/utils/cli/DelegatingCommand.java | 22 +++++- .../main/java/bjc/utils/cli/GenericCommand.java | 33 ++++++-- .../java/bjc/utils/cli/GenericCommandMode.java | 92 ++++++++++++++++------ .../src/main/java/bjc/utils/cli/GenericHelp.java | 21 +++++ .../src/main/java/bjc/utils/cli/ICommand.java | 8 +- .../main/java/bjc/utils/cli/ICommandHandler.java | 2 +- .../src/main/java/bjc/utils/cli/ICommandHelp.java | 4 +- .../src/main/java/bjc/utils/cli/ICommandMode.java | 15 ++-- .../src/main/java/bjc/utils/cli/NullHelp.java | 2 + .../src/main/java/bjc/utils/cli/package-info.java | 7 -- .../bjc/utils/components/ComponentDescription.java | 48 +++++++++++ .../components/ComponentDescriptionState.java | 74 +++++++++++++++++ .../utils/components/FileComponentRepository.java | 26 ++++++ .../bjc/utils/components/IComponentRepository.java | 2 +- .../bjc/utils/components/IDescribedComponent.java | 22 ++++-- .../java/bjc/utils/components/package-info.java | 7 -- 16 files changed, 323 insertions(+), 62 deletions(-) delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/cli/package-info.java delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/components/package-info.java (limited to 'BJC-Utils2/src/main/java/bjc/utils') 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 defaultHandlers; /* - * Contains help topics without an associated command + * Contains help topics without an associated command */ private IMap helpTopics; /* - * The action to execute upon encountering an unknown command + * The action to execute upon encountering an unknown command */ private BiConsumer unknownCommandHandler; /* - * The functions to use for input/output + * The functions to use for input/output */ private Consumer errorOutput; private Consumer 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 normalOutput, Consumer 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 { * @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 { /** * 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; diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java index 13132ed..b750848 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java @@ -82,4 +82,52 @@ public class ComponentDescription implements IDescribedComponent { public String toString() { return name + " component v" + version + ", written by " + author; } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + + result = prime * result + ((author == null) ? 0 : author.hashCode()); + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + version; + + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + ComponentDescription other = (ComponentDescription) obj; + + if(author == null) { + if(other.author != null) return false; + } else if(!author.equals(other.author)) return false; + + if(description == null) { + if(other.description != null) return false; + } else if(!description.equals(other.description)) return false; + + if(name == null) { + if(other.name != null) return false; + } else if(!name.equals(other.name)) return false; + + if(version != other.version) return false; + + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java index 02e003a..d6fbc5a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java @@ -67,4 +67,78 @@ public class ComponentDescriptionState { public ComponentDescription toDescription() { return new ComponentDescription(name, author, description, version); } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + + result = prime * result + ((author == null) ? 0 : author.hashCode()); + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + version; + + return result; + } + + @Override + public boolean equals(Object obj) { + if(this == obj) return true; + if(obj == null) return false; + if(getClass() != obj.getClass()) return false; + + ComponentDescriptionState other = (ComponentDescriptionState) obj; + + if(author == null) { + if(other.author != null) return false; + } else if(!author.equals(other.author)) return false; + + if(description == null) { + if(other.description != null) return false; + } else if(!description.equals(other.description)) return false; + + if(name == null) { + if(other.name != null) return false; + } else if(!name.equals(other.name)) return false; + + if(version != other.version) return false; + + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("ComponentDescriptionState ["); + + if(name != null) { + builder.append("name="); + builder.append(name); + builder.append(", "); + } + + if(description != null) { + builder.append("description="); + builder.append(description); + builder.append(", "); + } + + if(author != null) { + builder.append("author="); + builder.append(author); + builder.append(", "); + } + + builder.append("version="); + builder.append(version); + builder.append("]"); + + return builder.toString(); + } + } diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java index d69b794..6fd6177 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -148,4 +148,30 @@ public class FileComponentRepository + pth.toString() + ". This component will not be loaded"); } } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("FileComponentRepository ["); + + if(components != null) { + builder.append("components="); + builder.append(components); + builder.append(", "); + } + + if(sourceDirectory != null) { + builder.append("sourceDirectory="); + builder.append(sourceDirectory); + } + + builder.append("]"); + + return builder.toString(); + } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java index 5ef65ee..6ee51f3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java @@ -32,7 +32,7 @@ public interface IComponentRepository public ComponentType getByName(String name); /** - * Get a list of all the registered componets + * Get a list of all the registered components * * @return A list of all the registered components */ diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java b/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java index a397bbc..f231924 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java @@ -7,7 +7,7 @@ package bjc.utils.components; * @author ben * */ -public interface IDescribedComponent { +public interface IDescribedComponent extends Comparable{ /** * Get the author of this component * @@ -15,7 +15,7 @@ public interface IDescribedComponent { * * @return The author of the component */ - public default String getAuthor() { + default String getAuthor() { return "Anonymous"; } @@ -27,7 +27,7 @@ public interface IDescribedComponent { * * @return The description of the component */ - public default String getDescription() { + default String getDescription() { return "No description provided."; } @@ -38,7 +38,7 @@ public interface IDescribedComponent { * * @return The name of the component */ - public String getName(); + String getName(); /** * Get the version of this component @@ -47,7 +47,19 @@ public interface IDescribedComponent { * * @return The version of this component */ - public default int getVersion() { + default int getVersion() { return 1; } + + + @Override + default int compareTo(IDescribedComponent o) { + int res = getName().compareTo(o.getName()); + + if(res == 0) { + res = getVersion() - o.getVersion(); + } + + return res; + } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/package-info.java b/BJC-Utils2/src/main/java/bjc/utils/components/package-info.java deleted file mode 100644 index 37a9e84..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/components/package-info.java +++ /dev/null @@ -1,7 +0,0 @@ -/** - * This package contains utilities for components - * - * @author ben - * - */ -package bjc.utils.components; -- cgit v1.2.3