From a013802d1117fe4d3f2ff06353877a014b50e8c8 Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 23 Jul 2020 16:37:28 +0200 Subject: [PATCH] Added onCall interface, InterruptEvent, and a relevance measurement --- .../commands/InterruptingSystemCommand.java | 52 +++++++++++++++++++ .../envoy/client/data/commands/OnCall.java | 37 +++++++++++++ .../client/data/commands/SystemCommand.java | 28 +++++++++- .../data/commands/SystemCommandsMap.java | 1 + .../envoy/client/event/InterruptEvent.java | 28 ++++++++++ 5 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 client/src/main/java/envoy/client/data/commands/InterruptingSystemCommand.java create mode 100644 client/src/main/java/envoy/client/data/commands/OnCall.java create mode 100644 client/src/main/java/envoy/client/event/InterruptEvent.java diff --git a/client/src/main/java/envoy/client/data/commands/InterruptingSystemCommand.java b/client/src/main/java/envoy/client/data/commands/InterruptingSystemCommand.java new file mode 100644 index 0000000..90434c4 --- /dev/null +++ b/client/src/main/java/envoy/client/data/commands/InterruptingSystemCommand.java @@ -0,0 +1,52 @@ +package envoy.client.data.commands; + +import java.util.function.Function; +import java.util.function.Supplier; + +import envoy.client.event.InterruptEvent; +import envoy.event.EventBus; + +/** + * This is a Envoy-specific subclass that allows + * + * Project: envoy-client
+ * File: InterruptingSystemCommand.java
+ * Created: 23.07.2020
+ * + * @author Leon Hofmeister + * @since Envoy Client v0.2-beta + */ +public class InterruptingSystemCommand extends SystemCommand { + + private static final EventBus eventBus = EventBus.getInstance(); + + /** + * The reason why an {@link InterruptEvent} will be dispatched from this class. + */ + public static final String interruptionReason = "command executed"; + + /** + * Constructs a new {@code SystemCommand}. + * + * @param action the action that should be performed + * @param numberOfArguments the amount of arguments that need to be parsed for + * the underlying function + * @param description the description of this {@code SystemCommand} + * @since Envoy Client v0.2-beta + */ + public InterruptingSystemCommand(Function action, int numberOfArguments, String description) { + super(action, numberOfArguments, description); + } + + @Override + public void onCall() { + super.onCall(); + eventBus.dispatch(new InterruptEvent(interruptionReason)); + } + + @Override + public void onCall(Supplier consumer) { + super.onCall(consumer); + eventBus.dispatch(new InterruptEvent(interruptionReason)); + } +} diff --git a/client/src/main/java/envoy/client/data/commands/OnCall.java b/client/src/main/java/envoy/client/data/commands/OnCall.java new file mode 100644 index 0000000..3dbc18e --- /dev/null +++ b/client/src/main/java/envoy/client/data/commands/OnCall.java @@ -0,0 +1,37 @@ +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. + *

+ * Project: envoy-client
+ * File: OnCall.java
+ * Created: 23.07.2020
+ * + * @author Leon Hofmeister + * @since Envoy Client v0.2-beta + */ +public interface OnCall { + + /** + * Performs class specific actions when a {@link SystemCommand} has been called. + * + * The action that should be performed when this {@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 9a3eecd..f0b6a29 100644 --- a/client/src/main/java/envoy/client/data/commands/SystemCommand.java +++ b/client/src/main/java/envoy/client/data/commands/SystemCommand.java @@ -1,6 +1,7 @@ package envoy.client.data.commands; import java.util.function.Function; +import java.util.function.Supplier; /** * This class is the base class of all {@code SystemCommands} and contains an @@ -20,7 +21,7 @@ import java.util.function.Function; * @author Leon Hofmeister * @since Envoy Client v0.2-beta */ -public final class SystemCommand { +public class SystemCommand implements OnCall { /** * This variable stores the amount of arguments that need to be parsed for the @@ -39,8 +40,10 @@ public final class SystemCommand { protected final String description; + protected int relevance = 0; + /** - * Constructs a new {@code NoArgSystemCommand} that takes no arguments. + * Constructs a new {@code SystemCommand}. * * @param action the action that should be performed * @param numberOfArguments the amount of arguments that need to be parsed for @@ -72,4 +75,25 @@ public final class SystemCommand { * @since Envoy Client v0.2-beta */ public String getDescription() { return description; } + + /** + * @return the relevance + * @since Envoy Client v0.2-beta + */ + public int getRelevance() { return relevance; } + + /** + * @param relevance the relevance to set + * @since Envoy Client v0.2-beta + */ + public void setRelevance(int relevance) { this.relevance = relevance; } + + @Override + public void onCall() { relevance++; } + + @Override + public void onCall(Supplier consumer) { + onCall(); + consumer.get(); + } } diff --git a/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java b/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java index 2eb45f2..a9c6c84 100644 --- a/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java +++ b/client/src/main/java/envoy/client/data/commands/SystemCommandsMap.java @@ -265,6 +265,7 @@ public class SystemCommandsMap { try { systemCommand.getAction().apply(arguments); commandExecuted = true; + systemCommand.onCall(); } catch (final Exception e) { logger.log(Level.WARNING, "The system command " + getCommand(input) + " threw an exception: ", e); } diff --git a/client/src/main/java/envoy/client/event/InterruptEvent.java b/client/src/main/java/envoy/client/event/InterruptEvent.java new file mode 100644 index 0000000..7676267 --- /dev/null +++ b/client/src/main/java/envoy/client/event/InterruptEvent.java @@ -0,0 +1,28 @@ +package envoy.client.event; + +import envoy.event.Event; + +/** + * This event serves the purpose of interrupting something so that it will not + * be finished. + *

+ * Project: envoy-client
+ * File: InterruptEvent.java
+ * Created: 23.07.2020
+ * + * @author Leon Hofmeister + * @since Envoy Client v0.2-beta + */ +public class InterruptEvent extends Event { + + private static final long serialVersionUID = 1L; + + /** + * Creates a new {@link InterruptEvent}. + * + * @param reason the reason why this event exists -> allows finer sieving + * @since Envoy Client v0.2-beta + */ + public InterruptEvent(String reason) { super(reason); } + +}