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
018package org.apache.commons.cli;
019
020import java.io.Serializable;
021import java.util.Collection;
022import java.util.Iterator;
023import java.util.LinkedHashMap;
024import java.util.Map;
025
026/**
027 * A group of mutually exclusive options.
028 */
029public class OptionGroup implements Serializable {
030
031    /** The serial version UID. */
032    private static final long serialVersionUID = 1L;
033
034    /** Hold the options */
035    private final Map<String, Option> optionMap = new LinkedHashMap<>();
036
037    /** The name of the selected option */
038    private String selected;
039
040    /** Specified whether this group is required */
041    private boolean required;
042
043    /**
044     * Adds the given {@code Option} to this group.
045     *
046     * @param option the option to add to this group
047     * @return this option group with the option added
048     */
049    public OptionGroup addOption(final Option option) {
050        // key - option name
051        // value - the option
052        optionMap.put(option.getKey(), option);
053        return this;
054    }
055
056    /**
057     * Gets the names of the options in this group as a {@code Collection}.
058     *
059     * @return the names of the options in this group as a {@code Collection}.
060     */
061    public Collection<String> getNames() {
062        // the key set is the collection of names
063        return optionMap.keySet();
064    }
065
066    /**
067     * Gets the options in this group as a {@code Collection}.
068     *
069     * @return the options in this group as a {@code Collection}.
070     */
071    public Collection<Option> getOptions() {
072        // the values are the collection of options
073        return optionMap.values();
074    }
075
076    /**
077     * Gets the selected option name.
078     *
079     * If the selected option is deprecated <em>no warning is logged</em>.
080     * @return the selected option name.
081     */
082    public String getSelected() {
083        return selected;
084    }
085
086    /**
087     * Tests whether this option group is required.
088     *
089     * @return whether this option group is required
090     */
091    public boolean isRequired() {
092        return required;
093    }
094
095    /**
096     * Tests whether an option is selected.
097     *
098     *  If an option is selected and is deprecated <em>no warning is logged</em>.
099     * @return whether whether an option is selected.
100     * @since 1.9.0
101     */
102    public boolean isSelected() {
103        return selected != null;
104    }
105
106    /**
107     * Sets whether this group is required.
108     *
109     * @param required whether this group is required.
110     */
111    public void setRequired(final boolean required) {
112        this.required = required;
113    }
114
115    /**
116     * Sets the selected option of this group to {@code name}.
117     *
118     * If the selected option is deprecated <em>no warning is logged</em>.
119     * @param option the option that is selected
120     * @throws AlreadySelectedException if an option from this group has already been selected.
121     */
122    public void setSelected(final Option option) throws AlreadySelectedException {
123        if (option == null) {
124            // reset the option previously selected
125            selected = null;
126            return;
127        }
128        // if no option has already been selected or the
129        // same option is being reselected then set the
130        // selected member variable
131        if (selected != null && !selected.equals(option.getKey())) {
132            throw new AlreadySelectedException(this, option);
133        }
134        selected = option.getKey();
135    }
136
137    /**
138     * Returns the stringified version of this OptionGroup.
139     *
140     * @return the stringified representation of this group
141     */
142    @Override
143    public String toString() {
144        final StringBuilder buff = new StringBuilder();
145        final Iterator<Option> iter = getOptions().iterator();
146        buff.append("[");
147        while (iter.hasNext()) {
148            final Option option = iter.next();
149            if (option.getOpt() != null) {
150                buff.append("-");
151                buff.append(option.getOpt());
152            } else {
153                buff.append("--");
154                buff.append(option.getLongOpt());
155            }
156
157            if (option.getDescription() != null) {
158                buff.append(Char.SP);
159                buff.append(option.getDescription());
160            }
161
162            if (iter.hasNext()) {
163                buff.append(", ");
164            }
165        }
166        buff.append("]");
167        return buff.toString();
168    }
169}