package ie.dcu.segment.options; /** * A boolean segmentation algorithm parameter. * * @author Kevin McGuinness */ public class ToggleOption extends AbstractOption { /** * Create a toggle option with the given name, description, and default * value. * * @param name * The option name * @param description * A description of the option * @param def * The default value for the option */ public ToggleOption(String name, String description, boolean def) { super(OptionType.Toggle, name, description, def); } public Conversion convert(Object value) { if (value instanceof Boolean) { return valid((Boolean) value); } else if (value != null) { Boolean val = convert(value.toString()); if (val != null) { return valid(val); } return invalid("%s is not a valid boolean, value = %s", getName(), value); } return invalid("value is null"); } /** * Performs a conversion from a string to a boolean. If the value is "yes", * "true" or "1", then the result is true. Otherwise if the * value is "no", "false" or "0" the result is false. Any * other value produces null. The string is trimmed and case * is ignored. * * @param str * The string to convert. * @return true, false or null. */ private Boolean convert(String str) { String v = str.trim(); if (v.equalsIgnoreCase("true") || v.equalsIgnoreCase("yes") || v.equals("1")) { return true; } else if (v.equalsIgnoreCase("false") || v.equalsIgnoreCase("no") || v.equals("0")) { return false; } return null; } }