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.HashSet;
20  import java.util.Set;
21  
22  import org.apache.commons.cli2.Argument;
23  import org.apache.commons.cli2.Group;
24  import org.apache.commons.cli2.option.Switch;
25  import org.apache.commons.cli2.resource.ResourceConstants;
26  import org.apache.commons.cli2.resource.ResourceHelper;
27  
28  /**
29   * Builds Switch instance.
30   */
31  public class SwitchBuilder {
32      private final String enabledPrefix;
33      private final String disabledPrefix;
34      private String description;
35      private String preferredName;
36      private Set aliases;
37      private boolean required;
38      private Argument argument;
39      private Group children;
40      private int id;
41      private Boolean switchDefault;
42  
43      /**
44       * Creates a new SwitchBuilder using defaults.
45       * @see Switch#DEFAULT_ENABLED_PREFIX
46       * @see Switch#DEFAULT_DISABLED_PREFIX
47       */
48      public SwitchBuilder() {
49          this(Switch.DEFAULT_ENABLED_PREFIX, Switch.DEFAULT_DISABLED_PREFIX);
50      }
51  
52      /**
53       * Creates a new SwitchBuilder
54       * @param enabledPrefix the prefix to use for enabling the option
55       * @param disabledPrefix the prefix to use for disabling the option
56       * @throws IllegalArgumentException if either prefix is less than 1
57       *                                  character long or the prefixes match
58       */
59      public SwitchBuilder(final String enabledPrefix,
60                           final String disabledPrefix)
61          throws IllegalArgumentException {
62          if ((enabledPrefix == null) || (enabledPrefix.length() < 1)) {
63              throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_ENABLED_PREFIX));
64          }
65  
66          if ((disabledPrefix == null) || (disabledPrefix.length() < 1)) {
67              throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_DISABLED_PREFIX));
68          }
69  
70          if (enabledPrefix.equals(disabledPrefix)) {
71              throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_IDENTICAL_PREFIXES));
72          }
73  
74          this.enabledPrefix = enabledPrefix;
75          this.disabledPrefix = disabledPrefix;
76          reset();
77      }
78  
79      /**
80       * Creates a new Switch instance
81       * @return a new Switch instance
82       */
83      public Switch create() {
84          final Switch option =
85              new Switch(enabledPrefix, disabledPrefix, preferredName, aliases, description,
86                         required, argument, children, id, switchDefault);
87  
88          reset();
89  
90          return option;
91      }
92  
93      /**
94       * Resets the builder.
95       * @return this builder
96       */
97      public SwitchBuilder reset() {
98          description = null;
99          preferredName = null;
100         required = false;
101         aliases = new HashSet();
102         argument = null;
103         children = null;
104         id = 0;
105         switchDefault = null;
106 
107         return this;
108     }
109 
110     /**
111      * Use this option description
112      * @param newDescription the description to use
113      * @return this builder
114      */
115     public SwitchBuilder withDescription(final String newDescription) {
116         this.description = newDescription;
117 
118         return this;
119     }
120 
121     /**
122      * Use this option name. The first name is used as the preferred
123      * display name for the Command and then later names are used as aliases.
124      *
125      * @param name the name to use
126      * @return this builder
127      */
128     public SwitchBuilder withName(final String name) {
129         if (preferredName == null) {
130             preferredName = name;
131         } else {
132             aliases.add(name);
133         }
134 
135         return this;
136     }
137 
138     /**
139      * Use this optionality
140      * @param newRequired true iff the Option is required
141      * @return this builder
142      */
143     public SwitchBuilder withRequired(final boolean newRequired) {
144         this.required = newRequired;
145 
146         return this;
147     }
148 
149     /**
150      * Use this Argument
151      * @param newArgument the argument to use
152      * @return this builder
153      */
154     public SwitchBuilder withArgument(final Argument newArgument) {
155         this.argument = newArgument;
156 
157         return this;
158     }
159 
160     /**
161      * Use this child Group
162      * @param newChildren the child Group to use
163      * @return this builder
164      */
165     public SwitchBuilder withChildren(final Group newChildren) {
166         this.children = newChildren;
167 
168         return this;
169     }
170 
171     /**
172      * Sets the id
173      *
174      * @param newId
175      *            the id of the Switch
176      * @return this SwitchBuilder
177      */
178     public final SwitchBuilder withId(final int newId) {
179         this.id = newId;
180 
181         return this;
182     }
183 
184     /**
185      * Sets the default state for this switch
186      *
187      * @param newSwitchDefault the default state
188      * @return this SwitchBuilder
189      */
190     public final SwitchBuilder withSwitchDefault(final Boolean newSwitchDefault) {
191         this.switchDefault = newSwitchDefault;
192 
193         return this;
194     }
195 }