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 ++++++++++++++-------- 1 file changed, 44 insertions(+), 25 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli/GeneralCommandMode.java') 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); } -- cgit v1.2.3