package ie.dcu.segment.options; /** * A decimal (floating point) segmentation algorithm parameter. * * @author Kevin McGuinness */ public class DecimalOption extends AbstractOption { private final double min; private final double max; /** * Create a decimal 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 DecimalOption(String name, String description, double def) { this(name, description, def, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); } /** * Create a decimal option with the given name, description, default * value, minimum allowable value, and maximum allowable value. * * @param name * The option name * @param description * A description of the option * @param def * The default value for the option * @param min * The minimum allowable value for the option * @param max * The maximum allowable value for the option */ public DecimalOption(String name, String description, double def, double min, double max) { super(OptionType.Decimal, name, description, def); this.min = min; this.max = max; } public Conversion convert(Object value) { if (value instanceof Double) { return valid((Double) value); } else if (value != null) { try { return checkRange(new Double(value.toString().trim())); } catch (NumberFormatException e) { // Invalid String mesg = e.getLocalizedMessage(); return invalid("%s is not an decimal number: %s", getName(), mesg); } } return invalid("value is null"); } private Conversion checkRange(double val) { if (val >= min && val <= max) { // OK return valid(val); } // Out of range String format = "%s must be in ther range (%f, %f), value is %f"; return invalid(format, getName(), min, max, val); } }