1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.gmavenplus.model.internal;
18
19 import java.util.Arrays;
20
21
22
23
24
25
26
27
28
29 public class Version implements Comparable<Version> {
30
31
32
33
34 private int major;
35
36
37
38
39 private int minor;
40
41
42
43
44 private int revision;
45
46
47
48
49 private String tag;
50
51
52
53
54
55
56
57
58
59 public Version(final int newMajor, final int newMinor, final int newRevision, final String newTag) {
60 if (newMajor < 0 || newMinor < 0 || newRevision < 0) {
61
62 throw new IllegalArgumentException("Major must be >= 0 and minor >= 0 and revision >= 0.");
63 }
64
65 major = newMajor;
66 minor = newMinor;
67 revision = newRevision;
68 if (newTag == null || !newTag.isEmpty()) {
69 tag = newTag;
70 } else {
71 tag = null;
72 }
73 }
74
75
76
77
78
79
80
81
82 public Version(final int newMajor, final int newMinor, final int newRevision) {
83 this(newMajor, newMinor, newRevision, null);
84 }
85
86
87
88
89
90
91
92 public Version(final int newMajor, final int newMinor) {
93 this(newMajor, newMinor, 0);
94 }
95
96
97
98
99
100
101 public Version(final int newMajor) {
102 this(newMajor, 0);
103 }
104
105
106
107
108
109
110
111 public static Version parseFromString(final String version) {
112 if (version == null || version.isEmpty()) {
113 throw new IllegalArgumentException("Version must not be null or empty.");
114 }
115 String[] split = version.split("[._-]", 4);
116 try {
117 int tagIdx = 3;
118 int major = Integer.parseInt(split[0]);
119 int minor = 0;
120 int revision = 0;
121 StringBuilder tag = new StringBuilder();
122 if (split.length >= 2) {
123 try {
124 minor = Integer.parseInt(split[1]);
125 } catch (NumberFormatException nfe) {
126
127 tag.append(split[1]);
128 tagIdx = 1;
129 tag.append("-");
130 }
131 }
132 if (split.length >= 3) {
133 try {
134 revision = Integer.parseInt(split[2]);
135 } catch (NumberFormatException nfe) {
136
137 tag.append(split[2]);
138 tagIdx = 2;
139 tag.append("-");
140 }
141 }
142 if (split.length >= 4) {
143 for (int i = tagIdx; i < split.length; i++) {
144 if (i > tagIdx) {
145 tag.append("-");
146 }
147 tag.append(split[i]);
148 }
149 }
150 return new Version(major, minor, revision, tag.toString());
151 } catch (NumberFormatException e) {
152 throw new IllegalArgumentException("Major, minor, and revision must be integers.", e);
153 }
154 }
155
156
157
158
159
160
161
162 @Override
163 public final int hashCode() {
164 return Arrays.hashCode(new Object[]{major, minor, revision, tag});
165 }
166
167
168
169
170
171
172
173
174 @Override
175 public final boolean equals(final Object obj) {
176 if (obj == null) {
177 return false;
178 }
179 if (obj == this) {
180 return true;
181 }
182 if (obj.getClass() != getClass()) {
183 return false;
184 }
185 return compareTo((Version) obj) == 0;
186 }
187
188
189
190
191
192
193
194 @Override
195 public final String toString() {
196 StringBuilder buff = new StringBuilder();
197
198 buff.append(major)
199 .append(".").append(minor)
200 .append(".").append(revision);
201 if (tag != null) {
202 buff.append("-").append(tag);
203 }
204
205 return buff.toString();
206 }
207
208
209
210
211
212
213
214
215
216 @Override
217 public final int compareTo(final Version version) {
218 return compareTo(version, true);
219 }
220
221
222
223
224
225
226
227
228
229
230 public final int compareTo(final Version version, final boolean noTagsAreNewer) {
231
232 int comp = Integer.compare(major, version.major);
233 if (comp == 0) {
234 comp = Integer.compare(minor, version.minor);
235 }
236 if (comp == 0) {
237 comp = Integer.compare(revision, version.revision);
238 }
239 if (comp == 0) {
240 if (tag != null && version.tag != null) {
241 return tag.replace("beta", " beta").replace("alpha", " alpha")
242 .compareTo(version.tag.replace("beta", " beta").replace("alpha", " alpha"));
243 } else if (tag == null ^ version.tag == null) {
244 if (tag == null) {
245 return noTagsAreNewer ? 1 : -1;
246 } else {
247 return noTagsAreNewer ? -1 : 1;
248 }
249 } else {
250 return comp;
251 }
252 } else {
253 return comp;
254 }
255 }
256
257
258
259
260
261
262 public int getMajor() {
263 return major;
264 }
265
266
267
268
269
270
271
272 public Version setMajor(final int newMajor) {
273 major = newMajor;
274 return this;
275 }
276
277
278
279
280
281
282 public int getMinor() {
283 return minor;
284 }
285
286
287
288
289
290
291
292 public Version setMinor(final int newMinor) {
293 minor = newMinor;
294 return this;
295 }
296
297
298
299
300
301
302 public int getRevision() {
303 return revision;
304 }
305
306
307
308
309
310
311
312 public Version setRevision(final int newRevision) {
313 revision = newRevision;
314 return this;
315 }
316
317
318
319
320
321
322 public String getTag() {
323 return tag;
324 }
325
326
327
328
329
330
331
332 public Version setTag(final String newTag) {
333 tag = newTag;
334 return this;
335 }
336
337 }