]> Git Server - utils.git/commitdiff
interfaced available commands
authorRobin Cheney <cheneyr@eternal.ddnss.de>
Tue, 25 Nov 2025 08:18:59 +0000 (09:18 +0100)
committerRobin Cheney <cheneyr@eternal.ddnss.de>
Tue, 25 Nov 2025 08:18:59 +0000 (09:18 +0100)
pom.xml
src/main/java/de/ddnss/eternal/utils/io/input/AvailableCommands.java
src/main/java/de/ddnss/eternal/utils/io/input/Command.java
src/main/java/de/ddnss/eternal/utils/io/input/CommandEnumInterface.java [new file with mode: 0644]
src/main/java/de/ddnss/eternal/utils/io/input/user/CMDCommands.java

diff --git a/pom.xml b/pom.xml
index 6a8e605190e8d31519842ef13bfb4bb297ea1e4c..5352a727bde70c239f62f20cbbaf516d8fcaa026 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
 
     <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>
index faa2a11f0875e05036bde9445f857b1c25a72ba7..6dbf500d73fe27b5f73c4062c14e4ea520fb57fa 100644 (file)
@@ -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<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}
@@ -36,7 +36,7 @@ public enum AvailableCommands {
      * @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);
     }
 
     /**
@@ -45,7 +45,7 @@ public enum AvailableCommands {
      * @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);
     }
 
     /**
@@ -54,6 +54,11 @@ public enum AvailableCommands {
      * @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;
     }
 }
index 5f19821ee2f6497a0da15672b45811d03851997b..fe834748424c8b9991e210c8ec9d22a2418b3995 100644 (file)
@@ -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<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;
     }
@@ -34,7 +34,7 @@ public class Command<E> {
      * 
      * @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;
     }
 
@@ -44,7 +44,7 @@ public class Command<E> {
      * @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;
         };
@@ -69,7 +69,7 @@ public class Command<E> {
      *             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;
     };
@@ -80,11 +80,11 @@ public class Command<E> {
      * @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
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 (file)
index 0000000..b84b99e
--- /dev/null
@@ -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();
+}
index a999cf8294dd61075601b68dab1175cab2b5749c..567c0e55fccf948dea1b5c65c67d9657b4c2513a 100644 (file)
@@ -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 <E extends Enum<E> & CommandEnumInterface> void scan(Class<E> 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<String>("SAY", parts[0])).exec();
+            command.getCommand().bindArguments(new CommandOption<String>("SAY", parts[0])).exec();
         }
     }
 }