2020-07-24 11:09:05 +02:00
|
|
|
package envoy.client.data.commands;
|
|
|
|
|
2020-09-23 17:03:32 +02:00
|
|
|
import java.util.*;
|
2020-07-24 11:09:05 +02:00
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class acts as a builder for {@link SystemCommand}s.
|
|
|
|
*
|
|
|
|
* @author Leon Hofmeister
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
2020-08-22 13:37:07 +02:00
|
|
|
public final class SystemCommandBuilder {
|
2020-07-24 11:09:05 +02:00
|
|
|
|
|
|
|
private int numberOfArguments;
|
|
|
|
private Consumer<List<String>> action;
|
|
|
|
private List<String> defaults;
|
|
|
|
private String description;
|
|
|
|
private int relevance;
|
|
|
|
|
2020-09-23 17:03:32 +02:00
|
|
|
private final SystemCommandMap commandsMap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new {@code SystemCommandsBuilder} without underlying
|
|
|
|
* {@link SystemCommandMap}.
|
|
|
|
*
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommandBuilder() { this(null); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param commandsMap the map to use when calling build (optional)
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommandBuilder(SystemCommandMap commandsMap) { this.commandsMap = commandsMap; }
|
|
|
|
|
2020-07-24 11:09:05 +02:00
|
|
|
/**
|
|
|
|
* @param numberOfArguments the numberOfArguments to set
|
|
|
|
* @return this {@code SystemCommandBuilder}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommandBuilder setNumberOfArguments(int numberOfArguments) {
|
|
|
|
this.numberOfArguments = numberOfArguments;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param action the action to set
|
|
|
|
* @return this {@code SystemCommandBuilder}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommandBuilder setAction(Consumer<List<String>> action) {
|
|
|
|
this.action = action;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param description the description to set
|
|
|
|
* @return this {@code SystemCommandBuilder}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommandBuilder setDescription(String description) {
|
|
|
|
this.description = description;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param relevance the relevance to set
|
|
|
|
* @return this {@code SystemCommandBuilder}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommandBuilder setRelevance(int relevance) {
|
|
|
|
this.relevance = relevance;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param defaults the defaults to set
|
|
|
|
* @return this {@code SystemCommandBuilder}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommandBuilder setDefaults(String... defaults) {
|
|
|
|
this.defaults = List.of(defaults);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets all values stored.
|
|
|
|
*
|
|
|
|
* @return this {@code SystemCommandBuilder}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommandBuilder reset() {
|
|
|
|
numberOfArguments = 0;
|
|
|
|
action = null;
|
|
|
|
defaults = new ArrayList<>();
|
|
|
|
description = "";
|
|
|
|
relevance = 0;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a {@code SystemCommand} based upon the previously entered data.
|
|
|
|
*
|
|
|
|
* @return the built {@code SystemCommand}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommand build() { return build(true); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a {@code SystemCommand} based upon the previously entered data.<br>
|
|
|
|
* {@code SystemCommand#numberOfArguments} will be set to 0, regardless of the
|
|
|
|
* previous value.<br>
|
|
|
|
* At the end, this {@code SystemCommandBuilder} will be reset.
|
|
|
|
*
|
|
|
|
* @return the built {@code SystemCommand}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommand buildNoArg() {
|
|
|
|
numberOfArguments = 0;
|
|
|
|
return build(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a {@code SystemCommand} based upon the previously entered data.<br>
|
|
|
|
* {@code SystemCommand#numberOfArguments} will be set to use the rest of the
|
|
|
|
* string as argument, regardless of the previous value.<br>
|
|
|
|
* At the end, this {@code SystemCommandBuilder} will be reset.
|
|
|
|
*
|
|
|
|
* @return the built {@code SystemCommand}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommand buildRemainingArg() {
|
|
|
|
numberOfArguments = -1;
|
|
|
|
return build(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a {@code SystemCommand} based upon the previously entered data.<br>
|
2020-09-23 17:03:32 +02:00
|
|
|
* Automatically adds the built object to the given map.
|
2020-07-24 11:09:05 +02:00
|
|
|
* At the end, this {@code SystemCommandBuilder} <b>can</b> be reset but must
|
|
|
|
* not be.
|
|
|
|
*
|
|
|
|
* @param reset whether this {@code SystemCommandBuilder} should be reset
|
|
|
|
* afterwards.<br>
|
|
|
|
* This can be useful if another command wants to execute something
|
|
|
|
* similar
|
|
|
|
* @return the built {@code SystemCommand}
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommand build(boolean reset) {
|
|
|
|
final var sc = new SystemCommand(action, numberOfArguments, defaults, description);
|
|
|
|
sc.setRelevance(relevance);
|
|
|
|
if (reset) reset();
|
|
|
|
return sc;
|
|
|
|
}
|
2020-09-23 17:03:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a {@code SystemCommand} based upon the previously entered data.
|
|
|
|
* Automatically adds the built object to the given map.
|
|
|
|
*
|
|
|
|
* @param command the command under which to store the SystemCommand in the
|
|
|
|
* {@link SystemCommandMap}
|
|
|
|
* @return the built {@code SystemCommand}
|
|
|
|
* @throws NullPointerException if no map has been assigned to this builder
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommand build(String command) { return build(command, true); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a {@code SystemCommand} based upon the previously entered data.<br>
|
|
|
|
* Automatically adds the built object to the given map.
|
|
|
|
* {@code SystemCommand#numberOfArguments} will be set to 0, regardless of the
|
|
|
|
* previous value.<br>
|
|
|
|
* At the end, this {@code SystemCommandBuilder} will be reset.
|
|
|
|
*
|
|
|
|
* @param command the command under which to store the SystemCommand in the
|
|
|
|
* {@link SystemCommandMap}
|
|
|
|
* @return the built {@code SystemCommand}
|
|
|
|
* @throws NullPointerException if no map has been assigned to this builder
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommand buildNoArg(String command) {
|
|
|
|
numberOfArguments = 0;
|
|
|
|
return build(command, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a {@code SystemCommand} based upon the previously entered data.<br>
|
|
|
|
* Automatically adds the built object to the given map.
|
|
|
|
* {@code SystemCommand#numberOfArguments} will be set to use the rest of the
|
|
|
|
* string as argument, regardless of the previous value.<br>
|
|
|
|
* At the end, this {@code SystemCommandBuilder} will be reset.
|
|
|
|
*
|
|
|
|
* @param command the command under which to store the SystemCommand in the
|
|
|
|
* {@link SystemCommandMap}
|
|
|
|
* @return the built {@code SystemCommand}
|
|
|
|
* @throws NullPointerException if no map has been assigned to this builder
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommand buildRemainingArg(String command) {
|
|
|
|
numberOfArguments = -1;
|
|
|
|
return build(command, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a {@code SystemCommand} based upon the previously entered data.<br>
|
|
|
|
* Automatically adds the built object to the given map.
|
|
|
|
* At the end, this {@code SystemCommandBuilder} <b>can</b> be reset but must
|
|
|
|
* not be.
|
|
|
|
*
|
|
|
|
* @param command the command under which to store the SystemCommand in the
|
|
|
|
* {@link SystemCommandMap}
|
|
|
|
* @param reset whether this {@code SystemCommandBuilder} should be reset
|
|
|
|
* afterwards.<br>
|
|
|
|
* This can be useful if another command wants to execute
|
|
|
|
* something
|
|
|
|
* similar
|
|
|
|
* @return the built {@code SystemCommand}
|
|
|
|
* @throws NullPointerException if no map has been assigned to this builder
|
|
|
|
* @since Envoy Client v0.2-beta
|
|
|
|
*/
|
|
|
|
public SystemCommand build(String command, boolean reset) {
|
|
|
|
final var sc = new SystemCommand(action, numberOfArguments, defaults, description);
|
|
|
|
sc.setRelevance(relevance);
|
|
|
|
if (commandsMap != null) commandsMap.add(command, sc);
|
|
|
|
else throw new NullPointerException("No map in SystemCommandsBuilder present");
|
|
|
|
if (reset) reset();
|
|
|
|
return sc;
|
|
|
|
}
|
2020-07-24 11:09:05 +02:00
|
|
|
}
|