This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/client/src/main/java/envoy/client/data/commands/SystemCommandBuilder.java

233 lines
7.1 KiB
Java

package envoy.client.data.commands;
import java.util.*;
import java.util.function.Consumer;
/**
* This class acts as a builder for {@link SystemCommand}s.
*
* @author Leon Hofmeister
* @since Envoy Client v0.2-beta
*/
public final class SystemCommandBuilder {
private int numberOfArguments;
private Consumer<List<String>> action;
private List<String> defaults;
private String description;
private int relevance;
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;
}
/**
* @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>
* 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 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;
}
/**
* 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;
}
}