    001    /**
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *     http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017
    018    package org.apache.commons.cli;
    019
    020    import java.io.Serializable;
    021    import java.util.Collection;
    022    import java.util.HashMap;
    023    import java.util.Iterator;
    024    import java.util.Map;
    025
    026    /**
    027     * A group of mutually exclusive options.
    028     *
    029     * @author John Keyes ( john at integralsource.com )
    030     * @version $Revision: 680644 $, $Date: 2008-07-29 01:13:48 -0700 (Tue, 29 Jul 2008) $
    031     */
    032    public class OptionGroup implements Serializable
    033    {
    034        private static final long serialVersionUID = 1L;
    035
    036        /** hold the options */
    037        private Map optionMap = new HashMap();
    038
    039        /** the name of the selected option */
    040        private String selected;
    041
    042        /** specified whether this group is required */
    043        private boolean required;
    044
    045        /**
    046         * Add the specified <code>Option</code> to this group.
    047         *
    048         * @param option the option to add to this group
    049         * @return this option group with the option added
    050         */
    051        public OptionGroup addOption(Option option)
    052        {
    053            // key   - option name
    054            // value - the option
    055            optionMap.put(option.getKey(), option);
    056
    057            return this;
    058        }
    059
    060        /**
    061         * @return the names of the options in this group as a
    062         * <code>Collection</code>
    063         */
    064        public Collection getNames()
    065        {
    066            // the key set is the collection of names
    067            return optionMap.keySet();
    068        }
    069
    070        /**
    071         * @return the options in this group as a <code>Collection</code>
    072         */
    073        public Collection getOptions()
    074        {
    075            // the values are the collection of options
    076            return optionMap.values();
    077        }
    078
    079        /**
    080         * Set the selected option of this group to <code>name</code>.
    081         *
    082         * @param option the option that is selected
    083         * @throws AlreadySelectedException if an option from this group has
    084         * already been selected.
    085         */
    086        public void setSelected(Option option) throws AlreadySelectedException
    087        {
    088            // if no option has already been selected or the
    089            // same option is being reselected then set the
    090            // selected member variable
    091            if (selected == null || selected.equals(option.getOpt()))
    092            {
    093                selected = option.getOpt();
    094            }
    095            else
    096            {
    097                throw new AlreadySelectedException(this, option);
    098            }
    099        }
    100
    101        /**
    102         * @return the selected option name
    103         */
    104        public String getSelected()
    105        {
    106            return selected;
    107        }
    108
    109        /**
    110         * @param required specifies if this group is required
    111         */
    112        public void setRequired(boolean required)
    113        {
    114            this.required = required;
    115        }
    116
    117        /**
    118         * Returns whether this option group is required.
    119         *
    120         * @return whether this option group is required
    121         */
    122        public boolean isRequired()
    123        {
    124            return required;
    125        }
    126
    127        /**
    128         * Returns the stringified version of this OptionGroup.
    129         *
    130         * @return the stringified representation of this group
    131         */
    132        public String toString()
    133        {
    134            StringBuffer buff = new StringBuffer();
    135
    136            Iterator iter = getOptions().iterator();
    137
    138            buff.append("[");
    139
    140            while (iter.hasNext())
    141            {
    142                Option option = (Option) iter.next();
    143
    144                if (option.getOpt() != null)
    145                {
    146                    buff.append("-");
    147                    buff.append(option.getOpt());
    148                }
    149                else
    150                {
    151                    buff.append("--");
    152                    buff.append(option.getLongOpt());
    153                }
    154
    155                buff.append(" ");
    156                buff.append(option.getDescription());
    157
    158                if (iter.hasNext())
    159                {
    160                    buff.append(", ");
    161                }
    162            }
    163
    164            buff.append("]");
    165
    166            return buff.toString();
    167        }
    168    }