package ie.dcu.segment.options; /** * An integer segmentation algorithm parameter. * * @author Kevin McGuinness */ public class IntegerOption extends AbstractOption { private final int min; private final int max; /** * Create ain integer 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 IntegerOption(String name, String description, int def) { this(name, description, def, Integer.MIN_VALUE, Integer.MAX_VALUE); } /** * Create an integer 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 IntegerOption(String name, String description, int def, int min, int max) { super(OptionType.Integer, name, description, def); this.min = min; this.max = max; } public Conversion convert(Object value) { if (value instanceof Integer) { return checkRange((Integer) value); } else if (value != null) { try { return checkRange(new Integer(value.toString().trim())); } catch (NumberFormatException e) { // Invalid String mesg = e.getLocalizedMessage(); return invalid("%s is not an integer: %s", getName(), mesg); } } return invalid("%s is null", getName()); } private Conversion checkRange(int val) { if (val >= min && val <= max) { // OK return valid(val); } // Out of range String format = "%s must be in ther range (%d, %d), value is %d"; return invalid(format, getName(), min, max, val); } }