Added onCall interface, InterruptEvent, and a relevance measurement

This commit is contained in:
delvh 2020-07-23 16:37:28 +02:00
parent 6a1a9ecdbb
commit 42184c47f7
5 changed files with 144 additions and 2 deletions

View File

@ -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: <strong>envoy-client</strong><br>
* File: <strong>InterruptingSystemCommand.java</strong><br>
* Created: <strong>23.07.2020</strong><br>
*
* @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<String[], Void> 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<Void> consumer) {
super.onCall(consumer);
eventBus.dispatch(new InterruptEvent(interruptionReason));
}
}

View File

@ -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.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>OnCall.java</strong><br>
* Created: <strong>23.07.2020</strong><br>
*
* @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<Void> consumer);
}

View File

@ -1,6 +1,7 @@
package envoy.client.data.commands; package envoy.client.data.commands;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
/** /**
* This class is the base class of all {@code SystemCommands} and contains an * 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 * @author Leon Hofmeister
* @since Envoy Client v0.2-beta * @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 * 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 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 action the action that should be performed
* @param numberOfArguments the amount of arguments that need to be parsed for * @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 * @since Envoy Client v0.2-beta
*/ */
public String getDescription() { return description; } 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<Void> consumer) {
onCall();
consumer.get();
}
} }

View File

@ -265,6 +265,7 @@ public class SystemCommandsMap {
try { try {
systemCommand.getAction().apply(arguments); systemCommand.getAction().apply(arguments);
commandExecuted = true; commandExecuted = true;
systemCommand.onCall();
} catch (final Exception e) { } catch (final Exception e) {
logger.log(Level.WARNING, "The system command " + getCommand(input) + " threw an exception: ", e); logger.log(Level.WARNING, "The system command " + getCommand(input) + " threw an exception: ", e);
} }

View File

@ -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.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>InterruptEvent.java</strong><br>
* Created: <strong>23.07.2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Client v0.2-beta
*/
public class InterruptEvent extends Event<String> {
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); }
}