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