From 275a627719fc2231b16caea41130ff09f0f2b6a1 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 8 Apr 2016 13:28:09 -0400 Subject: Switch functional data to use interfaces --- .../java/bjc/utils/cli/GeneralCommandMode.java | 69 ++++++++++++++-------- .../main/java/bjc/utils/cli/GenericCommand.java | 59 ++++++++++++++++++ .../src/main/java/bjc/utils/cli/ICommand.java | 23 ++++++++ .../src/main/java/bjc/utils/cli/ICommandHelp.java | 26 ++++++++ 4 files changed, 152 insertions(+), 25 deletions(-) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli') diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GeneralCommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GeneralCommandMode.java index 4a41c1c..880e8c5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GeneralCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GeneralCommandMode.java @@ -1,10 +1,11 @@ package bjc.utils.cli; -import java.util.HashMap; -import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Consumer; +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IFunctionalMap; + /** * A general command mode, with a customizable set of commands * @@ -16,17 +17,19 @@ import java.util.function.Consumer; * */ public class GeneralCommandMode implements ICommandMode { - private Map commandHandlers; - private String customPrompt; + private IFunctionalMap commandHandlers; + private IFunctionalMap defaultHandlers; + + private IFunctionalMap helpTopics; - private Map defaultHandlers; + private BiConsumer unknownCommandHandler; - private Consumer errorOutput; - private String modeName; + private Consumer errorOutput; + private Consumer normalOutput; - private Consumer normalOutput; + private String modeName; - private BiConsumer unknownCommandHandler; + private String customPrompt; /** * Create a new general command mode @@ -41,20 +44,35 @@ public class GeneralCommandMode implements ICommandMode { this.normalOutput = normalOutput; this.errorOutput = errorOutput; - commandHandlers = new HashMap<>(); - defaultHandlers = new HashMap<>(); + commandHandlers = new FunctionalMap<>(); + defaultHandlers = new FunctionalMap<>(); - defaultHandlers.put("list", (args) -> { + defaultHandlers.put("list", new GenericCommand((args) -> { listCommands(); return this; - }); + }, "list\tList available command", + "Lists all of the commands available in this mode," + + " as well as the commands that are valid in any mode.")); - defaultHandlers.put("alias", (args) -> { + defaultHandlers.put("alias", new GenericCommand((args) -> { aliasCommands(args); return this; - }); + }, "alias\tAlias one command to another", + "alias gives a command another name it can be invoked by. It is invoked" + + " with two arguments, the name of the command to alias" + + ", and the alias to give that command.")); + + defaultHandlers.put("help", new GenericCommand((args) -> { + // TODO implement help system + return this; + }, "help\tConsult the help system", + "help consults the internal help system." + + " It can be invoked in two ways. Invoking it with no arguments" + + " causes it to print out all the topics you can ask for details on," + + " while invoking it with the name of a topic will print the entry" + + " for that topic")); } /** @@ -100,8 +118,7 @@ public class GeneralCommandMode implements ICommandMode { * if the specified command already has a handler * registered */ - public void addCommandHandler(String command, - ICommandHandler handler) { + public void addCommandHandler(String command, ICommand handler) { if (command == null) { throw new NullPointerException("Command must not be null"); } else if (handler == null) { @@ -162,14 +179,14 @@ public class GeneralCommandMode implements ICommandMode { normalOutput.accept( "The available commands for this mode are as follows:\n"); - commandHandlers.keySet().forEach((commandName) -> { + commandHandlers.keyList().forEach((commandName) -> { normalOutput.accept("\t" + commandName); }); normalOutput.accept( "\nThe following commands are available in all modes:\n"); - defaultHandlers.keySet().forEach((commandName) -> { + defaultHandlers.keyList().forEach((commandName) -> { normalOutput.accept("\t" + commandName); }); @@ -181,14 +198,11 @@ public class GeneralCommandMode implements ICommandMode { normalOutput.accept("\n"); if (defaultHandlers.containsKey(command)) { - return defaultHandlers.get(command).handle(args); + return defaultHandlers.get(command).getHandler().handle(args); } else if (commandHandlers.containsKey(command)) { - return commandHandlers.get(command).handle(args); + return commandHandlers.get(command).getHandler().handle(args); } else { - if (unknownCommandHandler == null) { - throw new UnsupportedOperationException( - "Command " + command + " is invalid."); - } else if (args != null) { + if (args != null) { errorOutput.accept("ERROR: Unrecognized command " + command + String.join(" ", args)); } else { @@ -196,6 +210,11 @@ public class GeneralCommandMode implements ICommandMode { .accept("ERROR: Unrecognized command " + command); } + if (unknownCommandHandler == null) { + throw new UnsupportedOperationException( + "Command " + command + " is invalid."); + } + unknownCommandHandler.accept(command, args); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java new file mode 100644 index 0000000..99951cc --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java @@ -0,0 +1,59 @@ +package bjc.utils.cli; + +/** + * Generic command implementation + * + * @author ben + * + */ +public class GenericCommand implements ICommand { + private static class GenericHelp implements ICommandHelp { + private String summary; + private String description; + + public GenericHelp(String summary, String description) { + this.summary = summary; + this.description = description; + } + + @Override + public String getSummary() { + return summary; + } + + @Override + public String getDescription() { + return description; + } + + } + + private ICommandHandler handler; + private ICommandHelp help; + + /** + * Create a new generic command + * + * @param handler + * The handler to use for the command + * @param description + * The description of the command + * @param help + * The detailed help message for the command + */ + public GenericCommand(ICommandHandler handler, String description, + String help) { + this.handler = handler; + this.help = new GenericHelp(description, help); + } + + @Override + public ICommandHandler getHandler() { + return handler; + } + + @Override + public ICommandHelp getHelp() { + return help; + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java new file mode 100644 index 0000000..681986d --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommand.java @@ -0,0 +1,23 @@ +package bjc.utils.cli; + +/** + * Represents a command that can be invoked from a {@link ICommandMode} + * + * @author ben + * + */ +public interface ICommand { + /** + * 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(); +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java new file mode 100644 index 0000000..ad03dac --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHelp.java @@ -0,0 +1,26 @@ +package bjc.utils.cli; + +/** + * Interface for the help entry for a command + * + * @author ben + * + */ +public interface ICommandHelp { + /** + * Get the summary line for a command + * + * Used for 'help commands' which gives the user a brief idea what all + * the commands do + * + * @return The summary line line for a command + */ + public String getSummary(); + + /** + * Get the description of a command + * + * @return The description of a command + */ + public String getDescription(); +} -- cgit v1.2.3