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.Iterator;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  
26  /**
27   * A group of mutually exclusive options.
28   */
29  public class OptionGroup implements Serializable
30  {
31      /** The serial version UID. */
32      private static final long serialVersionUID = 1L;
33      
34      /** hold the options */
35      private final Map<String, Option> optionMap = new LinkedHashMap<String, Option>();
36  
37      /** the name of the selected option */
38      private String selected;
39  
40      /** specified whether this group is required */
41      private boolean required;
42  
43      /**
44       * Add the specified <code>Option</code> to this group.
45       *
46       * @param option the option to add to this group
47       * @return this option group with the option added
48       */
49      public OptionGroup addOption(final Option option)
50      {
51          // key   - option name
52          // value - the option
53          optionMap.put(option.getKey(), option);
54  
55          return this;
56      }
57  
58      /**
59       * @return the names of the options in this group as a 
60       * <code>Collection</code>
61       */
62      public Collection<String> getNames()
63      {
64          // the key set is the collection of names
65          return optionMap.keySet();
66      }
67  
68      /**
69       * @return the options in this group as a <code>Collection</code>
70       */
71      public Collection<Option> getOptions()
72      {
73          // the values are the collection of options
74          return optionMap.values();
75      }
76  
77      /**
78       * Set the selected option of this group to <code>name</code>.
79       *
80       * @param option the option that is selected
81       * @throws AlreadySelectedException if an option from this group has 
82       * already been selected.
83       */
84      public void setSelected(final Option option) throws AlreadySelectedException
85      {
86          if (option == null)
87          {
88              // reset the option previously selected
89              selected = null;
90              return;
91          }
92          
93          // if no option has already been selected or the 
94          // same option is being reselected then set the
95          // selected member variable
96          if (selected == null || selected.equals(option.getKey()))
97          {
98              selected = option.getKey();
99          }
100         else
101         {
102             throw new AlreadySelectedException(this, option);
103         }
104     }
105 
106     /**
107      * @return the selected option name
108      */
109     public String getSelected()
110     {
111         return selected;
112     }
113 
114     /**
115      * @param required specifies if this group is required
116      */
117     public void setRequired(final boolean required)
118     {
119         this.required = required;
120     }
121 
122     /**
123      * Returns whether this option group is required.
124      *
125      * @return whether this option group is required
126      */
127     public boolean isRequired()
128     {
129         return required;
130     }
131 
132     /**
133      * Returns the stringified version of this OptionGroup.
134      * 
135      * @return the stringified representation of this group
136      */
137     @Override
138     public String toString()
139     {
140         final StringBuilder buff = new StringBuilder();
141         
142         final Iterator<Option> iter = getOptions().iterator();
143 
144         buff.append("[");
145 
146         while (iter.hasNext())
147         {
148             final Option option = iter.next();
149 
150             if (option.getOpt() != null)
151             {
152                 buff.append("-");
153                 buff.append(option.getOpt());
154             }
155             else
156             {
157                 buff.append("--");
158                 buff.append(option.getLongOpt());
159             }
160             
161             if (option.getDescription() != null)
162             {
163                 buff.append(" ");
164                 buff.append(option.getDescription());
165             }
166             
167             if (iter.hasNext())
168             {
169                 buff.append(", ");
170             }
171         }
172 
173         buff.append("]");
174 
175         return buff.toString();
176     }
177 }