From: Robin Cheney Date: Tue, 25 Nov 2025 08:18:59 +0000 (+0100) Subject: interfaced available commands X-Git-Url: https://git.eternal.ddnss.de/?a=commitdiff_plain;h=17b3054a65a9f94090f45637cf4c675fcb809d05;p=utils.git interfaced available commands --- diff --git a/pom.xml b/pom.xml index 6a8e605..5352a72 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.ddnss.eternal.utils utils - 1.1 + 1.2 UTF-8 diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/AvailableCommands.java b/src/main/java/de/ddnss/eternal/utils/io/input/AvailableCommands.java index faa2a11..6dbf500 100644 --- a/src/main/java/de/ddnss/eternal/utils/io/input/AvailableCommands.java +++ b/src/main/java/de/ddnss/eternal/utils/io/input/AvailableCommands.java @@ -9,22 +9,22 @@ import java.util.function.Function; * * @since 1.1 */ -public enum AvailableCommands { +public enum AvailableCommands implements CommandEnumInterface { EXIT(() -> { System.exit(0); - }); + }), - // SAY((CommandOption args) -> { - // System.out.println("HI"); - // System.out.println(args.get("SAY")); - // return null; - // }), + SAY((CommandOption 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 command; + public final Command command; /** * Constructs an {@link AvailableCommand} from a result-less {@link Function} @@ -36,7 +36,7 @@ public enum AvailableCommands { * @param arguments A {@link CommandOption} object */ private AvailableCommands(Function, Void> command, CommandOption arguments) { - this.command = new Command(command, arguments); + this.command = new Command(command, arguments); } /** @@ -45,7 +45,7 @@ public enum AvailableCommands { * @param command A {@link Function} that returns null */ private AvailableCommands(Function, Void> command) { - this.command = new Command(command); + this.command = new Command(command); } /** @@ -54,6 +54,11 @@ public enum AvailableCommands { * @param command A {@link Runnable} */ private AvailableCommands(Runnable command) { - this.command = new Command(command); + this.command = new Command(command); + } + + @Override + public Command getCommand() { + return command; } } diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/Command.java b/src/main/java/de/ddnss/eternal/utils/io/input/Command.java index 5f19821..fe83474 100644 --- a/src/main/java/de/ddnss/eternal/utils/io/input/Command.java +++ b/src/main/java/de/ddnss/eternal/utils/io/input/Command.java @@ -5,27 +5,27 @@ import java.util.function.Function; 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 { +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, Void> command; - CommandOption arguments; + final Function, Void> command; + CommandOption arguments; /** * * @param command * @param arguments */ - public Command(Function, Void> command, CommandOption arguments) { + public Command(Function, Void> command, CommandOption arguments) { this.command = command; this.arguments = arguments; } @@ -34,7 +34,7 @@ public class Command { * * @param command A {@link Function} that takes a {@link CommandOption} */ - public Command(Function, Void> command) { + public Command(Function, Void> command) { this.command = command; } @@ -44,7 +44,7 @@ public class Command { * @param command A {@link Runnable} to execute */ public Command(Runnable command) { - this.command = (CommandOption undefined) -> { + this.command = (CommandOption undefined) -> { command.run(); return null; }; @@ -69,7 +69,7 @@ public class Command { * their values * @return the {@link Command} object itself */ - public Command bindArguments(CommandOption args) { + public Command bindArguments(CommandOption args) { this.arguments = args; return this; }; @@ -80,11 +80,11 @@ public class Command { * @param arguments A {@link CommandOption} object holding necessary parameters * @throws InvalidArgumentsError */ - public void exec(CommandOption arguments) throws InvalidArgumentsError { + public void exec(CommandOption 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 diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/CommandEnumInterface.java b/src/main/java/de/ddnss/eternal/utils/io/input/CommandEnumInterface.java new file mode 100644 index 0000000..b84b99e --- /dev/null +++ b/src/main/java/de/ddnss/eternal/utils/io/input/CommandEnumInterface.java @@ -0,0 +1,17 @@ +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(); +} diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/user/CMDCommands.java b/src/main/java/de/ddnss/eternal/utils/io/input/user/CMDCommands.java index a999cf8..567c0e5 100644 --- a/src/main/java/de/ddnss/eternal/utils/io/input/user/CMDCommands.java +++ b/src/main/java/de/ddnss/eternal/utils/io/input/user/CMDCommands.java @@ -5,6 +5,7 @@ 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; /** * Should be useful for capturing console command @@ -20,8 +21,8 @@ public class CMDCommands { * * @see AvailableCommands */ - public static void scan() { - AvailableCommands command; + public static & CommandEnumInterface> void scan(Class enumClass) { + E command; Scanner scanner = UserInput.scanner(); @@ -38,13 +39,13 @@ public class CMDCommands { // 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("SAY", parts[0])).exec(); + command.getCommand().bindArguments(new CommandOption("SAY", parts[0])).exec(); } } }