2020-03-02 19:16:28 +01:00
|
|
|
package envoy.data;
|
|
|
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* Contains a single {@link Config} value as well as the corresponding command line arguments and
|
|
|
|
* its default value.
|
2020-08-23 13:39:31 +02:00
|
|
|
* <p>
|
|
|
|
* All {@code ConfigItem}s are automatically mandatory.
|
2020-03-02 19:16:28 +01:00
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @param <T> the type of the config item's value
|
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
2020-08-22 13:37:07 +02:00
|
|
|
public final class ConfigItem<T> {
|
2020-03-02 19:16:28 +01:00
|
|
|
|
|
|
|
private final String commandLong, commandShort;
|
|
|
|
private final Function<String, T> parseFunction;
|
|
|
|
|
|
|
|
private T value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a {@link ConfigItem}.
|
|
|
|
*
|
|
|
|
* @param commandLong the long command line argument to set this value
|
|
|
|
* @param commandShort the short command line argument to set this value
|
2020-10-19 18:17:51 +02:00
|
|
|
* @param parseFunction the {@code Function<String, T>} that parses the value from a string
|
2020-03-02 19:16:28 +01:00
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
2020-08-23 13:39:31 +02:00
|
|
|
public ConfigItem(String commandLong, String commandShort, Function<String, T> parseFunction) {
|
2020-03-02 19:16:28 +01:00
|
|
|
this.commandLong = commandLong;
|
|
|
|
this.commandShort = commandShort;
|
|
|
|
this.parseFunction = parseFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses this {@ConfigItem}'s value from a string.
|
|
|
|
*
|
|
|
|
* @param input the string to parse from
|
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public void parse(String input) {
|
|
|
|
value = parseFunction.apply(input);
|
|
|
|
}
|
2020-03-02 19:16:28 +01:00
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* @return The long command line argument to set the value of this {@link ConfigItem}
|
2020-03-02 19:16:28 +01:00
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
|
|
|
public String getCommandLong() { return commandLong; }
|
|
|
|
|
|
|
|
/**
|
2020-10-19 18:17:51 +02:00
|
|
|
* @return The short command line argument to set the value of this {@link ConfigItem}
|
2020-03-02 19:16:28 +01:00
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
|
|
|
public String getCommandShort() { return commandShort; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the value of this {@link ConfigItem}
|
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
2020-10-19 18:17:51 +02:00
|
|
|
public T get() {
|
|
|
|
return value;
|
|
|
|
}
|
2020-03-02 19:16:28 +01:00
|
|
|
|
2020-08-22 13:15:42 +02:00
|
|
|
/**
|
|
|
|
* @param value the value to set
|
|
|
|
* @since Envoy Common v0.2-beta
|
|
|
|
*/
|
|
|
|
protected void setValue(T value) { this.value = value; }
|
2020-03-02 19:16:28 +01:00
|
|
|
}
|