]> Git Server - utils.git/commitdiff
Version 1.1
authorRobin Cheney <cheneyr@eternal.ddnss.de>
Thu, 20 Nov 2025 08:56:39 +0000 (09:56 +0100)
committerRobin Cheney <cheneyr@eternal.ddnss.de>
Thu, 20 Nov 2025 08:56:39 +0000 (09:56 +0100)
13 files changed:
.gitignore
pom.xml
src/main/java/de/ddnss/eternal/utils/ArrayUtils.java
src/main/java/de/ddnss/eternal/utils/StringUtils.java
src/main/java/de/ddnss/eternal/utils/Test.java [deleted file]
src/main/java/de/ddnss/eternal/utils/io/error/InvalidArgumentsError.java [new file with mode: 0644]
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/CommandInterface.java [deleted file]
src/main/java/de/ddnss/eternal/utils/io/input/CommandOption.java
src/main/java/de/ddnss/eternal/utils/io/input/ParameterizedCommand.java [deleted file]
src/main/java/de/ddnss/eternal/utils/io/input/user/CMDCommands.java
src/main/java/de/ddnss/eternal/utils/io/input/user/UserInput.java

index 7bb4c2f90e28f86aa1430b3f52b17e36b4a9221c..5c661bfb5db9b1793fee09049254834c7c3ec08b 100644 (file)
@@ -1,2 +1,3 @@
 target/*
-de/ddnss/eternal/utils/Test.java
\ No newline at end of file
+*/Test.java
+.vscode/*
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index c5a0ca4ea24c06769ef1d3ad68c3ae48dec8d3b4..b9d7a8fac94f888a3a75ff257d589e397d8f7672 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -1,12 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>de.ddnss.eternal.utils</groupId>
     <artifactId>utils</artifactId>
-    <version>1.0</version>
+    <version>1.1</version>
 
     <properties>
         <maven.compiler.source>17</maven.compiler.source>
index 930d97f0131190e570a933a150415e5188dcddb7..e9540a42c0a0cb8062f81769129c549baf5fd599 100644 (file)
@@ -2,16 +2,23 @@ package de.ddnss.eternal.utils;
 
 import java.util.ArrayList;
 
-public abstract class ArrayUtils {
-    public static Object[] reverse(Object[] array) {
+/**
+ * This has some methods for working with Arrays.
+ * 
+ * @author Robin Cheney
+ * 
+ * @since 1.1
+ */
+public abstract class ArrayUtils<E> {
+    public E[] reverse(E[] array) {
 
-        ArrayList<Object> outArray = new ArrayList<Object>();
+        ArrayList<E> outArray = new ArrayList<E>();
 
         for (int i = 0; i < array.length; i++) {
 
             // prepend each character
             outArray.add(array[i]);
         }
-        return outArray.toArray(new Object[0]);
+        return outArray.toArray(array);
     }
 }
index eefae3381929aa24a3b7fc78052566e8f07aa0f2..526f821b162afd83bb1bbd9e262cbfa39614b907 100644 (file)
@@ -1,6 +1,19 @@
 package de.ddnss.eternal.utils;
 
+/**
+ * String utilities
+ * 
+ * @author Robin Cheney
+ * 
+ * @since 1.0
+ */
 public abstract class StringUtils {
+    /**
+     * Returns the given {@link String} in reversed order
+     * 
+     * @param string The normal {@link String}
+     * @return The reversed {@link String}
+     */
     public static String reverse(String string) {
 
         String outString = "";
diff --git a/src/main/java/de/ddnss/eternal/utils/Test.java b/src/main/java/de/ddnss/eternal/utils/Test.java
deleted file mode 100644 (file)
index 0ea89ca..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-package de.ddnss.eternal.utils;
-
-import de.ddnss.eternal.utils.io.input.user.CMDCommands;
-
-public class Test {
-    public static void main(String[] cmdargs) {
-
-        System.out.println("Enter command:");
-
-        while (true) {
-            // System.out.print("> ");
-            // String line = scanner.nextLine().trim();
-
-            // // Skip empty input
-            // if (line.isEmpty())
-            // continue;
-
-            // // Split into tokens (command + args)
-            // String[] parts = line.split("\\s+");
-            CMDCommands.scan();
-        }
-    }
-
-    // private static void handleSay(String[] args) {
-    // if (args.length == 0) {
-    // System.out.println("Usage: say <message>");
-    // return;
-    // }
-    // System.out.println(String.join(" ", args));
-    // }
-
-    // private static void handleAdd(String[] args) {
-    // if (args.length != 2) {
-    // System.out.println("Usage: add <num1> <num2>");
-    // return;
-    // }
-
-    // try {
-    // int a = Integer.parseInt(args[0]);
-    // int b = Integer.parseInt(args[1]);
-    // System.out.println("Result: " + (a + b));
-    // } catch (NumberFormatException e) {
-    // System.out.println("Arguments must be numbers.");
-    // }
-    // }
-}
diff --git a/src/main/java/de/ddnss/eternal/utils/io/error/InvalidArgumentsError.java b/src/main/java/de/ddnss/eternal/utils/io/error/InvalidArgumentsError.java
new file mode 100644 (file)
index 0000000..cb200c0
--- /dev/null
@@ -0,0 +1,17 @@
+package de.ddnss.eternal.utils.io.error;
+
+import de.ddnss.eternal.utils.io.input.CommandOption;
+
+/**
+ * Thrown when an {@link CommandOption} object does not fit the requirements of
+ * the function it was passed to
+ * 
+ * @author Robin Cheney
+ * 
+ * @since 1.1
+ */
+public class InvalidArgumentsError extends Exception {
+    public InvalidArgumentsError(Exception e) {
+        super(e);
+    }
+}
index 9d02d761d86825cca47f033af888e544add47c35..58d90845ea917aa7f4730e1106edc6e42e69e503 100644 (file)
@@ -2,19 +2,58 @@ package de.ddnss.eternal.utils.io.input;
 
 import java.util.function.Function;
 
+/**
+ * An enum of available {@link Command commands}
+ * 
+ * @author Robin Cheney
+ * 
+ * @since 1.1
+ */
 public enum AvailableCommands {
+    EXIT(() -> {
+        System.exit(0);
+    }),
+
     SAY((CommandOption<String> args) -> {
+        System.out.println("HI");
         System.out.println(args.get("SAY"));
         return null;
+    }),
+
+    GREET(() -> {
+        System.out.println("Hi");
     });
 
-    public final CommandInterface command;
+    public final Command<String> command;
 
+    /**
+     * Constructs an {@link AvailableCommand} from a result-less {@link Function}
+     * with given {@link CommandOption}<{@link String}>
+     * 
+     * @apiNote This would immediately bind the parameters to the function. This is
+     *          not very useful
+     * @param command   A {@link Function} that returns null
+     * @param arguments A {@link CommandOption} object
+     */
     private AvailableCommands(Function<CommandOption<String>, Void> command, CommandOption<String> arguments) {
-        this.command = new ParameterizedCommand<String>(command, arguments);
+        this.command = new Command<String>(command, arguments);
     }
 
+    /**
+     * Constructs an {@link AvailableCommand} from a result-less {@link Function}
+     * 
+     * @param command A {@link Function} that returns null
+     */
     private AvailableCommands(Function<CommandOption<String>, Void> command) {
-        this.command = new ParameterizedCommand<String>(command);
+        this.command = new Command<String>(command);
+    }
+
+    /**
+     * Constructs an {@link AvailableCommand} from a {@link Runnable}
+     * 
+     * @param command A {@link Runnable}
+     */
+    private AvailableCommands(Runnable command) {
+        this.command = new Command<String>(command);
     }
 }
index 429298e066286f92197ada6282b934c5a9864814..5f19821ee2f6497a0da15672b45811d03851997b 100644 (file)
@@ -1,19 +1,90 @@
 package de.ddnss.eternal.utils.io.input;
 
-public final class Command implements CommandInterface {
-    final Runnable command;
+import java.util.function.Function;
 
-    public Command(Runnable command) {
+import de.ddnss.eternal.utils.io.error.InvalidArgumentsError;
+
+/**
+ * Class for instantiating commands with parameter type {@code E}
+ * 
+ * @author Robin Cheney
+ * 
+ * @since 1.1
+ */
+public class Command<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 CommandOption} as the single parameter and always returns null
+     */
+    final Function<CommandOption<E>, Void> command;
+    CommandOption<E> arguments;
+
+    /**
+     * 
+     * @param command
+     * @param arguments
+     */
+    public Command(Function<CommandOption<E>, Void> command, CommandOption<E> arguments) {
         this.command = command;
+        this.arguments = arguments;
     }
 
-    @Override
-    public void exec() {
-        this.command.run();
+    /**
+     * 
+     * @param command A {@link Function} that takes a {@link CommandOption}
+     */
+    public Command(Function<CommandOption<E>, Void> command) {
+        this.command = command;
     }
 
-    @Override
-    public void exec(CommandOption<? extends Object> args) {
-        this.command.run();
+    /**
+     * Constructs a {@link Command} without any parameters
+     * 
+     * @param command A {@link Runnable} to execute
+     */
+    public Command(Runnable command) {
+        this.command = (CommandOption<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 CommandOption command options}
+     * @see #exec(CommandOption)
+     * @see #bindArguments(CommandOption)
+     */
+    public void exec() {
+        this.command.apply(arguments);
+    };
+
+    /**
+     * Binds arguments to the command
+     * 
+     * @param args A {@link CommandOption} object holding the command arguments and
+     *             their values
+     * @return the {@link Command} object itself
+     */
+    public Command<E> bindArguments(CommandOption<E> args) {
+        this.arguments = args;
+        return this;
+    };
+
+    /**
+     * Executes a command with a {@link CommandOption}
+     * 
+     * @param arguments A {@link CommandOption} object holding necessary parameters
+     * @throws InvalidArgumentsError
+     */
+    public void exec(CommandOption<E> arguments) throws InvalidArgumentsError {
+        try {
+            this.command.apply(arguments);
+        } catch (NullPointerException e) {
+            throw new InvalidArgumentsError(e);
+        }
     };
 }
\ No newline at end of file
diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/CommandInterface.java b/src/main/java/de/ddnss/eternal/utils/io/input/CommandInterface.java
deleted file mode 100644 (file)
index c43491c..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-package de.ddnss.eternal.utils.io.input;
-
-public interface CommandInterface {
-    public void exec();
-
-    public void exec(CommandOption<? extends Object> args);
-}
index d1d1efe70fc68a63146b7ad479046c4c3adb8104..ac55e28e9ecee5e472743862fda2191412eee746 100644 (file)
@@ -2,6 +2,15 @@ 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.1
+ */
 public class CommandOption<E> extends HashMap<String, E> {
     public CommandOption(String string, E e) {
         super();
diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/ParameterizedCommand.java b/src/main/java/de/ddnss/eternal/utils/io/input/ParameterizedCommand.java
deleted file mode 100644 (file)
index 8cc0f72..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-package de.ddnss.eternal.utils.io.input;
-
-import java.util.function.Function;
-
-public final class ParameterizedCommand<E extends Object> implements CommandInterface {
-    final Function<CommandOption<E>, Void> command;
-    CommandOption<E> arguments;
-
-    public ParameterizedCommand(Function<CommandOption<E>, Void> command, CommandOption<E> arguments) {
-        this.command = command;
-        this.arguments = arguments;
-    }
-
-    public ParameterizedCommand(Function<CommandOption<E>, Void> command) {
-        this.command = command;
-    }
-
-    @Override
-    public void exec() {
-        this.command.apply(arguments);
-    };
-
-    @Override
-    public void exec(CommandOption<? extends Object> arguments) {
-        this.command.apply(arguments);
-    }
-
-    // @Override
-    // public void exec(Object args) {
-    // this.command.apply(args);
-    // };
-}
\ No newline at end of file
index c94b392c4e47fb66193f94b38ed2c9f01a36b565..67b6b29523fc96324eddcbdb537e51088d6cefd1 100644 (file)
@@ -5,7 +5,20 @@ import java.util.Scanner;
 import de.ddnss.eternal.utils.io.input.AvailableCommands;
 import de.ddnss.eternal.utils.io.input.CommandOption;
 
+/**
+ * Should be useful for capturing console command
+ * 
+ * @author Robin Cheney
+ * 
+ * @since 1.1
+ */
 public class CMDCommands {
+    /**
+     * Creates a scanner that can and will read input and try to evaluate it as a
+     * {@link Command}
+     * 
+     * @see AvailableCommands
+     */
     public static void scan() {
         AvailableCommands command;
 
@@ -29,7 +42,8 @@ public class CMDCommands {
                 System.out.println("Unknown command: " + parts[0]);
                 break;
             }
-            command.command.exec(new CommandOption<String>("SAY", parts[0]));
+
+            command.command.bindArguments(new CommandOption<String>("SAY", parts[0])).exec();
         }
     }
 }
index 401bdaf11d20617bd4c11d4b05ebb851c2c4bf2d..e83d71e48a0375634b7d404696c96db2742e2c72 100644 (file)
@@ -2,11 +2,27 @@ package de.ddnss.eternal.utils.io.input.user;
 
 import java.util.Scanner;
 
+/**
+ * Utility class for utilising user input
+ * 
+ * @author Robin Cheney
+ * 
+ * @since 1.1
+ */
 public class UserInput {
+
+    /**
+     * Prints a prompt and returns the next line, the user puts in
+     * 
+     * @param prompt A prompt to be printed before the user input
+     * @return the user input
+     * 
+     * @see Scanner#nextLine()
+     */
     public static String prompt(String prompt) {
         System.out.println(prompt);
         Scanner input = scanner();
-        String result = input.next();
+        String result = input.nextLine();
         input.close();
         return result;
     }