-
Notifications
You must be signed in to change notification settings - Fork 17
/
Version.java
343 lines (285 loc) · 8.64 KB
/
Version.java
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* This file is part of LWC (https://github.com/Hidendra/LWC)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.griefcraft.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* The version class is an immutable object representation of a semantic version (semver.org)
*
* Some (valid) test cases:
* 1.0.1 > 1.0.0
* 1.0.0 > 1.0.0-beta2
* 1.0.0-beta2 > 1.0.0-alpha3
*/
public final class Version implements Comparable {
/**
* Release levels that can be used
*/
public final static String[] releaseLevels = new String[] {
"release", "alpha", "beta", "rc"
};
/**
* The regex to match the build number
*/
private final static Pattern REGEX_BUILD_NUMBER = Pattern.compile(".*\\(b(\\d+)\\).*");
/**
* The raw version string
*/
private final String rawVersion;
/**
* Major level version
*/
private int major;
/**
* Minor level version
*/
private int minor;
/**
* Patch level version
*/
private int patch;
/**
* Release level weight. 0 is assumed to be greater than non-zero (e.g no -alpha/-beta designation)
* e.g alpha = 1, beta = 2, rc = 3
*/
private int releaseLevelWeight;
/**
* Release level version, for example where -alpha2, releaseLevelWeight = 1 and releaseLevel = 2
*/
private int releaseLevel;
/**
* If the build number is available, store it as well. Updating schemes such as bleeding will use the build
* number to know if it should update or not
*/
private int buildNumber;
public Version(String version) {
this.rawVersion = version;
calculateWeights();
}
/**
* Check if this version is newer than the given version
*
* @param version
* @return
*/
public boolean newerThan(Version version) {
return compareTo(version) > 0;
}
/**
* Check i this version is older than the given version
*
* @param version
* @return
*/
public boolean olderThan(Version version) {
return compareTo(version) < 0;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
// add the initial version - major.minor.patch
builder.append(major);
builder.append(".");
builder.append(minor);
builder.append(".");
builder.append(patch);
// are we using a different release level than release?
if (releaseLevel > 0) {
String level = releaseLevels[releaseLevel];
builder.append("-");
builder.append(level);
builder.append(releaseLevelWeight);
}
// How about a build number ?
if (buildNumber > 0) {
builder.append(" ");
builder.append("(b");
builder.append(buildNumber);
builder.append(")");
}
return builder.toString();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Version)) {
return false;
}
Version version = (Version) o;
return version.compareTo(this) == 0;
}
public int compareTo(Object o) {
if (!(o instanceof Version)) {
return 0;
}
Version version = (Version) o;
return compareTo(version);
}
public int compareTo(Version o) {
// check for major version changes
if (major > o.getMajor()) {
return 1;
} else if (major < o.getMajor()) {
return -1;
}
// major versions are equal.. check minor level
if (minor > o.getMinor()) {
return 1;
} else if (minor < o.getMinor()) {
return -1;
}
// minor versions are equal, too! ..check patch level
if (patch > o.getPatch()) {
return 1;
} else if (patch < o.getPatch()) {
return -1;
}
// should we take into account release level?
if (releaseLevel != 0 || o.getReleaseLevel() != 0) {
// check for release status
if (releaseLevel == 0 || o.getReleaseLevel() == 0) {
return releaseLevel == 0 ? 1 : -1;
}
// they are both an alpha, beta, etc etc, so check for more mature state
if (releaseLevel > o.getReleaseLevel()) {
return 1;
} else if (releaseLevel < o.getReleaseLevel()) {
return -1;
}
// they are both the same release state, ...!
if (releaseLevelWeight > o.getReleaseLevelWeight()) {
return 1;
} else if(releaseLevelWeight < o.getReleaseLevelWeight()) {
return -1;
}
}
// Finally, we can take into account the build number ..
if (buildNumber > 0 && o.getBuildNumber() > 0) {
if (buildNumber > o.getBuildNumber()) {
return 1;
} else if(buildNumber < o.getBuildNumber()) {
return -1;
}
}
// the two versions are identical! wee
return 0;
}
/**
* @return
*/
public int getMajor() {
return major;
}
/**
* @return
*/
public int getMinor() {
return minor;
}
/**
* @return
*/
public int getPatch() {
return patch;
}
/**
* @return
*/
public int getReleaseLevelWeight() {
return releaseLevelWeight;
}
/**
* @return
*/
public int getReleaseLevel() {
return releaseLevel;
}
/**
* @return
*/
public int getBuildNumber() {
return buildNumber;
}
/**
* Calculate the version weights
*/
private void calculateWeights() {
if (rawVersion.isEmpty()) {
return;
}
// parse the major version
this.major = parseNumber(rawVersion);
// parse the minor version
int minorIndex = rawVersion.indexOf(".") + 1;
this.minor = parseNumber(rawVersion.substring(minorIndex));
// parse the patch version, which is optional
int patchIndex = minorIndex + new Integer(this.minor).toString().length() + 1;
if (patchIndex < rawVersion.length()) {
this.patch = parseNumber(rawVersion.substring(patchIndex));
} else {
this.patch = 0;
}
// search for release level
for (int index = 0; index < releaseLevels.length; index++) {
String level = releaseLevels[index];
if (!rawVersion.contains(level)) {
continue;
}
// literally translate the level to the corresponding index
this.releaseLevel = index;
// check for a weighting (e.g version) after it (e.g alpha2)
int levelIndex = rawVersion.indexOf(level) + level.length();
if (levelIndex < rawVersion.length()) {
this.releaseLevelWeight = parseNumber(rawVersion.substring(levelIndex));
} else {
this.releaseLevelWeight = 1;
}
break;
}
// search for build number
Matcher matcher = REGEX_BUILD_NUMBER.matcher(rawVersion);
if (matcher.matches()) {
this.buildNumber = Integer.parseInt(matcher.group(1));
}
}
/**
* Pull a number from the front of the given string
*
* @param str
* @return
*/
private int parseNumber(String str) {
if (str.isEmpty()) {
return 0;
}
// is it actually a number?
try {
return Integer.parseInt(str);
} catch (NumberFormatException e) { }
int found = 0;
int index = 1;
while (index < str.length()) {
String peek = str.substring(0, index);
try {
found = Integer.parseInt(peek);
index ++;
} catch (NumberFormatException e) {
return found;
}
}
return found;
}
}