summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java301
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSCommand.java20
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSException.java32
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSMode.java112
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java144
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSUtils.java63
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/MacroFDSMode.java79
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/SimpleFDSMode.java173
8 files changed, 0 insertions, 924 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java
deleted file mode 100644
index d94aae0..0000000
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java
+++ /dev/null
@@ -1,301 +0,0 @@
-package bjc.utils.cli.fds;
-
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStreamReader;
-import java.io.PrintStream;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
-import bjc.utils.cli.CommandHelp;
-import bjc.utils.cli.GenericHelp;
-import bjc.utils.cli.fds.FDSState.InputMode;
-import bjc.utils.funcutils.StringUtils;
-import bjc.utils.ioutils.Block;
-import bjc.utils.ioutils.BlockReader;
-import bjc.utils.ioutils.PushbackBlockReader;
-
-import com.ibm.icu.text.BreakIterator;
-
-import static bjc.utils.ioutils.BlockReaders.*;
-
-/**
- * Runs a FDS (FDiskScript) interface.
- *
- * This is a rudimentary console interface inspired heavily by FDisk's interface
- * style.
- *
- * Commands are denoted by a single character, but can invoke submodes.
- *
- * @author bjculkin
- *
- */
-public class FDS {
- private static SimpleFDSMode<?> miscMode;
-
- static {
- miscMode = new SimpleFDSMode<>("MSC");
- configureMiscMode();
- }
-
- private static void configureMiscMode() {
- GenericHelp loadScriptHelp = new GenericHelp("load-script\tLoad a script from a file", "");
- miscMode.addCommand("X", FDS::loadScript, loadScriptHelp);
-
- GenericHelp quitProgramHelp = new GenericHelp("quit-program\tQuit the program", "");
- miscMode.addCommand("Q", (state) -> state.modes.drop(state.modes.size()), quitProgramHelp);
-
- GenericHelp inlineInputHelp = new GenericHelp("inline-input\tProvide data input inline with commands",
- "");
- miscMode.addCommand("I", (state) -> {
- if(state.mode == InputMode.CHORD) {
- state.mode = InputMode.INLINE;
- } else if(state.mode == InputMode.INLINE) {
- state.mode = InputMode.CHORD;
- } else {
- state.printer.printf("? MNV\n");
- }
- }, inlineInputHelp);
-
- GenericHelp dataMacroHelp = new GenericHelp("input-macro\tDefine a macro for providing data", "");
- miscMode.addCommand("i", (state) -> createMacro(state, state.dataMacros), dataMacroHelp);
-
- GenericHelp comMacroHelp = new GenericHelp("command-macro\tDefine a macro for providing commands", "");
- miscMode.addCommand("c", (state) -> createMacro(state, state.commandMacros), comMacroHelp);
- }
-
- private static void createMacro(FDSState<?> state, Map<String, List<Block>> macroStore) {
- PushbackBlockReader source = state.datain;
-
- String macroName;
- List<Block> macroBody = new LinkedList<>();
-
- state.dataPrompter.accept("Enter macro name: ");
- Block blk = source.next();
-
- String blkContents = blk.contents.trim();
-
- BreakIterator charBreaker = BreakIterator.getCharacterInstance();
- charBreaker.setText(blkContents);
-
- macroName = blkContents.substring(0, charBreaker.next());
-
- state.dataPrompter.accept("Enter macro body (. to end body)");
- while(source.hasNext()) {
- blk = source.next();
-
- blkContents = blk.contents.trim();
-
- if(blkContents.equals(".")) break;
-
- macroBody.add(new Block(blk.blockNo, blkContents, blk.startLine, blk.endLine));
- }
-
- macroStore.put(macroName, macroBody);
-
- state.dataPrompter.accept(state.defaultPrompt);
- }
-
- /**
- * Run a provided FDS mode until it is exited or there is no more input.
- *
- * @param state
- * The initial state for the mode.
- *
- * @return The final state of the mode.
- *
- * @throws FDSException
- * If something went wrong during mode execution.
- */
- public static <S> S runFDS(FDSState<S> state) throws FDSException {
- BlockReader blockSource = state.comin;
-
- while(blockSource.hasNext() && !state.modes.empty()) {
- Block comBlock = blockSource.next();
-
- handleCommandString(comBlock, state);
- }
-
- return state.state;
- }
-
- private static <S> void handleCommandString(Block comBlock, FDSState<S> state) throws FDSException {
- String comString = comBlock.contents.trim();
- BreakIterator charBreaker = BreakIterator.getCharacterInstance();
- charBreaker.setText(comString);
-
- switch(state.mode) {
- case INLINE:
- if(comString.contains(" ")) {
- handleInlineCommand(comString.split(" "), state, comBlock);
- break;
- }
- case CHORD:
- if(StringUtils.graphemeCount(comString) > 1) {
- chordCommand(comBlock, state, comString);
- break;
- }
- case NORMAL:
- handleCommand(comString.substring(0, charBreaker.next()), state);
- break;
- default:
- throw new FDSException(String.format("Unknown input mode '%s'", state.mode));
- }
- }
-
- private static <S> void handleInlineCommand(String[] commands, FDSState<S> state, Block comBlock)
- throws FDSException {
- boolean dataInput = false;
-
- for(int i = 0; i < commands.length; i++) {
- String strang = commands[i].trim();
-
- if(dataInput) {
- if(strang.equals(";")) {
- dataInput = false;
- } else {
- Block dataBlock = new Block(comBlock.blockNo + i, strang, comBlock.startLine,
- comBlock.endLine);
-
- state.datain.addBlock(dataBlock);
- }
- } else {
- chordCommand(comBlock, state, strang);
-
- dataInput = true;
- }
- }
- }
-
- private static <S> void chordCommand(Block comBlock, FDSState<S> state, String comString) throws FDSException {
- PushbackBlockReader source = state.comin;
-
- BreakIterator charBreaker = BreakIterator.getCharacterInstance();
- charBreaker.setText(comString);
-
- int lastPos = charBreaker.first();
-
- while(charBreaker.next() != BreakIterator.DONE && lastPos < comString.length()) {
- String c = comString.substring(lastPos, charBreaker.current());
-
- Block newCom = new Block(comBlock.blockNo + 1, c, comBlock.startLine, comBlock.startLine);
-
- source.addBlock(newCom);
-
- lastPos = charBreaker.current();
- }
- }
-
- @SuppressWarnings("unchecked")
- private static <S> void handleCommand(String com, FDSState<S> state) throws FDSException {
- if(state.modes.empty()) return;
-
- PrintStream printer = state.printer;
-
- /*
- * Handle built-in commands over user commands.
- */
- switch(com) {
- case "x":
- if(state.mode == InputMode.CHORD) {
- state.mode = InputMode.NORMAL;
- } else if(state.mode == InputMode.NORMAL) {
- state.mode = InputMode.CHORD;
- } else {
- printer.println("? MNV\n");
- }
- break;
- case "q":
- FDSMode<S> md = state.modes.pop();
- printer.printf("!< %s\n", md.getName());
- break;
- case "m":
- helpSummary(printer, state);
- break;
- case "M":
- /*
- * We'll see if this cast actually causes any issues.
- *
- * None of the commands in misc. mode should depend on
- * the state type, so it shouldn't matter.
- */
- state.modes.push((FDSMode<S>) miscMode);
- printer.printf("!> MSC\n");
- break;
- case "@":
- state.modes.push(state.dataMacroMode);
- printer.printf("!> IDM\n");
- break;
- case "#":
- state.modes.push(state.comMacroMode);
- printer.printf("!> ICM\n");
- break;
- default:
- FDSMode<S> curMode = state.modes.top();
-
- if(curMode.hasSubmode(com)) {
- FDSMode<S> mode = curMode.getSubmode(com);
-
- state.modes.push(mode);
- printer.printf("!> %s\n", mode.getName());
- } else if(curMode.hasCommand(com)) {
- curMode.getCommand(com).run(state);
- } else {
- printer.printf("? UBC '%s'\n", com);
- }
- }
- }
-
- private static <S> void loadScript(FDSState<S> state) {
- state.dataPrompter.accept("Enter a filename: ");
-
- String fileName = state.datain.next().contents.split(" ")[0];
-
- PrintStream printer = state.printer;
-
- try {
- FileInputStream fis = new FileInputStream(fileName);
-
- BlockReader reader = simple("\\R", new InputStreamReader(fis));
-
- state.comin = pushback(serial(reader, state.comin));
- state.datain = pushback(serial(reader, state.datain));
- } catch(FileNotFoundException fnfex) {
- printer.printf("? FNF '%s'\n", fileName);
- }
-
- state.dataPrompter.accept(state.defaultPrompt);
- }
-
- private static <S> void helpSummary(PrintStream printer, FDSState<S> state) {
- FDSMode<S> mode = state.modes.top();
-
- printer.printf("Help for mode %s:\n", mode.getName());
-
- for(String bound : mode.registeredChars()) {
- Collection<CommandHelp> help = mode.getHelp(bound);
-
- if(help.size() > 1) {
- for(CommandHelp hlp : help) {
- printer.printf("\t%s\t- %s\n", bound, hlp.getSummary());
- }
-
- printer.println();
- } else {
- CommandHelp hlp = help.iterator().next();
-
- printer.printf("\t%s\t- %s\n", bound, hlp.getSummary());
- }
- }
-
- printer.printf("\nHelp for global commands:\n");
- printer.printf("\tx\t- chord mode\tread each character as a seperate command\n");
- printer.printf("\tq\t- exit mode\texit the current submode\n");
- printer.printf("\tm\t- show help\tshow this help message\n");
- printer.printf("\tM\t- misc. mode\tsubmode with misc. commands\n");
- printer.printf("\t@\t- invoke data macro\tinvoke bound data macros\n");
- printer.printf("\t#\t- invoke command macro\tinvoke bound command macros\n\n");
- }
-} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSCommand.java
deleted file mode 100644
index bb64cdf..0000000
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSCommand.java
+++ /dev/null
@@ -1,20 +0,0 @@
-package bjc.utils.cli.fds;
-
-/**
- * A command attached to an FDS interface.
- *
- * @author bjculkin
- *
- * @param <S>
- * The state type of the interface.
- */
-@FunctionalInterface
-public interface FDSCommand<S> {
- /**
- * Run this command.
- *
- * @param state
- * The current FDS state.
- */
- void run(FDSState<S> state);
-}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSException.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSException.java
deleted file mode 100644
index 7569d95..0000000
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSException.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package bjc.utils.cli.fds;
-
-/**
- * Exception thrown when something goes wrong with FDS.
- *
- * @author bjculkin
- *
- */
-public class FDSException extends Exception {
- /**
- * Create a new FDS exception with a message and a cause.
- *
- * @param message
- * The message for the exception.
- *
- * @param cause
- * The cause of the exception.
- */
- public FDSException(String message, Throwable cause) {
- super(message, cause);
- }
-
- /**
- * Create a new FDS exception with a message.
- *
- * @param message
- * The message for the exception.
- */
- public FDSException(String message) {
- super(message);
- }
-}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSMode.java
deleted file mode 100644
index 60aeb38..0000000
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSMode.java
+++ /dev/null
@@ -1,112 +0,0 @@
-package bjc.utils.cli.fds;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-
-import bjc.utils.cli.CommandHelp;
-import bjc.utils.cli.NullHelp;
-
-/**
- * A collection of related FDS commands.
- *
- * @author bjculkin
- *
- * @param <S>
- * The FDS state type.
- */
-public interface FDSMode<S> {
- /**
- * The default help for anything in a mode command.
- */
- public static final List<CommandHelp> DEFAULT_HELP = Arrays.asList(new NullHelp());
-
- /**
- * Get the name of this mode.
- *
- * @return The mode of this name.
- */
- default String getName() {
- return "Unnamed Mode";
- }
-
- /**
- * Get all the characters that are registered to something in this mode.
- *
- * In this context, something means a command or submode.
- *
- * @return All of the characters registered to something in this mode.
- */
- String[] registeredChars();
-
- /*
- * Check for the existence of commands/submodes.
- */
-
- /**
- * Check if there is a command registered to the given character.
- *
- * @param c
- * The character to check
- *
- * @return Whether or not there is a command bound to that character.
- */
- boolean hasCommand(String c);
-
- /**
- * Check if there is a submode registered to the given character.
- *
- * @param c
- * The character to check
- *
- * @return Whether or not there is a submode bound to that character.
- */
- boolean hasSubmode(String c);
-
- /*
- * Get commands and submodes.
- */
-
- /**
- * Get the command attached to a given character.
- *
- * @param c
- * The character to get the command for.
- *
- * @return The command bound to that character.
- *
- * @throws FDSException
- * If there is no command bound to that character.
- */
- FDSCommand<S> getCommand(String c) throws FDSException;
-
- /**
- * Get the command attached to a given character.
- *
- * @param c
- * The character to get the command for.
- *
- * @return The command bound to that character.
- *
- * @throws FDSException
- * If there is no command bound to that character.
- */
- FDSMode<S> getSubmode(String c) throws FDSException;
-
- /*
- * Help utilities
- */
- /**
- * Get the help for what's bound to a character.
- *
- * This should be one line.
- *
- * @param c
- * The character to look at the help for.
- *
- * @return The help for what's bound to the character.
- */
- default Collection<CommandHelp> getHelp(String c) {
- return DEFAULT_HELP;
- }
-}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java
deleted file mode 100644
index e28e6bc..0000000
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java
+++ /dev/null
@@ -1,144 +0,0 @@
-package bjc.utils.cli.fds;
-
-import java.io.PrintStream;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.function.Consumer;
-
-import bjc.utils.esodata.SimpleStack;
-import bjc.utils.esodata.Stack;
-import bjc.utils.ioutils.Block;
-import bjc.utils.ioutils.PushbackBlockReader;
-
-/**
- * Internal state for an FDS interface.
- *
- * @author bjculkin
- *
- * @param <S>
- * The state type of the interface.
- */
-public class FDSState<S> {
- /**
- * The input mode for the interface.
- *
- * @author bjculkin
- *
- */
- public static enum InputMode {
- /**
- * Normal mode.
- *
- * Reads only the first character in the block as a command.
- */
- NORMAL,
- /**
- * Reads every character in the block as a command.
- */
- CHORD,
- /**
- * Reads every character in the block, but after a terminal
- * command, data will be read in-line separated by spaces until
- * a semicolon is read.
- *
- * The semicolon can be escaped with a backslash.
- */
- INLINE;
- }
-
- /**
- * The state of the interface
- */
- public S state;
- /**
- * The input mode for the interface.
- */
- public InputMode mode;
-
- /**
- * The modes being used.
- */
- public Stack<FDSMode<S>> modes;
-
- /**
- * The source to read command blocks from.
- */
- public PushbackBlockReader comin;
-
- /**
- * The source to read data blocks from.
- */
- public PushbackBlockReader datain;
-
- /**
- * The destination for output.
- */
- public PrintStream printer;
-
- /**
- * The repository for data macros.
- */
- public Map<String, List<Block>> dataMacros;
-
- /**
- * The repository for command macros.
- */
- public Map<String, List<Block>> commandMacros;
-
- FDSMode<S> dataMacroMode;
- FDSMode<S> comMacroMode;
-
- /**
- * Function to change the current data prompt.
- */
- public Consumer<String> dataPrompter;
-
- /**
- * The default data prompt.
- */
- public String defaultPrompt;
-
- /**
- * Create a new interface state.
- *
- * @param stat
- * The initial state for the interface.
- *
- * @param inputMode
- * The input mode for the interface.
- * @param cmin
- * The source of command blocks.
- *
- * @param datin
- * The source of data blocks.
- *
- * @param print
- * The destination for output.
- * @param dataPrompt
- * The function to use for changing the data prompt.
- *
- * @param normalPrompt
- * The default data prompt.
- */
- public FDSState(S stat, InputMode inputMode, PushbackBlockReader cmin, PushbackBlockReader datin,
- PrintStream print, Consumer<String> dataPrompt, String normalPrompt) {
- state = stat;
- mode = inputMode;
-
- comin = cmin;
- datain = datin;
- printer = print;
-
- dataPrompter = dataPrompt;
- defaultPrompt = normalPrompt;
-
- modes = new SimpleStack<>();
-
- dataMacros = new HashMap<>();
- commandMacros = new HashMap<>();
-
- dataMacroMode = new MacroFDSMode<>(dataMacros, datain::addBlock);
- comMacroMode = new MacroFDSMode<>(commandMacros, comin::addBlock);
- }
-} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSUtils.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSUtils.java
deleted file mode 100644
index a245471..0000000
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSUtils.java
+++ /dev/null
@@ -1,63 +0,0 @@
-package bjc.utils.cli.fds;
-
-import static bjc.utils.ioutils.BlockReaders.pushback;
-import static bjc.utils.ioutils.BlockReaders.simple;
-import static bjc.utils.ioutils.BlockReaders.trigger;
-
-import java.io.PrintStream;
-import java.io.Reader;
-
-import bjc.utils.cli.fds.FDSState.InputMode;
-import bjc.utils.ioutils.BlockReader;
-import bjc.utils.ioutils.Prompter;
-import bjc.utils.ioutils.PushbackBlockReader;
-
-/**
- * Utilities for dealing with FDS
- *
- * @author bjculkin
- *
- */
-public class FDSUtils {
- /**
- * Run a FDS instance from a reader.
- *
- * @param reader
- * The reader to use.
- *
- * @param out
- * The output stream to use.
- *
- * @param mode
- * The mode to use.
- *
- * @param ctx
- * The initial state.
- *
- * @return The final state.
- *
- * @throws FDSException
- * If something goes wrong.
- */
- public static <S> S runFromReader(Reader reader, PrintStream out, FDSMode<S> mode, S ctx) throws FDSException {
- BlockReader input = simple("\\R", reader);
-
- Prompter comPrompter = new Prompter("Enter a command (m for help): ", out);
- Prompter dataPrompter = new Prompter("> ", out);
-
- BlockReader rawComInput = trigger(input, comPrompter);
- BlockReader rawDataInput = trigger(input, dataPrompter);
-
- PushbackBlockReader comInput = pushback(rawComInput);
- PushbackBlockReader dataInput = pushback(rawDataInput);
-
- FDSState<S> fdsState = new FDSState<>(ctx, InputMode.NORMAL, comInput, dataInput, out,
- dataPrompter::setPrompt, "> ");
-
- fdsState.modes.push(mode);
-
- FDS.runFDS(fdsState);
-
- return ctx;
- }
-} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/MacroFDSMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/MacroFDSMode.java
deleted file mode 100644
index 2576ee5..0000000
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/MacroFDSMode.java
+++ /dev/null
@@ -1,79 +0,0 @@
-package bjc.utils.cli.fds;
-
-import bjc.utils.ioutils.Block;
-
-import java.util.List;
-import java.util.Map;
-import java.util.function.Consumer;
-
-/**
- * A implementation of FDS mode that invokes macros bound into a map.
- *
- * @author EVE
- *
- * @param <S>
- * The FDS state type.
- */
-public class MacroFDSMode<S> implements FDSMode<S> {
- private final class MacroFDSCommand implements FDSCommand<S> {
- private String macroName;
-
- public MacroFDSCommand(String c) {
- macroName = c;
- }
-
- @Override
- public void run(FDSState<S> state) {
- macros.get(macroName).forEach(dest);
- }
- }
-
- /*
- * The available macros.
- */
- private Map<String, List<Block>> macros;
-
- /*
- * Where to send blocks from macros.
- */
- private Consumer<Block> dest;
-
- /**
- * Create a new FDS mode for macros.
- *
- * @param macros
- * The macros to use.
- *
- * @param dest
- * The destination for blocks from the macros.
- */
- public MacroFDSMode(Map<String, List<Block>> macros, Consumer<Block> dest) {
- this.macros = macros;
- this.dest = dest;
- }
-
- @Override
- public String[] registeredChars() {
- return macros.keySet().toArray(new String[0]);
- }
-
- @Override
- public boolean hasCommand(String comName) {
- return macros.containsKey(comName);
- }
-
- @Override
- public boolean hasSubmode(String submodeName) {
- return false;
- }
-
- @Override
- public FDSCommand<S> getCommand(String comName) throws FDSException {
- return new MacroFDSCommand(comName);
- }
-
- @Override
- public FDSMode<S> getSubmode(String submodeName) throws FDSException {
- throw new FDSException("Submodes aren't available in macroName modes");
- }
-} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/SimpleFDSMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/SimpleFDSMode.java
deleted file mode 100644
index 8854d8e..0000000
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/SimpleFDSMode.java
+++ /dev/null
@@ -1,173 +0,0 @@
-package bjc.utils.cli.fds;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.Multimap;
-
-import bjc.utils.cli.CommandHelp;
-
-import static java.lang.String.format;
-
-/**
- * Simple implementation of {@link FDSMode}.
- *
- * @author bjculkin
- *
- * @param <S>
- * The FDS state type.
- */
-public class SimpleFDSMode<S> implements FDSMode<S> {
- private Map<String, FDSCommand<S>> commands;
- private Map<String, FDSMode<S>> modes;
- private Multimap<String, CommandHelp> help;
-
- private Set<String> registered;
- private String[] registeredArray;
- private boolean changed;
-
- private String modeName;
-
- /**
- * Create a new empty FDS mode.
- */
- public SimpleFDSMode() {
- commands = new HashMap<>();
- modes = new HashMap<>();
- help = HashMultimap.create();
-
- registered = new HashSet<>();
- changed = true;
- }
-
- /**
- * Create a new empty mode with a given name.
- *
- * @param name
- * The name of the mode.
- */
- public SimpleFDSMode(String name) {
- this();
-
- modeName = name;
- }
-
- @Override
- public String getName() {
- if(modeName == null)
- return FDSMode.super.getName();
- else
- return modeName;
- }
-
- /**
- * Add a command to the mode.
- *
- * @param c
- * The character to bind to the command.
- *
- * @param comm
- * The command to add.
- *
- * @param hlp
- * The help for the command.
- *
- * @throws IllegalArgumentException
- * If the character is already bound to a command.
- */
- public void addCommand(String c, FDSCommand<S> comm, CommandHelp hlp) {
- if(comm == null)
- throw new NullPointerException("Command must not be null");
- else if(commands.containsKey(c))
- throw new IllegalArgumentException(format("Character '%s' is already bound"));
-
- commands.put(c, comm);
- help.put(c, hlp);
-
- registered.add(c);
- if(!changed) changed = true;
- }
-
- /**
- * Add a submode to the mode.
- *
- * @param c
- * The character to bind to the submode.
- *
- * @param mode
- * The submode to add.
- *
- * @param hlp
- * The help for the submode.
- *
- * @throws IllegalArgumentException
- * If the character is already bound to a submode.
- */
- public void addSubmode(String c, FDSMode<S> mode, CommandHelp hlp) {
- if(mode == null)
- throw new NullPointerException("Mode must not be null");
- else if(modes.containsKey(c))
- throw new IllegalArgumentException(format("Character '%s' is already bound"));
-
- modes.put(c, mode);
- help.put(c, hlp);
-
- registered.add(c);
- if(!changed) changed = true;
- }
-
- @Override
- public String[] registeredChars() {
- if(!changed) return registeredArray;
-
- registeredArray = new String[registered.size()];
-
- int i = 0;
- for(String c : registered) {
- registeredArray[i] = c;
-
- i += 1;
- }
-
- changed = false;
-
- return registeredArray;
- }
-
- @Override
- public boolean hasCommand(String c) {
- return commands.containsKey(c);
- }
-
- @Override
- public boolean hasSubmode(String c) {
- return modes.containsKey(c);
- }
-
- @Override
- public FDSCommand<S> getCommand(String c) throws FDSException {
- if(!commands.containsKey(c)) {
- throw new FDSException(String.format("No command bound to '%s'", c));
- }
-
- return commands.get(c);
- }
-
- @Override
- public FDSMode<S> getSubmode(String c) throws FDSException {
- if(!modes.containsKey(c)) {
- throw new FDSException(String.format("No mode bound to '%s'", c));
- }
-
- return modes.get(c);
- }
-
- @Override
- public Collection<CommandHelp> getHelp(String c) {
- return help.get(c);
- }
-} \ No newline at end of file