-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
VersionNumber.java
322 lines (267 loc) · 9.47 KB
/
VersionNumber.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
/*
* Copyright 2012 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 org.gradle.util;
import com.google.common.base.Objects;
import com.google.common.collect.Ordering;
import org.gradle.internal.deprecation.DeprecationLogger;
import javax.annotation.Nullable;
/**
* This class is only here to maintain binary compatibility with existing plugins.
*
* @deprecated Will be removed in Gradle 9.0.
*/
@Deprecated
public class VersionNumber implements Comparable<VersionNumber> {
private static void logDeprecation(int majorVersion) {
DeprecationLogger.deprecateType(VersionNumber.class)
.willBeRemovedInGradle9()
.withUpgradeGuideSection(majorVersion, "org_gradle_util_reports_deprecations")
.nagUser();
}
private static final DefaultScheme DEFAULT_SCHEME = new DefaultScheme();
private static final SchemeWithPatchVersion PATCH_SCHEME = new SchemeWithPatchVersion();
public static final VersionNumber UNKNOWN = version(0);
private final int major;
private final int minor;
private final int micro;
private final int patch;
private final String qualifier;
private final AbstractScheme scheme;
private boolean deprecationLogged;
public VersionNumber(int major, int minor, int micro, @Nullable String qualifier) {
this(major, minor, micro, 0, qualifier, DEFAULT_SCHEME, true);
}
public VersionNumber(int major, int minor, int micro, int patch, @Nullable String qualifier) {
this(major, minor, micro, patch, qualifier, PATCH_SCHEME, true);
}
private VersionNumber(int major, int minor, int micro, int patch, @Nullable String qualifier, AbstractScheme scheme, boolean logDeprecation) {
this.major = major;
this.minor = minor;
this.micro = micro;
this.patch = patch;
this.qualifier = qualifier;
this.scheme = scheme;
if (logDeprecation) {
logDeprecation(7);
deprecationLogged = true;
}
}
private void maybeLogDeprecation() {
if(!deprecationLogged) {
logDeprecation(7);
}
}
public int getMajor() {
maybeLogDeprecation();
return major;
}
public int getMinor() {
maybeLogDeprecation();
return minor;
}
public int getMicro() {
maybeLogDeprecation();
return micro;
}
public int getPatch() {
maybeLogDeprecation();
return patch;
}
@Nullable
public String getQualifier() {
maybeLogDeprecation();
return qualifier;
}
public VersionNumber getBaseVersion() {
maybeLogDeprecation();
return new VersionNumber(major, minor, micro, patch, null, scheme, false);
}
@Override
public int compareTo(VersionNumber other) {
logDeprecation(8);
if (major != other.major) {
return major - other.major;
}
if (minor != other.minor) {
return minor - other.minor;
}
if (micro != other.micro) {
return micro - other.micro;
}
if (patch != other.patch) {
return patch - other.patch;
}
return Ordering.natural().nullsLast().compare(toLowerCase(qualifier), toLowerCase(other.qualifier));
}
@Override
public boolean equals(@Nullable Object other) {
return other instanceof VersionNumber && compareTo((VersionNumber) other) == 0;
}
@Override
public int hashCode() {
int result = major;
result = 31 * result + minor;
result = 31 * result + micro;
result = 31 * result + patch;
result = 31 * result + Objects.hashCode(qualifier);
return result;
}
@Override
public String toString() {
maybeLogDeprecation();
return scheme.format(this);
}
public static VersionNumber version(int major) {
return version(major, 0);
}
public static VersionNumber version(int major, int minor) {
return new VersionNumber(major, minor, 0, 0, null, DEFAULT_SCHEME, false);
}
/**
* Returns the default MAJOR.MINOR.MICRO-QUALIFIER scheme.
*/
public static Scheme scheme() {
logDeprecation(7);
return DEFAULT_SCHEME;
}
/**
* Returns the MAJOR.MINOR.MICRO.PATCH-QUALIFIER scheme.
*/
public static Scheme withPatchNumber() {
logDeprecation(7);
return PATCH_SCHEME;
}
public static VersionNumber parse(String versionString) {
logDeprecation(8);
return DEFAULT_SCHEME.parse(versionString);
}
@Nullable
private String toLowerCase(@Nullable String string) {
return string == null ? null : string.toLowerCase();
}
/**
* Returns the version number scheme.
*/
@Deprecated
public interface Scheme {
VersionNumber parse(String value);
String format(VersionNumber versionNumber);
}
private abstract static class AbstractScheme implements Scheme {
final int depth;
protected AbstractScheme(int depth) {
this.depth = depth;
}
@Override
public VersionNumber parse(@Nullable String versionString) {
if (versionString == null || versionString.length() == 0) {
return UNKNOWN;
}
Scanner scanner = new Scanner(versionString);
int major = 0;
int minor = 0;
int micro = 0;
int patch = 0;
if (!scanner.hasDigit()) {
return UNKNOWN;
}
major = scanner.scanDigit();
if (scanner.isSeparatorAndDigit('.')) {
scanner.skipSeparator();
minor = scanner.scanDigit();
if (scanner.isSeparatorAndDigit('.')) {
scanner.skipSeparator();
micro = scanner.scanDigit();
if (depth > 3 && scanner.isSeparatorAndDigit('.', '_')) {
scanner.skipSeparator();
patch = scanner.scanDigit();
}
}
}
if (scanner.isEnd()) {
return new VersionNumber(major, minor, micro, patch, null, this, false);
}
if (scanner.isQualifier()) {
scanner.skipSeparator();
return new VersionNumber(major, minor, micro, patch, scanner.remainder(), this, false);
}
return UNKNOWN;
}
private static class Scanner {
int pos;
final String str;
private Scanner(String string) {
this.str = string;
}
boolean hasDigit() {
return pos < str.length() && Character.isDigit(str.charAt(pos));
}
boolean isSeparatorAndDigit(char... separators) {
return pos < str.length() - 1 && oneOf(separators) && Character.isDigit(str.charAt(pos + 1));
}
private boolean oneOf(char... separators) {
char current = str.charAt(pos);
for (int i = 0; i < separators.length; i++) {
char separator = separators[i];
if (current == separator) {
return true;
}
}
return false;
}
boolean isQualifier() {
return pos < str.length() - 1 && oneOf('.', '-');
}
int scanDigit() {
int start = pos;
while (hasDigit()) {
pos++;
}
return Integer.parseInt(str.substring(start, pos));
}
public boolean isEnd() {
return pos == str.length();
}
public void skipSeparator() {
pos++;
}
@Nullable
public String remainder() {
return pos == str.length() ? null : str.substring(pos);
}
}
}
private static class DefaultScheme extends AbstractScheme {
private static final String VERSION_TEMPLATE = "%d.%d.%d%s";
public DefaultScheme() {
super(3);
}
@Override
public String format(VersionNumber versionNumber) {
return String.format(VERSION_TEMPLATE, versionNumber.major, versionNumber.minor, versionNumber.micro, versionNumber.qualifier == null ? "" : "-" + versionNumber.qualifier);
}
}
private static class SchemeWithPatchVersion extends AbstractScheme {
private static final String VERSION_TEMPLATE = "%d.%d.%d.%d%s";
private SchemeWithPatchVersion() {
super(4);
}
@Override
public String format(VersionNumber versionNumber) {
return String.format(VERSION_TEMPLATE, versionNumber.major, versionNumber.minor, versionNumber.micro, versionNumber.patch, versionNumber.qualifier == null ? "" : "-" + versionNumber.qualifier);
}
}
}