package ie.dcu.segment.options; /** * Abstract implementation of the {@link Option} interface. * * @author Kevin McGuinness */ public abstract class AbstractOption implements Option { private final OptionType type; private final String name; private final String description; private final T defaultValue; private T value; protected AbstractOption( OptionType type, String name, String description, T defaultValue) { // Preconditions assert (type != null); assert (name != null); assert (description != null); assert (defaultValue != null); // Assign this.type = type; this.name = name; this.description = description; this.defaultValue = defaultValue; this.value = defaultValue; } public OptionType getType() { return type; } public String getName() { return name; } public String getDescription() { return description; } public T getDefaultValue() { return defaultValue; } public T getValue() { return value; } public void setValue(Object value) { if (value == null) { this.value = defaultValue; } else { Conversion conv = convert(value); if (conv.isOk()) { this.value = conv.getResult(); } else { this.value = defaultValue; } } } public T[] values() { return null; } protected Conversion valid(T result) { return new Conversion(result, null); } protected Conversion invalid(String message) { return new Conversion(null, message); } protected Conversion invalid(String format, Object... args) { return new Conversion(null, String.format(format, args)); } }