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 | 7 | this(Switch.DEFAULT_ENABLED_PREFIX, Switch.DEFAULT_DISABLED_PREFIX); |
50 | 7 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public SwitchBuilder(final String enabledPrefix, |
60 | |
final String disabledPrefix) |
61 | 7 | throws IllegalArgumentException { |
62 | 7 | if ((enabledPrefix == null) || (enabledPrefix.length() < 1)) { |
63 | 0 | throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_ENABLED_PREFIX)); |
64 | |
} |
65 | |
|
66 | 7 | if ((disabledPrefix == null) || (disabledPrefix.length() < 1)) { |
67 | 0 | throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ILLEGAL_DISABLED_PREFIX)); |
68 | |
} |
69 | |
|
70 | 7 | if (enabledPrefix.equals(disabledPrefix)) { |
71 | 0 | throw new IllegalArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_IDENTICAL_PREFIXES)); |
72 | |
} |
73 | |
|
74 | 7 | this.enabledPrefix = enabledPrefix; |
75 | 7 | this.disabledPrefix = disabledPrefix; |
76 | 7 | reset(); |
77 | 7 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public Switch create() { |
84 | 7 | final Switch option = |
85 | |
new Switch(enabledPrefix, disabledPrefix, preferredName, aliases, description, |
86 | |
required, argument, children, id, switchDefault); |
87 | |
|
88 | 7 | reset(); |
89 | |
|
90 | 7 | return option; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public SwitchBuilder reset() { |
98 | 14 | description = null; |
99 | 14 | preferredName = null; |
100 | 14 | required = false; |
101 | 14 | aliases = new HashSet(); |
102 | 14 | argument = null; |
103 | 14 | children = null; |
104 | 14 | id = 0; |
105 | 14 | switchDefault = null; |
106 | |
|
107 | 14 | return this; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public SwitchBuilder withDescription(final String newDescription) { |
116 | 0 | this.description = newDescription; |
117 | |
|
118 | 0 | return this; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public SwitchBuilder withName(final String name) { |
129 | 7 | if (preferredName == null) { |
130 | 7 | preferredName = name; |
131 | |
} else { |
132 | 0 | aliases.add(name); |
133 | |
} |
134 | |
|
135 | 7 | return this; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public SwitchBuilder withRequired(final boolean newRequired) { |
144 | 0 | this.required = newRequired; |
145 | |
|
146 | 0 | return this; |
147 | |
} |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public SwitchBuilder withArgument(final Argument newArgument) { |
155 | 0 | this.argument = newArgument; |
156 | |
|
157 | 0 | return this; |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public SwitchBuilder withChildren(final Group newChildren) { |
166 | 0 | this.children = newChildren; |
167 | |
|
168 | 0 | return this; |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public final SwitchBuilder withId(final int newId) { |
179 | 0 | this.id = newId; |
180 | |
|
181 | 0 | return this; |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public final SwitchBuilder withSwitchDefault(final Boolean newSwitchDefault) { |
191 | 7 | this.switchDefault = newSwitchDefault; |
192 | |
|
193 | 7 | return this; |
194 | |
} |
195 | |
} |