package de.ddnss.eternal.utils.io.error;
-import de.ddnss.eternal.utils.io.input.CommandOption;
+import de.ddnss.eternal.utils.io.input.GenericCommandOption;
/**
- * Thrown when an {@link CommandOption} object does not fit the requirements of
+ * Thrown when an {@link GenericCommandOption} object does not fit the
+ * requirements of
* the function it was passed to
*
* @author Robin Cheney
System.exit(0);
}),
- SAY((CommandOption<String> args) -> {
+ SAY((CommandOption args) -> {
System.out.println("HI");
System.out.println(args.get("SAY"));
return null;
* @param command A {@link Function} that returns null
* @param arguments A {@link CommandOption} object
*/
- private AvailableCommands(Function<CommandOption<String>, Void> command, CommandOption<String> arguments) {
+ private AvailableCommands(Function<CommandOption, Void> command,
+ CommandOption arguments) {
this.command = new Command(command, arguments);
}
*
* @param command A {@link Function} that returns null
*/
- private AvailableCommands(Function<CommandOption<String>, Void> command) {
+ private AvailableCommands(Function<CommandOption, Void> command) {
this.command = new Command(command);
}
/**
* The command to be executed. Must be passed as a runnable to the constructor
* or if parameters are necessary, it must be a {@link Function} that takes a
- * {@link CommandOption} as the single parameter and always returns null
+ * {@link GenericCommandOption} as the single parameter and always returns null
*/
- final Function<CommandOption<String>, Void> command;
- CommandOption<String> arguments;
+ final Function<CommandOption, Void> command;
+ CommandOption arguments;
/**
*
* @param command
* @param arguments
*/
- public Command(Function<CommandOption<String>, Void> command, CommandOption<String> arguments) {
+ public Command(Function<CommandOption, Void> command, CommandOption arguments) {
this.command = command;
this.arguments = arguments;
}
/**
*
- * @param command A {@link Function} that takes a {@link CommandOption}
+ * @param command A {@link Function} that takes a {@link GenericCommandOption}
*/
- public Command(Function<CommandOption<String>, Void> command) {
+ public Command(Function<CommandOption, Void> command) {
this.command = command;
}
* @param command A {@link Runnable} to execute
*/
public Command(Runnable command) {
- this.command = (CommandOption<String> undefined) -> {
+ this.command = (CommandOption undefined) -> {
command.run();
return null;
};
* Execute the command with the previously provided arguments
*
* @apiNote This method has a shorthand property where you can provide
- * {@link CommandOption command options}
- * @see #exec(CommandOption)
- * @see #bindArguments(CommandOption)
+ * {@link GenericCommandOption command options}
+ * @see #exec(GenericCommandOption)
+ * @see #bindArguments(GenericCommandOption)
*/
public void exec() {
this.command.apply(arguments);
/**
* Binds arguments to the command
*
- * @param args A {@link CommandOption} object holding the command arguments and
+ * @param args A {@link GenericCommandOption} object holding the command
+ * arguments and
* their values
* @return the {@link Command} object itself
*/
- public Command bindArguments(CommandOption<String> args) {
+ public Command bindArguments(CommandOption args) {
this.arguments = args;
return this;
};
/**
- * Executes a command with a {@link CommandOption}
+ * Executes a command with a {@link GenericCommandOption}
*
- * @param arguments A {@link CommandOption} object holding necessary parameters
+ * @param arguments A {@link GenericCommandOption} object holding necessary
+ * parameters
* @throws InvalidArgumentsError
*/
- public void exec(CommandOption<String> arguments) throws InvalidArgumentsError {
+ public void exec(CommandOption arguments) throws InvalidArgumentsError {
try {
this.command.apply(arguments);
} catch (NullPointerException String) {
*
* @since 1.1
*/
-public class CommandOption<E> extends HashMap<String, E> {
- public CommandOption(String string, E e) {
+public class CommandOption extends GenericCommandOption<String> {
+ public CommandOption(String string, String e) {
super();
this.put(string, e);
}
--- /dev/null
+package de.ddnss.eternal.utils.io.input;
+
+import java.util.function.Function;
+
+import de.ddnss.eternal.utils.io.error.InvalidArgumentsError;
+
+/**
+ * Class for instantiating commands with parameter type {@code String}
+ *
+ * @author Robin Cheney
+ *
+ * @since 1.2
+ */
+public class GenericCommand<E> {
+ /**
+ * The command to be executed. Must be passed as a runnable to the constructor
+ * or if parameters are necessary, it must be a {@link Function} that takes a
+ * {@link GenericCommandOption} as the single parameter and always returns null
+ */
+ final Function<GenericCommandOption<E>, Void> command;
+ GenericCommandOption<E> arguments;
+
+ /**
+ *
+ * @param command
+ * @param arguments
+ */
+ public GenericCommand(Function<GenericCommandOption<E>, Void> command, GenericCommandOption<E> arguments) {
+ this.command = command;
+ this.arguments = arguments;
+ }
+
+ /**
+ *
+ * @param command A {@link Function} that takes a {@link GenericCommandOption}
+ */
+ public GenericCommand(Function<GenericCommandOption<E>, Void> command) {
+ this.command = command;
+ }
+
+ /**
+ * Constructs a {@link GenericCommand} without any parameters
+ *
+ * @param command A {@link Runnable} to execute
+ */
+ public GenericCommand(Runnable command) {
+ this.command = (GenericCommandOption<E> undefined) -> {
+ command.run();
+ return null;
+ };
+ }
+
+ /**
+ * Execute the command with the previously provided arguments
+ *
+ * @apiNote This method has a shorthand property where you can provide
+ * {@link GenericCommandOption command options}
+ * @see #exec(GenericCommandOption)
+ * @see #bindArguments(GenericCommandOption)
+ */
+ public void exec() {
+ this.command.apply(arguments);
+ };
+
+ /**
+ * Binds arguments to the command
+ *
+ * @param args A {@link GenericCommandOption} object holding the command
+ * arguments and
+ * their values
+ * @return the {@link GenericCommand} object itself
+ */
+ public GenericCommand<E> bindArguments(GenericCommandOption<E> args) {
+ this.arguments = args;
+ return this;
+ };
+
+ /**
+ * Executes a command with a {@link GenericCommandOption}
+ *
+ * @param arguments A {@link GenericCommandOption} object holding necessary
+ * parameters
+ * @throws InvalidArgumentsError
+ */
+ public void exec(GenericCommandOption<E> arguments) throws InvalidArgumentsError {
+ try {
+ this.command.apply(arguments);
+ } catch (NullPointerException String) {
+ throw new InvalidArgumentsError(String);
+ }
+ };
+}
\ No newline at end of file
--- /dev/null
+package de.ddnss.eternal.utils.io.input;
+
+import java.util.HashMap;
+
+/**
+ * Command options container
+ *
+ * @apiNote extends {@link HashMap} whose functions you can use
+ *
+ * @author Robin Cheney
+ *
+ * @since 1.2
+ */
+public class GenericCommandOption<E> extends HashMap<String, E> {
+ public GenericCommandOption(String string, E e) {
+ super();
+ this.put(string, e);
+ }
+
+ public GenericCommandOption() {
+ super();
+ }
+}
\ No newline at end of file
import java.util.Scanner;
import de.ddnss.eternal.utils.io.input.AvailableCommands;
-import de.ddnss.eternal.utils.io.input.CommandOption;
import de.ddnss.eternal.utils.io.input.Command;
import de.ddnss.eternal.utils.io.input.CommandEnumInterface;
+import de.ddnss.eternal.utils.io.input.CommandOption;
/**
* Should be useful for capturing console command
break;
}
- command.getCommand().bindArguments(new CommandOption<String>("SAY", parts[0])).exec();
+ command.getCommand().bindArguments(new CommandOption("SAY", parts[0])).exec();
}
}
}