    001    package org.maltparser.core.options.option;
    002
    003    import org.maltparser.core.exception.MaltChainedException;
    004    import org.maltparser.core.options.OptionException;
    005    import org.maltparser.core.options.OptionGroup;
    006
    007    /**
    008     * An integer option is an option that can only contain an integer value.
    009     *
    010     * @author Johan Hall
    011     * @since 1.0
    012    **/
    013    public class IntegerOption extends Option{
    014            private int defaultValue = 0;
    015
    016            /**
    017             * Creates an integer option description
    018             *
    019             * @param group a reference to the option group.
    020             * @param name  the name of the option.
    021             * @param shortDescription      a short description of the option.
    022             * @param flag  a short string that can be used in the command line.
    023             * @param usage a string that explains the usage of the option.
    024             * @param defaultValue  a default value string (must be an integer value).
    025             * @throws OptionException
    026             */
    027            public IntegerOption(OptionGroup group,
    028                            String name,
    029                            String shortDescription,
    030                            String flag,
    031                            String usage,
    032                            String defaultValue) throws MaltChainedException {
    033                    super(group, name, shortDescription, flag, usage);
    034                    setDefaultValue(defaultValue);
    035            }
    036
    037            /* (non-Javadoc)
    038             * @see org.maltparser.core.options.option.Option#getValueObject(java.lang.String)
    039             */
    040            public Object getValueObject(String value) throws MaltChainedException {
    041                    try {
    042                            return new Integer(Integer.parseInt(value));
    043                    } catch (NumberFormatException e) {
    044                            throw new OptionException("Illegal integer value '"+value+"' for the '"+getName()+"' option. ", e);
    045                    }
    046            }
    047
    048            /* (non-Javadoc)
    049             * @see org.maltparser.core.options.option.Option#getDefaultValueObject()
    050             */
    051            public Object getDefaultValueObject() throws MaltChainedException {
    052                    return new Integer(defaultValue);
    053            }
    054
    055            /* (non-Javadoc)
    056             * @see org.maltparser.core.options.option.Option#setDefaultValue(java.lang.String)
    057             */
    058            public void setDefaultValue(String defaultValue) throws MaltChainedException {
    059                    try {
    060                            this.defaultValue = Integer.parseInt(defaultValue);
    061                    } catch (NumberFormatException e) {
    062                            throw new OptionException("Illegal integer default value '"+defaultValue+"' for the '"+getName()+"' option. ", e);
    063                    }
    064            }
    065
    066            /* (non-Javadoc)
    067             * @see org.maltparser.core.options.option.Option#getDefaultValueString()
    068             */
    069            public String getDefaultValueString() {
    070                    return Integer.toString(defaultValue);
    071            }
    072
    073            /* (non-Javadoc)
    074             * @see org.maltparser.core.options.option.Option#getStringRepresentation(java.lang.Object)
    075             */
    076            public String getStringRepresentation(Object value) {
    077                    if (value instanceof Integer) {
    078                            return value.toString();
    079                    }
    080                    return null;
    081            }
    082
    083            /* (non-Javadoc)
    084             * @see org.maltparser.core.options.option.Option#toString()
    085             */
    086            public String toString() {
    087                    final StringBuilder sb = new StringBuilder();
    088                    sb.append(super.toString());
    089                    sb.append("-----------------------------------------------------------------------------\n");
    090                    return sb.toString();
    091            }
    092    }