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.program.version.impl;
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.DOT_CHAR;
30 import static net.sourceforge.plantumldependency.common.constants.CommonConstants.HASHCODE_PRIME_NUMBER1;
31 import static net.sourceforge.plantumldependency.common.constants.CommonConstants.HASHCODE_PRIME_NUMBER2;
32 import static net.sourceforge.plantumldependency.common.constants.log.ErrorConstants.UNEXPECTED_ERROR;
33 import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
34 import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.AFTER;
35 import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.BEFORE;
36 import static net.sourceforge.plantumldependency.common.utils.comparable.ComparableResult.EQUAL;
37 import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
38 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.COMPILATION_TIME_NULL_ERROR;
39 import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.NOT_POSITIVE_VERSION_NUMBER_ERROR;
40
41 import java.util.Date;
42 import java.util.logging.Logger;
43
44 import net.sourceforge.plantumldependency.commoncli.program.version.ProgramVersion;
45
46
47
48
49
50
51
52
53 public class ProgramVersionImpl implements ProgramVersion {
54
55
56 private static final long serialVersionUID = 3475034667983466083L;
57
58
59 private static final transient Logger LOGGER = getLogger(ProgramVersionImpl.class.getName());
60
61
62 private int majorVersionNumber;
63
64
65 private int minorVersionNumber;
66
67
68 private int revisionVersionNumber;
69
70
71 private Date compilationTime;
72
73
74 private boolean snapshot;
75
76
77
78
79
80
81
82
83
84
85
86
87 public ProgramVersionImpl(final int majorVersion, final int minorVersion, final int revisionVersion) {
88 this(majorVersion, minorVersion, revisionVersion, new Date(), false);
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public ProgramVersionImpl(final int majorVersion, final int minorVersion, final int revisionVersion,
105 final Date compilation) {
106 this(majorVersion, minorVersion, revisionVersion, compilation, false);
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public ProgramVersionImpl(final int majorVersion, final int minorVersion, final int revisionVersion,
125 final Date compilation, final boolean isSnapshot) {
126 setCompilationTime(compilation);
127 setMajorVersionNumber(majorVersion);
128 setMinorVersionNumber(minorVersion);
129 setRevisionVersionNumber(revisionVersion);
130 setSnapshot(isSnapshot);
131 }
132
133
134
135
136
137
138 @Override
139 public int compareTo(final ProgramVersion o) {
140 if (this == o) {
141 return EQUAL.getResult();
142 }
143 int comparison = getMajorVersionNumber() < o.getMajorVersionNumber() ? BEFORE.getResult()
144 : getMajorVersionNumber() == o.getMajorVersionNumber() ? EQUAL.getResult() : AFTER.getResult();
145 if (comparison == EQUAL.getResult()) {
146 comparison = getMinorVersionNumber() < o.getMinorVersionNumber() ? BEFORE.getResult()
147 : getMinorVersionNumber() == o.getMinorVersionNumber() ? EQUAL.getResult() : AFTER.getResult();
148 if (comparison == EQUAL.getResult()) {
149 comparison = getRevisionVersionNumber() < o.getRevisionVersionNumber() ? BEFORE.getResult()
150 : getRevisionVersionNumber() == o.getRevisionVersionNumber() ? EQUAL.getResult() : AFTER
151 .getResult();
152 if (comparison == EQUAL.getResult()) {
153 comparison = getCompilationTime().compareTo(o.getCompilationTime());
154 if (comparison == EQUAL.getResult()) {
155 return isSnapshot() == o.isSnapshot() ? EQUAL.getResult() : isSnapshot() ? AFTER.getResult()
156 : BEFORE.getResult();
157 }
158 return comparison;
159
160 }
161 return comparison;
162
163 }
164 return comparison;
165
166 }
167 return comparison;
168
169 }
170
171
172
173
174
175
176 @Override
177 public ProgramVersion deepClone() {
178 ProgramVersionImpl p = null;
179
180 try {
181 p = (ProgramVersionImpl) super.clone();
182 p.compilationTime = (Date) getCompilationTime().clone();
183 } catch (final CloneNotSupportedException cnse) {
184 LOGGER.log(SEVERE, UNEXPECTED_ERROR, cnse);
185 }
186
187 return p;
188 }
189
190
191
192
193
194
195 @Override
196 public boolean equals(final Object obj) {
197 if (this == obj) {
198 return true;
199 }
200 if (obj == null) {
201 return false;
202 }
203 if (getClass() != obj.getClass()) {
204 return false;
205 }
206 final ProgramVersionImpl other = (ProgramVersionImpl) obj;
207 if (compilationTime == null) {
208 if (other.compilationTime != null) {
209 return false;
210 }
211 } else if (!compilationTime.equals(other.compilationTime)) {
212 return false;
213 }
214 if (majorVersionNumber != other.majorVersionNumber) {
215 return false;
216 }
217 if (minorVersionNumber != other.minorVersionNumber) {
218 return false;
219 }
220 if (revisionVersionNumber != other.revisionVersionNumber) {
221 return false;
222 }
223 if (snapshot != other.snapshot) {
224 return false;
225 }
226 return true;
227 }
228
229
230
231
232
233
234 @Override
235 public Date getCompilationTime() {
236 return new Date(compilationTime.getTime());
237 }
238
239
240
241
242
243
244 @Override
245 public String getFullVersionNumber() {
246 return getMajorVersionNumber() + DOT_CHAR + getMinorVersionNumber() + DOT_CHAR + getRevisionVersionNumber();
247 }
248
249
250
251
252
253
254 @Override
255 public int getMajorVersionNumber() {
256 return majorVersionNumber;
257 }
258
259
260
261
262
263
264 @Override
265 public int getMinorVersionNumber() {
266 return minorVersionNumber;
267 }
268
269
270
271
272
273
274 @Override
275 public int getRevisionVersionNumber() {
276 return revisionVersionNumber;
277 }
278
279
280
281
282
283
284 @Override
285 public int hashCode() {
286 final int prime = 31;
287 int result = 1;
288 result = prime * result + ((compilationTime == null) ? 0 : compilationTime.hashCode());
289 result = prime * result + majorVersionNumber;
290 result = prime * result + minorVersionNumber;
291 result = prime * result + revisionVersionNumber;
292 result = prime * result + (snapshot ? HASHCODE_PRIME_NUMBER1 : HASHCODE_PRIME_NUMBER2);
293 return result;
294 }
295
296
297
298
299
300
301 @Override
302 public boolean isSnapshot() {
303 return snapshot;
304 }
305
306
307
308
309
310
311
312
313
314 private void setCompilationTime(final Date value) {
315 checkNull(value, COMPILATION_TIME_NULL_ERROR);
316
317 compilationTime = value;
318 }
319
320
321
322
323
324
325
326
327
328 private void setMajorVersionNumber(final int value) {
329 if (value < 0) {
330 throw new IllegalArgumentException(buildLogString(NOT_POSITIVE_VERSION_NUMBER_ERROR, value));
331 }
332 majorVersionNumber = value;
333 }
334
335
336
337
338
339
340
341
342
343 private void setMinorVersionNumber(final int value) {
344 if (value < 0) {
345 throw new IllegalArgumentException(buildLogString(NOT_POSITIVE_VERSION_NUMBER_ERROR, value));
346 }
347 minorVersionNumber = value;
348 }
349
350
351
352
353
354
355
356
357
358 private void setRevisionVersionNumber(final int value) {
359 if (value < 0) {
360 throw new IllegalArgumentException(buildLogString(NOT_POSITIVE_VERSION_NUMBER_ERROR, value));
361 }
362 revisionVersionNumber = value;
363 }
364
365
366
367
368
369
370
371
372
373 private void setSnapshot(final boolean value) {
374 snapshot = value;
375 }
376
377
378
379
380
381
382 @Override
383 public String toString() {
384 return getClass().getSimpleName() + " [compilationTime=" + compilationTime + ", majorVersionNumber="
385 + majorVersionNumber + ", minorVersionNumber=" + minorVersionNumber + ", revisionVersionNumber="
386 + revisionVersionNumber + ", snapshot=" + snapshot + "]";
387 }
388 }