    001    package org.maltparser.core.options;
    002
    003    import java.util.Collection;
    004    import java.util.HashMap;
    005    import java.util.TreeSet;
    006
    007    import org.maltparser.core.options.option.Option;
    008
    009
    010    /**
    011     * An option group categories a group of options.
    012     *
    013     * @author Johan Hall
    014     * @since 1.0
    015    **/
    016    public class OptionGroup {
    017            private String name;
    018            private HashMap<String, Option> options;
    019
    020            public static int toStringSetting = 0;
    021            public final static int WITHGROUPNAME = 0;
    022            public final static int NOGROUPNAME = 1;
    023
    024            /**
    025             * Creates an option group with an option group name.
    026             *
    027             * @param name  The name of the option group
    028             */
    029            public OptionGroup(String name) {
    030                    setName(name);
    031                    options = new HashMap<String, Option>();
    032            }
    033
    034            /**
    035             * Returns the name of the option group
    036             *
    037             * @return      the name of the option group
    038             */
    039            public String getName() {
    040                    return name;
    041            }
    042
    043            /**
    044             * Sets the name of the option group
    045             *
    046             * @param name  the name of the option group
    047             */
    048            public void setName(String name) {
    049                    this.name = name.toLowerCase();
    050            }
    051
    052            /**
    053             * Adds an option to the option group.
    054             *
    055             * @param option        an option
    056             * @throws OptionException
    057             */
    058            public void addOption(Option option) throws OptionException {
    059                    if (option.getName() == null || option.getName().equals("")) {
    060                            throw new OptionException("The option name is null or contains the empty string. ");
    061                    } else if (options.containsKey(option.getName().toLowerCase())) {
    062                            throw new OptionException("The option name already exists for that option group. ");
    063                    } else {
    064                            options.put(option.getName().toLowerCase(), option);
    065                    }
    066            }
    067
    068            /**
    069             * Returns the option according to the option name.
    070             *
    071             * @param optionname    an option name
    072             * @return      an option, <i>null</i> if the option name can't be found
    073             */
    074            public Option getOption(String optionname) {
    075                    return options.get(optionname);
    076            }
    077
    078            /**
    079             * Returns all options for this option group.
    080             *
    081             * @return      a list of options
    082             */
    083            public Collection<Option> getOptionList() {
    084                    return options.values();
    085            }
    086
    087            /* (non-Javadoc)
    088             * @see java.lang.Object#toString()
    089             */
    090            public String toString() {
    091                    final StringBuilder sb = new StringBuilder();
    092    //              for (String value : new TreeSet<String>(options.keySet())) {
    093    //                      sb.append("super.put(\"");
    094    //                      sb.append("--");
    095    //                      sb.append(name);
    096    //                      sb.append("-");
    097    //                      sb.append(options.get(value).getName());
    098    //                      sb.append("\", \"");
    099    //                      sb.append(options.get(value).getDefaultValueString());
    100    //                      sb.append("\");\n");
    101    //              }
    102
    103                    if (OptionGroup.toStringSetting == OptionGroup.WITHGROUPNAME) {
    104                            sb.append("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
    105                            sb.append("+ " + name+"\n");
    106                            sb.append("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
    107                    }
    108
    109                    for (String value : new TreeSet<String>(options.keySet())) {
    110                            sb.append(options.get(value).toString());
    111                    }
    112                    return sb.toString();
    113            }
    114    }