<groupId>de.ddnss.eternal.utils</groupId>
<artifactId>utils</artifactId>
- <version>1.1</version>
+ <version>1.2</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
*
* @since 1.1
*/
-public enum AvailableCommands {
+public enum AvailableCommands implements CommandEnumInterface {
EXIT(() -> {
System.exit(0);
- });
+ }),
- // SAY((CommandOption<String> args) -> {
- // System.out.println("HI");
- // System.out.println(args.get("SAY"));
- // return null;
- // }),
+ SAY((CommandOption<String> args) -> {
+ System.out.println("HI");
+ System.out.println(args.get("SAY"));
+ return null;
+ }),
- // GREET(() -> {
- // System.out.println("Hi");
- // });
+ GREET(() -> {
+ System.out.println("Hi");
+ });
- public final Command<String> command;
+ public final Command command;
/**
* Constructs an {@link AvailableCommand} from a result-less {@link Function}
* @param arguments A {@link CommandOption} object
*/
private AvailableCommands(Function<CommandOption<String>, Void> command, CommandOption<String> arguments) {
- this.command = new Command<String>(command, arguments);
+ this.command = new Command(command, arguments);
}
/**
* @param command A {@link Function} that returns null
*/
private AvailableCommands(Function<CommandOption<String>, Void> command) {
- this.command = new Command<String>(command);
+ this.command = new Command(command);
}
/**
* @param command A {@link Runnable}
*/
private AvailableCommands(Runnable command) {
- this.command = new Command<String>(command);
+ this.command = new Command(command);
+ }
+
+ @Override
+ public Command getCommand() {
+ return command;
}
}
import de.ddnss.eternal.utils.io.error.InvalidArgumentsError;
/**
- * Class for instantiating commands with parameter type {@code E}
+ * Class for instantiating commands with parameter type {@code String}
*
* @author Robin Cheney
*
* @since 1.1
*/
-public class Command<E> {
+public class 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
*/
- final Function<CommandOption<E>, Void> command;
- CommandOption<E> arguments;
+ final Function<CommandOption<String>, Void> command;
+ CommandOption<String> arguments;
/**
*
* @param command
* @param arguments
*/
- public Command(Function<CommandOption<E>, Void> command, CommandOption<E> arguments) {
+ public Command(Function<CommandOption<String>, Void> command, CommandOption<String> arguments) {
this.command = command;
this.arguments = arguments;
}
*
* @param command A {@link Function} that takes a {@link CommandOption}
*/
- public Command(Function<CommandOption<E>, Void> command) {
+ public Command(Function<CommandOption<String>, Void> command) {
this.command = command;
}
* @param command A {@link Runnable} to execute
*/
public Command(Runnable command) {
- this.command = (CommandOption<E> undefined) -> {
+ this.command = (CommandOption<String> undefined) -> {
command.run();
return null;
};
* their values
* @return the {@link Command} object itself
*/
- public Command<E> bindArguments(CommandOption<E> args) {
+ public Command bindArguments(CommandOption<String> args) {
this.arguments = args;
return this;
};
* @param arguments A {@link CommandOption} object holding necessary parameters
* @throws InvalidArgumentsError
*/
- public void exec(CommandOption<E> arguments) throws InvalidArgumentsError {
+ public void exec(CommandOption<String> arguments) throws InvalidArgumentsError {
try {
this.command.apply(arguments);
- } catch (NullPointerException e) {
- throw new InvalidArgumentsError(e);
+ } catch (NullPointerException String) {
+ throw new InvalidArgumentsError(String);
}
};
}
\ No newline at end of file
--- /dev/null
+package de.ddnss.eternal.utils.io.input;
+
+/**
+ * Implement this interface in every enum that contains available commands
+ *
+ * @since 1.2
+ *
+ * @author Robin Cheney
+ */
+public interface CommandEnumInterface {
+ /**
+ * Getter for the commmand
+ *
+ * @return the command object
+ */
+ Command getCommand();
+}
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;
/**
* Should be useful for capturing console command
*
* @see AvailableCommands
*/
- public static void scan() {
- AvailableCommands command;
+ public static <E extends Enum<E> & CommandEnumInterface> void scan(Class<E> enumClass) {
+ E command;
Scanner scanner = UserInput.scanner();
// Split into tokens (command + args)
String[] parts = line.split("\\s+");
try {
- command = AvailableCommands.valueOf(parts[0].toUpperCase()); // first word
+ command = Enum.valueOf(enumClass, parts[0].toUpperCase()); // first word
} catch (IllegalArgumentException e) {
System.out.println("Unknown command: " + parts[0]);
break;
}
- command.command.bindArguments(new CommandOption<String>("SAY", parts[0])).exec();
+ command.getCommand().bindArguments(new CommandOption<String>("SAY", parts[0])).exec();
}
}
}