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  package org.apache.commons.cli2.builder;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.cli2.Group;
23  import org.apache.commons.cli2.Option;
24  import org.apache.commons.cli2.option.GroupImpl;
25  
26  /**
27   * Builds Group instances
28   */
29  public class GroupBuilder {
30  
31      private String name;
32      private String description;
33      private List options;
34      private int minimum;
35      private int maximum;
36      private boolean required;
37  
38      /**
39       * Creates a new GroupBuilder
40       */
41      public GroupBuilder() {
42          reset();
43      }
44  
45      /**
46       * Creates a new Group instance
47       * @return the new Group instance
48       */
49      public Group create() {
50          final GroupImpl group =
51              new GroupImpl(options, name, description, minimum, maximum, required);
52  
53          reset();
54  
55          return group;
56      }
57  
58      /**
59       * Resets the builder.
60       * @return this builder     
61       */
62      public GroupBuilder reset() {
63          name = null;
64          description = null;
65          options = new ArrayList();
66          minimum = 0;
67          maximum = Integer.MAX_VALUE;
68          required = true;
69          return this;
70      }
71  
72      /**
73       * Use this option description.
74       * @param newDescription the description to use
75       * @return this builder
76       */
77      public GroupBuilder withDescription(final String newDescription) {
78          this.description = newDescription;
79          return this;
80      }
81  
82      /**
83       * Use this option name
84       * @param newName the name to use
85       * @return this builder
86       */
87      public GroupBuilder withName(final String newName) {
88          this.name = newName;
89          return this;
90      }
91  
92      /**
93       * A valid group requires at least this many options present
94       * @param newMinimum the minimum Options required
95       * @return this builder
96       */
97      public GroupBuilder withMinimum(final int newMinimum) {
98          this.minimum = newMinimum;
99          return this;
100     }
101 
102     /**
103      * A valid group requires at most this many options present
104      * @param newMaximum the maximum Options allowed
105      * @return this builder
106      */
107     public GroupBuilder withMaximum(final int newMaximum) {
108         this.maximum = newMaximum;
109         return this;
110     }
111 
112     /**
113      * Add this option to the group
114      * @param option the Option to add
115      * @return this builder
116      */
117     public GroupBuilder withOption(final Option option) {
118         this.options.add(option);
119         return this;
120     }
121 
122     /**
123      * Sets the required flag. This flag is evaluated for groups that are
124      * added to other groups as child groups. If set to <b>true</b> the
125      * minimum and maximum constraints of the child group are always evaluated.
126      * @param newRequired the required flag
127      * @return this builder
128      */
129     public GroupBuilder withRequired(final boolean newRequired) {
130         this.required = newRequired;
131         return this;
132     }
133 }