View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.cli;
19  
20  import java.io.Serializable;
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  /**
27   * A group of mutually exclusive options.
28   *
29   * @version $Id: OptionGroup.java 1443102 2013-02-06 18:12:16Z tn $
30   */
31  public class OptionGroup implements Serializable
32  {
33      /** The serial version UID. */
34      private static final long serialVersionUID = 1L;
35      
36      /** hold the options */
37      private Map<String, Option> optionMap = new HashMap<String, Option>();
38  
39      /** the name of the selected option */
40      private String selected;
41  
42      /** specified whether this group is required */
43      private boolean required;
44  
45      /**
46       * Add the specified <code>Option</code> to this group.
47       *
48       * @param option the option to add to this group
49       * @return this option group with the option added
50       */
51      public OptionGroup addOption(Option option)
52      {
53          // key   - option name
54          // value - the option
55          optionMap.put(option.getKey(), option);
56  
57          return this;
58      }
59  
60      /**
61       * @return the names of the options in this group as a 
62       * <code>Collection</code>
63       */
64      public Collection<String> getNames()
65      {
66          // the key set is the collection of names
67          return optionMap.keySet();
68      }
69  
70      /**
71       * @return the options in this group as a <code>Collection</code>
72       */
73      public Collection<Option> getOptions()
74      {
75          // the values are the collection of options
76          return optionMap.values();
77      }
78  
79      /**
80       * Set the selected option of this group to <code>name</code>.
81       *
82       * @param option the option that is selected
83       * @throws AlreadySelectedException if an option from this group has 
84       * already been selected.
85       */
86      public void setSelected(Option option) throws AlreadySelectedException
87      {
88          if (option == null)
89          {
90              // reset the option previously selected
91              selected = null;
92              return;
93          }
94          
95          // if no option has already been selected or the 
96          // same option is being reselected then set the
97          // selected member variable
98          if (selected == null || selected.equals(option.getKey()))
99          {
100             selected = option.getKey();
101         }
102         else
103         {
104             throw new AlreadySelectedException(this, option);
105         }
106     }
107 
108     /**
109      * @return the selected option name
110      */
111     public String getSelected()
112     {
113         return selected;
114     }
115 
116     /**
117      * @param required specifies if this group is required
118      */
119     public void setRequired(boolean required)
120     {
121         this.required = required;
122     }
123 
124     /**
125      * Returns whether this option group is required.
126      *
127      * @return whether this option group is required
128      */
129     public boolean isRequired()
130     {
131         return required;
132     }
133 
134     /**
135      * Returns the stringified version of this OptionGroup.
136      * 
137      * @return the stringified representation of this group
138      */
139     @Override
140     public String toString()
141     {
142         StringBuilder buff = new StringBuilder();
143         
144         Iterator<Option> iter = getOptions().iterator();
145 
146         buff.append("[");
147 
148         while (iter.hasNext())
149         {
150             Option option = iter.next();
151 
152             if (option.getOpt() != null)
153             {
154                 buff.append("-");
155                 buff.append(option.getOpt());
156             }
157             else
158             {
159                 buff.append("--");
160                 buff.append(option.getLongOpt());
161             }
162             
163             if (option.getDescription() != null)
164             {
165                 buff.append(" ");
166                 buff.append(option.getDescription());
167             }
168             
169             if (iter.hasNext())
170             {
171                 buff.append(", ");
172             }
173         }
174 
175         buff.append("]");
176 
177         return buff.toString();
178     }
179 }