Apply suggestions from @kske 1

This commit is contained in:
Leon Hofmeister 2020-10-01 09:22:50 +02:00
parent 7468947728
commit 08ce7a9f86
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C
4 changed files with 45 additions and 59 deletions

View File

@ -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<String> 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<String> arguments, Runnable additionalAction) {
call(arguments);
additionalAction.run();
}
}

View File

@ -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<Void> consumer);
}

View File

@ -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<List<String>> 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<Void> consumer) {
onCall();
consumer.get();
public void call(List<String> arguments) {
action.accept(arguments);
++relevance;
}
/**

View File

@ -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(