1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package net.sourceforge.plantumldependency.commoncli.option;
26
27 import static java.util.logging.Level.SEVERE;
28 import static java.util.logging.Logger.getLogger;
29 import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.COMMA_CHAR;
30 import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.LINE_CHAR;
31 import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.SPACE_CHAR;
32 import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.TAB_CHAR;
33 import static net.sourceforge.plantumldependency.common.constants.log.ErrorConstants.UNEXPECTED_ERROR;
34 import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
35 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.FULL_USAGE_DESCRIPTION_NULL_ERROR;
36 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.OPTION_NAME_NULL_ERROR;
37 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.OPTION_SECONDARY_NAMES_NULL_ERROR;
38 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.OPTION_STATUS_NULL_ERROR;
39
40 import java.util.Set;
41 import java.util.TreeSet;
42 import java.util.logging.Logger;
43
44 import net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus;
45
46
47
48
49
50
51
52
53 public abstract class AbstractOption implements Option {
54
55
56 private static final long serialVersionUID = -2714033818095664600L;
57
58
59 private static final transient Logger LOGGER = getLogger(AbstractOption.class.getName());
60
61
62
63
64
65 private StringBuilder fullUsageDescription;
66
67
68 private String name;
69
70
71 private Set < String > secondaryNames;
72
73
74 private OptionStatus status;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 protected AbstractOption(final String optionName, final Set < String > optionSecondaryNames,
93 final StringBuilder fullOptionDescription, final OptionStatus optionStatus) {
94 setName(optionName);
95 setSecondaryNames(new TreeSet < String >(optionSecondaryNames));
96 setFullUsageDescription(new StringBuilder(fullOptionDescription));
97 setStatus(optionStatus);
98 }
99
100
101
102
103
104
105 @Override
106 public int compareTo(final Option o) {
107 return getName().compareTo(o.getName());
108 }
109
110
111
112
113
114
115 @Override
116 public Option deepClone() {
117 AbstractOption a = null;
118
119 try {
120 a = (AbstractOption) super.clone();
121 a.fullUsageDescription = new StringBuilder(getFullUsageDescription());
122 a.secondaryNames = new TreeSet < String >(getSecondaryNames());
123 } catch (final CloneNotSupportedException cnse) {
124 LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
125 }
126
127 return a;
128 }
129
130
131
132
133
134
135 @Override
136 public boolean equals(final Object obj) {
137 if (this == obj) {
138 return true;
139 }
140 if (obj == null) {
141 return false;
142 }
143 if (getClass() != obj.getClass()) {
144 return false;
145 }
146 final AbstractOption other = (AbstractOption) obj;
147 if (name == null) {
148 if (other.name != null) {
149 return false;
150 }
151 } else if (!name.equals(other.name)) {
152 return false;
153 }
154 return true;
155 }
156
157
158
159
160
161
162 @Override
163 public Set < String > getAllNames() {
164 final Set < String > allNames = new TreeSet < String >(getSecondaryNames());
165 allNames.add(getName());
166 return allNames;
167 }
168
169
170
171
172
173
174 @Override
175 public StringBuilder getFullUsage() {
176 final StringBuilder buffer = new StringBuilder(getName());
177
178 for (final String secondaryName : getSecondaryNames()) {
179 buffer.append(COMMA_CHAR);
180 buffer.append(SPACE_CHAR);
181 buffer.append(secondaryName);
182 }
183
184 buffer.append(getFullUsageAdditions());
185 buffer.append(LINE_CHAR);
186 buffer.append(TAB_CHAR);
187 buffer.append(TAB_CHAR);
188 buffer.append(getFullUsageDescription());
189 buffer.append(getFullUsageDescriptionAdditions());
190
191 return buffer;
192 }
193
194
195
196
197
198
199
200
201 protected abstract StringBuilder getFullUsageAdditions();
202
203
204
205
206
207
208
209
210 private StringBuilder getFullUsageDescription() {
211 return fullUsageDescription;
212 }
213
214
215
216
217
218
219
220
221 protected abstract StringBuilder getFullUsageDescriptionAdditions();
222
223
224
225
226
227
228 @Override
229 public StringBuilder getMainUsage() {
230 final StringBuilder buffer = new StringBuilder(getName());
231 buffer.append(getMainUsageAdditions());
232 return buffer;
233 }
234
235
236
237
238
239
240
241
242 protected abstract StringBuilder getMainUsageAdditions();
243
244
245
246
247
248
249 @Override
250 public String getName() {
251 return name;
252 }
253
254
255
256
257
258
259 @Override
260 public Set < String > getSecondaryNames() {
261 return secondaryNames;
262 }
263
264
265
266
267
268
269
270 @Override
271 public OptionStatus getStatus() {
272 return status;
273 }
274
275
276
277
278
279
280 @Override
281 public int hashCode() {
282 final int prime = 31;
283 int result = 1;
284 result = prime * result + ((name == null) ? 0 : name.hashCode());
285 return result;
286 }
287
288
289
290
291
292
293 @Override
294 public boolean isMandatory() {
295 return getStatus().isMandatory();
296 }
297
298
299
300
301
302
303
304
305
306 private void setFullUsageDescription(final StringBuilder value) {
307 checkNull(value, FULL_USAGE_DESCRIPTION_NULL_ERROR);
308
309 fullUsageDescription = value;
310 }
311
312
313
314
315
316
317
318
319
320 private void setName(final String value) {
321 checkNull(value, OPTION_NAME_NULL_ERROR);
322
323 name = value;
324 }
325
326
327
328
329
330
331
332
333
334 private void setSecondaryNames(final Set < String > value) {
335 checkNull(value, OPTION_SECONDARY_NAMES_NULL_ERROR);
336
337 secondaryNames = value;
338 }
339
340
341
342
343
344
345
346
347
348 private void setStatus(final OptionStatus value) {
349 checkNull(value, OPTION_STATUS_NULL_ERROR);
350
351 status = value;
352 }
353
354
355
356
357
358
359 @Override
360 public String toString() {
361 return getClass().getSimpleName() + " [fullUsageDescription=" + fullUsageDescription + ", name=" + name
362 + ", secondaryNames=" + secondaryNames + ", status=" + status + "]";
363 }
364 }