Option.java

package edu.udel.cis.vsl.tass.config;

/**
 * An instance of this class represents a command-line option for TASS.
 * 
 * The type of the option specifies how the command-line value should be parsed.
 * A BOOLEAN value should appear as "true" or "false" and will create a
 * corresponding Java Boolean value. A NUMBER value could be an integer or a
 * real, and TASS will try to figure out which based on the presence of a
 * decimal point or some other indicator.
 * 
 * @author siegel
 * 
 */
public abstract class Option {

	/**
	 * Is this option applicable only to a verification run, only to a
	 * comparision run, or to any run of TASS?
	 */
	public enum OptionKind {
		VERIFY, COMPARE, RUN
	}

	public enum OptionType {
		BOOLEAN, INTEGER, DOUBLE, STRING
	};

	private String name;

	private OptionKind kind;

	private OptionType type;

	private String description;

	private String defaultValue;

	public Option(String name, OptionKind kind, OptionType type,
			String description) {
		this.name = name;
		this.kind = kind;
		this.type = type;
		this.description = description;
	}

	protected void setDefaultValue(String string) {
		this.defaultValue = string;
	}

	public String name() {
		return name;
	}

	public OptionType type() {
		return type;
	}

	public OptionKind kind() {
		return kind;
	}

	public boolean appliesToVerify() {
		return kind == OptionKind.VERIFY || kind == OptionKind.RUN;
	}

	public boolean appliesToCompare() {
		return kind == OptionKind.COMPARE || kind == OptionKind.RUN;
	}

	public String description() {
		return description;
	}

	public String defaultValue() {
		return defaultValue;
	}

	private void checkApplicability(RunConfiguration configuration) {
		if (configuration instanceof VerifyConfiguration && !appliesToVerify()) {
			throw new IllegalArgumentException("Option " + name
					+ " does not apply to verify command.");
		}
		if (configuration instanceof CompareConfiguration
				&& !appliesToCompare()) {
			throw new IllegalArgumentException("Option " + name
					+ " does not apply to compare command.");
		}
	}

	public abstract void set(RunConfiguration configuration, Object value);

	public abstract Object get(RunConfiguration configuration);

	public void setCheck(RunConfiguration configuration, Object value) {
		checkApplicability(configuration);
		set(configuration, value);
	}

	public String toString() {
		String result = "-" + name;

		if (type == OptionType.BOOLEAN) {
			result += " or -" + name + "=BOOLEAN";
		} else {
			result += "=" + type;
		}
		if (defaultValue != null) {
			result += " (default: " + defaultValue + ")";
		}
		result += "\n    " + description;
		return result;
	}
}