target/*
-de/ddnss/eternal/utils/Test.java
\ No newline at end of file
+*/Test.java
+.vscode/*
\ No newline at end of file
<?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>
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);
}
}
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 = "";
+++ /dev/null
-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.");
- // }
- // }
-}
--- /dev/null
+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);
+ }
+}
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);
}
}
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
+++ /dev/null
-package de.ddnss.eternal.utils.io.input;
-
-public interface CommandInterface {
- public void exec();
-
- public void exec(CommandOption<? extends Object> args);
-}
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();
+++ /dev/null
-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
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;
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();
}
}
}
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;
}