1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
45
46
47
48 public SwitchBuilder() {
49 this(Switch.DEFAULT_ENABLED_PREFIX, Switch.DEFAULT_DISABLED_PREFIX);
50 }
51
52
53
54
55
56
57
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
81
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
95
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
112
113
114
115 public SwitchBuilder withDescription(final String newDescription) {
116 this.description = newDescription;
117
118 return this;
119 }
120
121
122
123
124
125
126
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
140
141
142
143 public SwitchBuilder withRequired(final boolean newRequired) {
144 this.required = newRequired;
145
146 return this;
147 }
148
149
150
151
152
153
154 public SwitchBuilder withArgument(final Argument newArgument) {
155 this.argument = newArgument;
156
157 return this;
158 }
159
160
161
162
163
164
165 public SwitchBuilder withChildren(final Group newChildren) {
166 this.children = newChildren;
167
168 return this;
169 }
170
171
172
173
174
175
176
177
178 public final SwitchBuilder withId(final int newId) {
179 this.id = newId;
180
181 return this;
182 }
183
184
185
186
187
188
189
190 public final SwitchBuilder withSwitchDefault(final Boolean newSwitchDefault) {
191 this.switchDefault = newSwitchDefault;
192
193 return this;
194 }
195 }