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     * Add the specified {@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     * @return the names of the options in this group as a {@code Collection}
058     */
059    public Collection<String> getNames() {
060        // the key set is the collection of names
061        return optionMap.keySet();
062    }
063
064    /**
065     * @return the options in this group as a {@code Collection}
066     */
067    public Collection<Option> getOptions() {
068        // the values are the collection of options
069        return optionMap.values();
070    }
071
072    /**
073     * @return the selected option name
074     */
075    public String getSelected() {
076        return selected;
077    }
078
079    /**
080     * Tests whether this option group is required.
081     *
082     * @return whether this option group is required
083     */
084    public boolean isRequired() {
085        return required;
086    }
087
088    /**
089     * @param required specifies if this group is required
090     */
091    public void setRequired(final boolean required) {
092        this.required = required;
093    }
094
095    /**
096     * Sets the selected option of this group to {@code name}.
097     *
098     * @param option the option that is selected
099     * @throws AlreadySelectedException if an option from this group has already been selected.
100     */
101    public void setSelected(final Option option) throws AlreadySelectedException {
102        if (option == null) {
103            // reset the option previously selected
104            selected = null;
105            return;
106        }
107        // if no option has already been selected or the
108        // same option is being reselected then set the
109        // selected member variable
110        if (selected != null && !selected.equals(option.getKey())) {
111            throw new AlreadySelectedException(this, option);
112        }
113        selected = option.getKey();
114    }
115
116    /**
117     * Returns the stringified version of this OptionGroup.
118     *
119     * @return the stringified representation of this group
120     */
121    @Override
122    public String toString() {
123        final StringBuilder buff = new StringBuilder();
124        final Iterator<Option> iter = getOptions().iterator();
125        buff.append("[");
126        while (iter.hasNext()) {
127            final Option option = iter.next();
128            if (option.getOpt() != null) {
129                buff.append("-");
130                buff.append(option.getOpt());
131            } else {
132                buff.append("--");
133                buff.append(option.getLongOpt());
134            }
135
136            if (option.getDescription() != null) {
137                buff.append(Char.SP);
138                buff.append(option.getDescription());
139            }
140
141            if (iter.hasNext()) {
142                buff.append(", ");
143            }
144        }
145        buff.append("]");
146        return buff.toString();
147    }
148}