From 08ce7a9f864dd77bf20aba2f459072329d49272a Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 1 Oct 2020 09:22:50 +0200 Subject: [PATCH] Apply suggestions from @kske 1 --- .../envoy/client/data/commands/Callable.java | 39 +++++++++++++++++++ .../envoy/client/data/commands/OnCall.java | 30 -------------- .../client/data/commands/SystemCommand.java | 26 +++---------- .../data/commands/SystemCommandMap.java | 9 +---- 4 files changed, 45 insertions(+), 59 deletions(-) create mode 100644 client/src/main/java/envoy/client/data/commands/Callable.java delete mode 100644 client/src/main/java/envoy/client/data/commands/OnCall.java diff --git a/client/src/main/java/envoy/client/data/commands/Callable.java b/client/src/main/java/envoy/client/data/commands/Callable.java new file mode 100644 index 0000000..ebd843d --- /dev/null +++ b/client/src/main/java/envoy/client/data/commands/Callable.java @@ -0,0 +1,39 @@ +package envoy.client.data.commands; + +import java.util.List; + +/** + * This interface defines an action that should be performed when a system + * command gets called. + * + * @author Leon Hofmeister + * @since Envoy Client v0.2-beta + */ +public interface Callable { + + /** + * Performs the instance specific action when a {@link SystemCommand} has been + * called. + * + * @param arguments the arguments that should be passed to the + * {@link SystemCommand} + * @since Envoy Client v0.2-beta + */ + void call(List arguments); + + /** + * Performs the instance specific action when a {@link SystemCommand} has been + * called and additionally enables the user to execute his own action once it + * has been called. + * + * @param arguments the arguments that should be passed to the + * {@link SystemCommand} + * @param additionalAction the action to perform when this system command has + * been called + * @since Envoy Client v0.2-beta + */ + default void call(List arguments, Runnable additionalAction) { + call(arguments); + additionalAction.run(); + } +} diff --git a/client/src/main/java/envoy/client/data/commands/OnCall.java b/client/src/main/java/envoy/client/data/commands/OnCall.java deleted file mode 100644 index 941b4fa..0000000 --- a/client/src/main/java/envoy/client/data/commands/OnCall.java +++ /dev/null @@ -1,30 +0,0 @@ -package envoy.client.data.commands; - -import java.util.function.Supplier; - -/** - * This interface defines an action that should be performed when a system - * command gets called. - * - * @author Leon Hofmeister - * @since Envoy Client v0.2-beta - */ -public interface OnCall { - - /** - * Performs class specific actions when a {@link SystemCommand} has been called. - * - * @since Envoy Client v0.2-beta - */ - void onCall(); - - /** - * Performs actions that can only be performed by classes that are not - * {@link SystemCommand}s when a SystemCommand has been called. - * - * @param consumer the action to perform when this {@link SystemCommand} has - * been called - * @since Envoy Client v0.2-beta - */ - void onCall(Supplier consumer); -} diff --git a/client/src/main/java/envoy/client/data/commands/SystemCommand.java b/client/src/main/java/envoy/client/data/commands/SystemCommand.java index b5dae0b..7a788dd 100644 --- a/client/src/main/java/envoy/client/data/commands/SystemCommand.java +++ b/client/src/main/java/envoy/client/data/commands/SystemCommand.java @@ -1,7 +1,7 @@ package envoy.client.data.commands; import java.util.*; -import java.util.function.*; +import java.util.function.Consumer; /** * This class is the base class of all {@code SystemCommands} and contains an @@ -17,7 +17,7 @@ import java.util.function.*; * @author Leon Hofmeister * @since Envoy Client v0.2-beta */ -public final class SystemCommand implements OnCall { +public final class SystemCommand implements Callable { protected int relevance; @@ -55,12 +55,6 @@ public final class SystemCommand implements OnCall { this.description = description; } - /** - * @return the action that should be performed - * @since Envoy Client v0.2-beta - */ - public Consumer> getAction() { return action; } - /** * @return the argument count of the command * @since Envoy Client v0.2-beta @@ -85,20 +79,10 @@ public final class SystemCommand implements OnCall { */ public void setRelevance(int relevance) { this.relevance = relevance; } - /** - * Increments the relevance of this {@code SystemCommand}. - */ @Override - public void onCall() { relevance++; } - - /** - * Increments the relevance of this {@code SystemCommand} and executes the - * supplier. - */ - @Override - public void onCall(Supplier consumer) { - onCall(); - consumer.get(); + public void call(List arguments) { + action.accept(arguments); + ++relevance; } /** diff --git a/client/src/main/java/envoy/client/data/commands/SystemCommandMap.java b/client/src/main/java/envoy/client/data/commands/SystemCommandMap.java index a102f38..ddb2d5e 100644 --- a/client/src/main/java/envoy/client/data/commands/SystemCommandMap.java +++ b/client/src/main/java/envoy/client/data/commands/SystemCommandMap.java @@ -155,14 +155,7 @@ public final class SystemCommandMap { final var arguments = extractArguments(input, systemCommand); // Executing the function try { - systemCommand.getAction().accept(arguments); - systemCommand.onCall(); - } catch (final IndexOutOfBoundsException e) { - logger.log(Level.SEVERE, - String.format( - "System command %s threw an IndexOutOfBoundsException. The most likely cause is a lower number of arguments than expected. It could also be a problem of the action performed: ", - command), - e); + systemCommand.call(arguments); } catch (final NumberFormatException e) { logger.log(Level.INFO, String.format(