package envoy.data; import java.util.function.Function; /** * Contains a single {@link Config} value as well as the corresponding command line arguments and * its default value. *

* All {@code ConfigItem}s are automatically mandatory. * * @author Kai S. K. Engelbart * @param the type of the config item's value * @since Envoy Common v0.1-beta */ public final class ConfigItem { private final String commandLong, commandShort; private final Function 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 * @param parseFunction the {@code Function} that parses the value from a string * @since Envoy Common v0.1-beta */ public ConfigItem(String commandLong, String commandShort, Function parseFunction) { 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 */ public void parse(String input) { value = parseFunction.apply(input); } /** * @return The long command line argument to set the value of this {@link ConfigItem} * @since Envoy Common v0.1-beta */ public String getCommandLong() { return commandLong; } /** * @return The short command line argument to set the value of this {@link ConfigItem} * @since Envoy Common v0.1-beta */ public String getCommandShort() { return commandShort; } /** * @return the value of this {@link ConfigItem} * @since Envoy Common v0.1-beta */ public T get() { return value; } /** * @param value the value to set * @since Envoy Common v0.2-beta */ protected void setValue(T value) { this.value = value; } }