117 lines
3.2 KiB
Java
117 lines
3.2 KiB
Java
package envoy.client.data.commands;
|
|
|
|
import java.util.*;
|
|
import java.util.function.Consumer;
|
|
|
|
/**
|
|
* This class is the base class of all {@code SystemCommands} and contains an action and a number of
|
|
* arguments that should be used as input for this function. No {@code SystemCommand} can return
|
|
* anything. Every {@code SystemCommand} must have as argument type {@code List<String>} so that the
|
|
* words following the indicator String can be used as input of the function. This approach has one
|
|
* limitation:<br>
|
|
* <b>Order matters!</b> Changing the order of arguments will likely result in unexpected behavior.
|
|
*
|
|
* @author Leon Hofmeister
|
|
* @since Envoy Client v0.2-beta
|
|
*/
|
|
public final class SystemCommand implements Callable {
|
|
|
|
protected int relevance;
|
|
|
|
/**
|
|
* The argument count of the command.
|
|
*/
|
|
protected final int numberOfArguments;
|
|
|
|
/**
|
|
* This function takes a {@code List<String>} as argument because automatically
|
|
* {@code SystemCommand#numberOfArguments} words following the necessary command will be put
|
|
* into this list.
|
|
*
|
|
* @see String#split(String)
|
|
*/
|
|
protected final Consumer<List<String>> action;
|
|
|
|
protected final String description;
|
|
|
|
protected final List<String> defaults;
|
|
|
|
/**
|
|
* Constructs a new {@code SystemCommand}.
|
|
*
|
|
* @param action the action performed by the command
|
|
* @param numberOfArguments the argument count accepted by the action
|
|
* @param defaults the default values for the corresponding arguments
|
|
* @param description the description of this {@code SystemCommand}
|
|
* @since Envoy Client v0.2-beta
|
|
*/
|
|
public SystemCommand(Consumer<List<String>> action, int numberOfArguments,
|
|
List<String> defaults, String description) {
|
|
this.numberOfArguments = numberOfArguments;
|
|
this.action = action;
|
|
this.defaults = defaults == null ? new ArrayList<>() : defaults;
|
|
this.description = description;
|
|
}
|
|
|
|
/**
|
|
* @return the argument count of the command
|
|
* @since Envoy Client v0.2-beta
|
|
*/
|
|
public int getNumberOfArguments() { return numberOfArguments; }
|
|
|
|
/**
|
|
* @return the description
|
|
* @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 call(List<String> arguments) {
|
|
action.accept(arguments);
|
|
++relevance;
|
|
}
|
|
|
|
/**
|
|
* @return the defaults
|
|
* @since Envoy Client v0.2-beta
|
|
*/
|
|
public List<String> getDefaults() { return defaults; }
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(action);
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj)
|
|
return true;
|
|
if (obj == null)
|
|
return false;
|
|
if (getClass() != obj.getClass())
|
|
return false;
|
|
final var other = (SystemCommand) obj;
|
|
return Objects.equals(action, other.action);
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "SystemCommand [relevance=" + relevance + ", numberOfArguments=" + numberOfArguments
|
|
+ ", "
|
|
+ (description != null ? "description=" + description + ", " : "")
|
|
+ (defaults != null ? "defaults=" + defaults : "") + "]";
|
|
}
|
|
}
|