This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
GradleVersion.java
430 lines (370 loc) · 13.6 KB
/
GradleVersion.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* Copyright (C) 2016 The Android Open Source Project
*
* 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 com.android.ide.common.repository;
import static com.google.common.base.Strings.isNullOrEmpty;
import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Supports versions in the given formats: <ul> <li>major (e.g. 1)</li> <li>major.minor (e.g.
* 1.0)</li> <li>major.minor.micro (e.g. 1.1.1)</li> </ul> A version can also be a "preview" (e.g.
* 1-alpha1, 1.0.0-rc2) or an unreleased version (or "snapshot") (e.g. 1-SNAPSHOT,
* 1.0.0-alpha1-SNAPSHOT).
*/
public class GradleVersion implements Comparable<GradleVersion>, Serializable {
private static final String PLUS = "+";
private static final Pattern PREVIEW_PATTERN = Pattern.compile("([a-zA-z]+)[\\-]?([\\d]+)?");
private final String mRawValue;
@NonNull
private final VersionSegment mMajorSegment;
@Nullable
private final VersionSegment mMinorSegment;
@Nullable
private final VersionSegment mMicroSegment;
private final int mPreview;
@Nullable
private final String mPreviewType;
private final boolean mSnapshot;
@NonNull
private final List<VersionSegment> mAdditionalSegments;
/**
* Parses the given version. This method does the same as {@link #parse(String)}, but it does
* not throw exceptions if the given value does not conform with any of the supported version
* formats.
*
* @param value the version to parse.
* @return the created {@code Version} object, or {@code null} if the given value does not
* conform with any of the supported version formats.
*/
@Nullable
public static GradleVersion tryParse(@NonNull String value) {
try {
return parse(value);
} catch (RuntimeException ignored) {
}
return null;
}
/**
* Parses the given version.
*
* @param value the version to parse.
* @return the created {@code Version} object.
* @throws IllegalArgumentException if the given value does not conform with any of the
* supported version formats.
*/
@NonNull
public static GradleVersion parse(@NonNull String value) {
String version = value;
String qualifiers = null;
char dash = '-';
int dashIndex = value.indexOf(dash);
if (dashIndex != -1) {
if (dashIndex < value.length() - 1) {
qualifiers = value.substring(dashIndex + 1);
}
version = value.substring(0, dashIndex);
}
try {
List<VersionSegment> parsedVersionSegments = splitSegments(version);
int segmentCount = parsedVersionSegments.size();
VersionSegment majorSegment;
VersionSegment minorSegment = null;
VersionSegment microSegment = null;
List<VersionSegment> additionalSegments = Lists.newArrayList();
if (segmentCount > 0) {
majorSegment = parsedVersionSegments.get(0);
if (segmentCount > 1) {
minorSegment = parsedVersionSegments.get(1);
}
if (segmentCount >= 3) {
microSegment = parsedVersionSegments.get(2);
}
if (segmentCount > 3) {
additionalSegments.addAll(parsedVersionSegments.subList(3, segmentCount));
}
int preview = 0;
String previewType = null;
boolean snapshot = false;
if (qualifiers != null) {
if (isSnapshotQualifier(qualifiers)) {
snapshot = true;
qualifiers = null;
}
else {
// find and remove "SNAPSHOT" at the end of the qualifiers.
int lastDashIndex = qualifiers.lastIndexOf(dash);
if (lastDashIndex != -1) {
String mayBeSnapshot = qualifiers.substring(lastDashIndex + 1);
if (isSnapshotQualifier(mayBeSnapshot)) {
snapshot = true;
qualifiers = qualifiers.substring(0, lastDashIndex);
}
}
}
if (!isNullOrEmpty(qualifiers)) {
Matcher matcher = PREVIEW_PATTERN.matcher(qualifiers);
if (matcher.matches()) {
previewType = matcher.group(1);
if (matcher.groupCount() == 2) {
String group = matcher.group(2);
if (!isNullOrEmpty(group)) {
preview = Integer.parseInt(group);
}
}
}
}
}
return new GradleVersion(value, majorSegment, minorSegment, microSegment,
additionalSegments, preview, previewType, snapshot);
}
} catch (NumberFormatException e) {
throw parsingFailure(value, e);
}
throw parsingFailure(value);
}
@NonNull
private static List<VersionSegment> splitSegments(@NonNull String version) {
Iterable<String> segments = Splitter.on('.').split(version);
List<VersionSegment> parsedSegments = Lists.newArrayListWithCapacity(3);
for (String segment : segments) {
parsedSegments.addAll(parseSegment(segment));
}
return parsedSegments;
}
@NonNull
private static List<VersionSegment> parseSegment(@NonNull String text) {
int length = text.length();
if (length > 1 && text.endsWith(PLUS)) {
// Segment has a number and a '+' (e.g. second segment in '2.1+')
List<VersionSegment> segments = Lists.newArrayListWithCapacity(2);
// We need to split '1+' into 2 segments: '1' and '+'
segments.add(new VersionSegment(text.substring(0, length - 1)));
segments.add(new VersionSegment(PLUS));
return segments;
}
return Collections.singletonList(new VersionSegment(text));
}
private static boolean isSnapshotQualifier(@NonNull String value) {
return "SNAPSHOT".equalsIgnoreCase(value) || "dev".equalsIgnoreCase(value);
}
@NonNull
private static IllegalArgumentException parsingFailure(@NonNull String value) {
return parsingFailure(value, null);
}
@NonNull
private static IllegalArgumentException parsingFailure(@NonNull String value,
@Nullable Throwable cause) {
return new IllegalArgumentException(String.format("'%1$s' is not a valid version", value),
cause);
}
public GradleVersion(int major, int minor, int micro) {
this((major + "." + minor + "." + micro), new VersionSegment(major),
new VersionSegment(minor), new VersionSegment(micro),
Collections.<VersionSegment>emptyList(), 0, null, false);
}
private GradleVersion(@NonNull String rawValue,
@NonNull VersionSegment majorSegment,
@Nullable VersionSegment minorSegment,
@Nullable VersionSegment microSegment,
@NonNull List<VersionSegment> additionalSegments,
int preview,
@Nullable String previewType,
boolean snapshot) {
mRawValue = rawValue;
mMajorSegment = majorSegment;
mMinorSegment = minorSegment;
mMicroSegment = microSegment;
mAdditionalSegments = ImmutableList.copyOf(additionalSegments);
mPreview = preview;
mPreviewType = previewType;
mSnapshot = snapshot;
}
public int getMajor() {
return valueOf(mMajorSegment);
}
@NonNull
public VersionSegment getMajorSegment() {
return mMajorSegment;
}
public int getMinor() {
return valueOf(mMinorSegment);
}
@Nullable
public VersionSegment getMinorSegment() {
return mMinorSegment;
}
public int getMicro() {
return valueOf(mMicroSegment);
}
private static int valueOf(@Nullable VersionSegment segment) {
return segment != null ? segment.getValue() : 0;
}
@Nullable
public VersionSegment getMicroSegment() {
return mMicroSegment;
}
public int getPreview() {
return mPreview;
}
@Nullable
public String getPreviewType() {
return mPreviewType;
}
public boolean isSnapshot() {
return mSnapshot;
}
public int compareTo(@NonNull String version) {
return compareTo(parse(version));
}
@Override
public int compareTo(@NonNull GradleVersion version) {
return compareTo(version, false);
}
public int compareIgnoringQualifiers(@NonNull String version) {
return compareIgnoringQualifiers(parse(version));
}
public int compareIgnoringQualifiers(@NonNull GradleVersion version) {
return compareTo(version, true);
}
private int compareTo(@NonNull GradleVersion version, boolean ignoreQualifiers) {
int delta = getMajor() - version.getMajor();
if (delta != 0) {
return delta;
}
delta = getMinor() - version.getMinor();
if (delta != 0) {
return delta;
}
delta = getMicro() - version.getMicro();
if (delta != 0) {
return delta;
}
if (!ignoreQualifiers) {
if (mPreviewType == null) {
if (version.mPreviewType != null) {
return 1;
}
} else if (version.mPreviewType == null) {
return -1;
} else {
delta = mPreviewType.compareToIgnoreCase(version.mPreviewType);
}
if (delta != 0) {
return delta;
}
delta = mPreview - version.mPreview;
if (delta != 0) {
return delta;
}
delta = mSnapshot == version.mSnapshot ? 0 : (mSnapshot ? -1 : 1);
}
return delta;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
GradleVersion that = (GradleVersion) o;
return compareTo(that) == 0;
}
@Override
public int hashCode() {
return Objects.hashCode(mMajorSegment, mMinorSegment, mMicroSegment, mPreview, mPreviewType,
mSnapshot);
}
@Override
public String toString() {
return mRawValue;
}
/**
* @return version segments present after the "micro" segments. For example, parsing "1.2.3.4.5"
* will result in "4" and "5" to be considered "additional" version segments.
*/
@NonNull
public List<VersionSegment> getAdditionalSegments() {
return mAdditionalSegments;
}
public static class VersionSegment implements Serializable {
@NonNull
private final String mText;
private final int mValue;
VersionSegment(int value) {
mText = String.valueOf(value);
mValue = value;
}
VersionSegment(@NonNull String text) {
mText = text;
if (PLUS.equals(text)) {
mValue = Integer.MAX_VALUE;
} else {
// +1 is a valid number which will be parsed correctly but it is not a correct
// version segment.
if (text.startsWith(PLUS)) {
throw new NumberFormatException("Version segment cannot start with +");
}
int value;
try {
value = Integer.parseInt(text);
}
catch (NumberFormatException e) {
value = 0;
}
mValue = value;
}
}
@NonNull
public String getText() {
return mText;
}
public int getValue() {
return mValue;
}
public boolean acceptsGreaterValue() {
return PLUS.equals(mText);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
VersionSegment that = (VersionSegment) o;
return Objects.equal(mText, that.mText);
}
@Override
public int hashCode() {
return Objects.hashCode(mText);
}
@Override
public String toString() {
return mText;
}
}
}