/
Version.java
333 lines (287 loc) · 8.39 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
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.util;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.springframework.util.StringUtils;
/**
* Define the version number of a module. A typical version is represented as
* {@code MAJOR.MINOR.PATCH.QUALIFIER} where the qualifier can have an extra version.
* <p>
* For example: {@code 1.2.0.RC1} is the first release candidate of 1.2.0 and
* {@code 1.5.0.M4} is the fourth milestone of 1.5.0. The special {@code RELEASE}
* qualifier indicates a final release (a.k.a. GA)
* <p>
* The main purpose of parsing a version is to compare it with another version, see
* {@link Comparable}.
*
* @author Stephane Nicoll
*/
@SuppressWarnings("serial")
public final class Version implements Serializable, Comparable<Version> {
private static final VersionQualifierComparator qualifierComparator = new VersionQualifierComparator();
private static final VersionParser parser = new VersionParser(
Collections.emptyList());
private final Integer major;
private final Integer minor;
private final Integer patch;
private final Qualifier qualifier;
// For Jackson
@SuppressWarnings("unused")
private Version() {
this(null, null, null, null);
}
public Version(Integer major, Integer minor, Integer patch, Qualifier qualifier) {
this.major = major;
this.minor = minor;
this.patch = patch;
this.qualifier = qualifier;
}
public Integer getMajor() {
return this.major;
}
public Integer getMinor() {
return this.minor;
}
public Integer getPatch() {
return this.patch;
}
public Qualifier getQualifier() {
return this.qualifier;
}
@Override
public String toString() {
return this.major + "." + this.minor + "." + this.patch
+ (this.qualifier != null ? "." + this.qualifier.qualifier
+ (this.qualifier.version != null ? this.qualifier.version : "")
: "");
}
/**
* Parse the string representation of a {@link Version}. Throws an
* {@link InvalidVersionException} if the version could not be parsed.
* @param text the version text
* @return a Version instance for the specified version text
* @throws InvalidVersionException if the version text could not be parsed
* @see VersionParser
*/
public static Version parse(String text) {
return parser.parse(text);
}
/**
* Parse safely the specified string representation of a {@link Version}.
* <p>
* Return {@code null} if the text represents an invalid version.
* @param text the version text
* @return a Version instance for the specified version text
* @see VersionParser
*/
public static Version safeParse(String text) {
try {
return parse(text);
}
catch (InvalidVersionException ex) {
return null;
}
}
@Override
public int compareTo(Version other) {
if (other == null) {
return 1;
}
int majorDiff = safeCompare(this.major, other.major);
if (majorDiff != 0) {
return majorDiff;
}
int minorDiff = safeCompare(this.minor, other.minor);
if (minorDiff != 0) {
return minorDiff;
}
int patch = safeCompare(this.patch, other.patch);
if (patch != 0) {
return patch;
}
return qualifierComparator.compare(this.qualifier, other.qualifier);
}
private static int safeCompare(Integer first, Integer second) {
Integer firstIndex = (first != null ? first : 0);
Integer secondIndex = (second != null ? second : 0);
return firstIndex.compareTo(secondIndex);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.major == null) ? 0 : this.major.hashCode());
result = prime * result + ((this.minor == null) ? 0 : this.minor.hashCode());
result = prime * result + ((this.patch == null) ? 0 : this.patch.hashCode());
result = prime * result
+ ((this.qualifier == null) ? 0 : this.qualifier.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Version other = (Version) obj;
if (this.major == null) {
if (other.major != null) {
return false;
}
}
else if (!this.major.equals(other.major)) {
return false;
}
if (this.minor == null) {
if (other.minor != null) {
return false;
}
}
else if (!this.minor.equals(other.minor)) {
return false;
}
if (this.patch == null) {
if (other.patch != null) {
return false;
}
}
else if (!this.patch.equals(other.patch)) {
return false;
}
if (this.qualifier == null) {
if (other.qualifier != null) {
return false;
}
}
else if (!this.qualifier.equals(other.qualifier)) {
return false;
}
return true;
}
/**
* A version qualifier.
*/
public static class Qualifier implements Serializable {
public Qualifier(String qualifier) {
this.qualifier = qualifier;
}
private String qualifier;
private Integer version;
public String getQualifier() {
return this.qualifier;
}
public void setQualifier(String qualifier) {
this.qualifier = qualifier;
}
public Integer getVersion() {
return this.version;
}
public void setVersion(Integer version) {
this.version = version;
}
@Override
public String toString() {
return "Qualifier ["
+ (this.qualifier != null ? "qualifier=" + this.qualifier + ", " : "")
+ (this.version != null ? "version=" + this.version : "") + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((this.qualifier == null) ? 0 : this.qualifier.hashCode());
result = prime * result
+ ((this.version == null) ? 0 : this.version.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Qualifier other = (Qualifier) obj;
if (this.qualifier == null) {
if (other.qualifier != null) {
return false;
}
}
else if (!this.qualifier.equals(other.qualifier)) {
return false;
}
if (this.version == null) {
if (other.version != null) {
return false;
}
}
else if (!this.version.equals(other.version)) {
return false;
}
return true;
}
}
private static class VersionQualifierComparator implements Comparator<Qualifier> {
static final String RELEASE = "RELEASE";
static final String SNAPSHOT = "BUILD-SNAPSHOT";
static final String MILESTONE = "M";
static final String RC = "RC";
static final List<String> KNOWN_QUALIFIERS = Arrays.asList(MILESTONE, RC,
SNAPSHOT, RELEASE);
@Override
public int compare(Qualifier o1, Qualifier o2) {
Qualifier first = (o1 != null ? o1 : new Qualifier(RELEASE));
Qualifier second = (o2 != null ? o2 : new Qualifier(RELEASE));
int qualifier = compareQualifier(first, second);
return (qualifier != 0 ? qualifier : compareQualifierVersion(first, second));
}
private static int compareQualifierVersion(Qualifier first, Qualifier second) {
Integer firstVersion = (first.getVersion() != null ? first.getVersion() : 0);
Integer secondVersion = (second.getVersion() != null ? second.getVersion()
: 0);
return firstVersion.compareTo(secondVersion);
}
private static int compareQualifier(Qualifier first, Qualifier second) {
Integer firstIndex = getQualifierIndex(first.qualifier);
Integer secondIndex = getQualifierIndex(second.qualifier);
// Unknown qualifier, alphabetic ordering
if (firstIndex == -1 && secondIndex == -1) {
return first.qualifier.compareTo(second.qualifier);
}
else {
return firstIndex.compareTo(secondIndex);
}
}
private static int getQualifierIndex(String qualifier) {
return (StringUtils.hasText(qualifier) ? KNOWN_QUALIFIERS.indexOf(qualifier)
: 0);
}
}
}