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    /** The serial version UID. */
031    private static final long serialVersionUID = 1L;
032
033    /** hold the options */
034    private final Map<String, Option> optionMap = new LinkedHashMap<>();
035
036    /** The name of the selected option */
037    private String selected;
038
039    /** specified whether this group is required */
040    private boolean required;
041
042    /**
043     * Add the specified {@code Option} to this group.
044     *
045     * @param option the option to add to this group
046     * @return this option group with the option added
047     */
048    public OptionGroup addOption(final Option option) {
049        // key - option name
050        // value - the option
051        optionMap.put(option.getKey(), option);
052
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     * Set 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
108        // if no option has already been selected or the
109        // same option is being reselected then set the
110        // selected member variable
111        if (selected != null && !selected.equals(option.getKey())) {
112            throw new AlreadySelectedException(this, option);
113        }
114        selected = option.getKey();
115    }
116
117    /**
118     * Returns the stringified version of this OptionGroup.
119     *
120     * @return the stringified representation of this group
121     */
122    @Override
123    public String toString() {
124        final StringBuilder buff = new StringBuilder();
125
126        final Iterator<Option> iter = getOptions().iterator();
127
128        buff.append("[");
129
130        while (iter.hasNext()) {
131            final Option option = iter.next();
132
133            if (option.getOpt() != null) {
134                buff.append("-");
135                buff.append(option.getOpt());
136            } else {
137                buff.append("--");
138                buff.append(option.getLongOpt());
139            }
140
141            if (option.getDescription() != null) {
142                buff.append(" ");
143                buff.append(option.getDescription());
144            }
145
146            if (iter.hasNext()) {
147                buff.append(", ");
148            }
149        }
150
151        buff.append("]");
152
153        return buff.toString();
154    }
155}