diff options
Diffstat (limited to 'dicore3/command/src/main/java/io/dico/dicore/command/predef/PredefinedCommand.java')
-rw-r--r-- | dicore3/command/src/main/java/io/dico/dicore/command/predef/PredefinedCommand.java | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/predef/PredefinedCommand.java b/dicore3/command/src/main/java/io/dico/dicore/command/predef/PredefinedCommand.java new file mode 100644 index 0000000..4e7ba07 --- /dev/null +++ b/dicore3/command/src/main/java/io/dico/dicore/command/predef/PredefinedCommand.java @@ -0,0 +1,49 @@ +package io.dico.dicore.command.predef; + +import io.dico.dicore.command.CommandBuilder; +import io.dico.dicore.command.ExtendedCommand; +import io.dico.dicore.command.ICommandAddress; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +/** + * Marker class for commands that are generated. These commands can be replaced using methods in {@link CommandBuilder} + */ +public abstract class PredefinedCommand<T extends PredefinedCommand<T>> extends ExtendedCommand<T> { + static final Map<String, Consumer<ICommandAddress>> predefinedCommandGenerators = new HashMap<>(); + + /** + * Get a predefined command + * + * @param name the name + * @return the subscriber + */ + public static Consumer<ICommandAddress> getPredefinedCommandGenerator(String name) { + return predefinedCommandGenerators.get(name); + } + + /** + * Register a predefined command + * + * @param name the name + * @param consumer the generator which adds the child to the address + * @return true if and only if the subscriber was registered (false if the name exists) + */ + public static boolean registerPredefinedCommandGenerator(String name, Consumer<ICommandAddress> consumer) { + return predefinedCommandGenerators.putIfAbsent(name, consumer) == null; + } + + static { + registerPredefinedCommandGenerator("help", HelpCommand::registerAsChild); + registerPredefinedCommandGenerator("syntax", SyntaxCommand::registerAsChild); + } + + public PredefinedCommand() { + } + + public PredefinedCommand(boolean modifiable) { + super(modifiable); + } +} |