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.argument;
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.LEFT_BRACKET_CHAR;
30 import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.RIGHT_BRACKET_CHAR;
31 import static net.sourceforge.plantumldependency.common.constants.CommonConstants.HASHCODE_PRIME_NUMBER1;
32 import static net.sourceforge.plantumldependency.common.constants.CommonConstants.HASHCODE_PRIME_NUMBER2;
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.common.utils.comparable.ComparableResult.AFTER;
36 import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.BEFORE;
37 import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.EQUAL;
38 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.FULL_USAGE_DESCRIPTION_NULL_ERROR;
39
40 import java.util.logging.Logger;
41
42
43
44
45
46
47
48
49
50
51 public abstract class AbstractOptionArgument < A > implements OptionArgument < A > {
52
53
54 private static final transient Logger LOGGER = getLogger(AbstractOptionArgument.class.getName());
55
56
57 private static final long serialVersionUID = -9201749588723279692L;
58
59
60 private boolean mandatory;
61
62
63
64
65
66 private StringBuilder fullUsageDescription;
67
68
69
70
71
72
73
74
75
76
77
78 protected AbstractOptionArgument(final boolean optionArgumentIsMandatory,
79 final StringBuilder fullArgumentDescription) {
80 setMandatory(optionArgumentIsMandatory);
81 setFullUsageDescription(new StringBuilder(fullArgumentDescription));
82 }
83
84
85
86
87
88
89 @Override
90 public int compareTo(final OptionArgument < A > o) {
91 final int comparison;
92
93 if (this == o) {
94 comparison = EQUAL.getResult();
95 } else {
96 if (mandatory == o.isMandatory()) {
97 comparison = fullUsageDescription.toString().compareTo(o.getFullUsageDescription().toString());
98 } else if (mandatory) {
99 comparison = AFTER.getResult();
100 } else {
101 comparison = BEFORE.getResult();
102 }
103 }
104
105 return comparison;
106 }
107
108
109
110
111
112
113 @Override
114 public OptionArgument < A > deepClone() {
115 AbstractOptionArgument < A > a = null;
116
117 try {
118 a = (AbstractOptionArgument < A >) super.clone();
119 a.fullUsageDescription = new StringBuilder(getFullUsageDescription());
120 } catch (final CloneNotSupportedException cnse) {
121 LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
122 }
123
124 return a;
125 }
126
127
128
129
130
131
132 @Override
133 public boolean equals(final Object obj) {
134 if (this == obj) {
135 return true;
136 }
137 if (obj == null) {
138 return false;
139 }
140 if (getClass() != obj.getClass()) {
141 return false;
142 }
143 final AbstractOptionArgument < ? > other = (AbstractOptionArgument < ? >) obj;
144 if (fullUsageDescription == null) {
145 if (other.fullUsageDescription != null) {
146 return false;
147 }
148 } else if (other.fullUsageDescription == null) {
149 return true;
150 } else if (!fullUsageDescription.toString().equals(other.fullUsageDescription.toString())) {
151 return false;
152 }
153 if (mandatory != other.mandatory) {
154 return false;
155 }
156 return true;
157 }
158
159
160
161
162
163
164 @Override
165 public StringBuilder getFullUsageDescription() {
166 return fullUsageDescription;
167 }
168
169
170
171
172
173
174 @Override
175 public StringBuilder getMainUsage() {
176 StringBuilder buffer = null;
177
178 if (isMandatory()) {
179 buffer = new StringBuilder(getMainUsageDescription());
180 } else {
181 buffer = new StringBuilder(LEFT_BRACKET_CHAR);
182 buffer.append(getMainUsageDescription());
183 buffer.append(RIGHT_BRACKET_CHAR);
184 }
185
186 return buffer;
187 }
188
189
190
191
192
193
194
195 protected abstract String getMainUsageDescription();
196
197
198
199
200
201
202 @Override
203 public int hashCode() {
204 final int prime = 31;
205 int result = 1;
206 result = prime * result + ((fullUsageDescription == null) ? 0 : fullUsageDescription.toString().hashCode());
207 result = prime * result + (mandatory ? HASHCODE_PRIME_NUMBER1 : HASHCODE_PRIME_NUMBER2);
208 return result;
209 }
210
211
212
213
214
215
216 @Override
217 public boolean isMandatory() {
218 return mandatory;
219 }
220
221
222
223
224
225
226
227
228
229 private void setFullUsageDescription(final StringBuilder value) {
230 checkNull(value, FULL_USAGE_DESCRIPTION_NULL_ERROR);
231
232 fullUsageDescription = value;
233 }
234
235
236
237
238
239
240
241
242
243 private void setMandatory(final boolean value) {
244 mandatory = value;
245 }
246
247
248
249
250
251
252 @Override
253 public String toString() {
254 return getClass().getSimpleName() + " [fullUsageDescription=" + fullUsageDescription + ", mandatory="
255 + mandatory + "]";
256 }
257 }