-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVersion.java
More file actions
278 lines (231 loc) · 9 KB
/
Version.java
File metadata and controls
278 lines (231 loc) · 9 KB
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
package org.utplsql.api;
import org.utplsql.api.exception.InvalidVersionException;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toMap;
/**
* Simple class to parse utPLSQL Version-information and provide the separate version-numbers
*
* @author pesse
*/
public class Version implements Comparable<Version> {
public final static Version V3_0_0 = new Version("3.0.0", 3, 0, 0, null, true);
public final static Version V3_0_1 = new Version("3.0.1", 3, 0, 1, null, true);
public final static Version V3_0_2 = new Version("3.0.2", 3, 0, 2, null, true);
public final static Version V3_0_3 = new Version("3.0.3", 3, 0, 3, null, true);
public final static Version V3_0_4 = new Version("3.0.4", 3, 0, 4, null, true);
public final static Version V3_1_0 = new Version("3.1.0", 3, 1, 0, 1847, true);
public final static Version V3_1_1 = new Version("3.1.1", 3, 1, 1, 1865, true);
public final static Version V3_1_2 = new Version("3.1.2", 3, 1, 2, 2130, true);
public final static Version V3_1_3 = new Version("3.1.3", 3, 1, 3, 2398, true);
public final static Version V3_1_4 = new Version("3.1.4", 3, 1, 4, 2223, true);
public final static Version V3_1_5 = new Version("3.1.5", 3, 1, 5, 2707, true);
public final static Version V3_1_6 = new Version("3.1.6", 3, 1, 6, 2729, true);
public final static Version V3_1_7 = new Version("3.1.7", 3, 1, 7, 3085, true);
public final static Version V3_1_8 = new Version("3.1.8", 3, 1, 8, 3188, true);
public final static Version V3_1_9 = new Version("3.1.9", 3, 1, 9, 3268, true);
public final static Version V3_1_10 = new Version("3.1.10", 3, 1, 10, 3347, true);
public final static Version V3_1_11 = new Version("3.1.11", 3, 1, 11, 3557, true);
public final static Version V3_1_12 = new Version("3.1.12", 3, 1, 12, 3876, true);
private final static Map<String, Version> knownVersions =
Stream.of(V3_0_0, V3_0_1, V3_0_2, V3_0_3, V3_0_4, V3_1_0, V3_1_1, V3_1_2, V3_1_3, V3_1_4, V3_1_5, V3_1_6, V3_1_7, V3_1_8, V3_1_9, V3_1_10, V3_1_11, V3_1_12)
.collect(toMap(Version::toString, Function.identity()));
public final static Version LATEST = V3_1_12;
private final String origString;
private final Integer major;
private final Integer minor;
private final Integer bugfix;
private final Integer build;
private final boolean valid;
private Version(String origString, @Nullable Integer major, @Nullable Integer minor, @Nullable Integer bugfix, @Nullable Integer build, boolean valid) {
this.origString = origString;
this.major = major;
this.minor = minor;
this.bugfix = bugfix;
this.build = build;
this.valid = valid;
}
/**
* Use {@link Version#create} factory method instead
* For removal
*/
@Deprecated()
public Version(String versionString) {
assert versionString != null;
Version dummy = parseVersionString(versionString);
this.origString = dummy.origString;
this.major = dummy.major;
this.minor = dummy.minor;
this.bugfix = dummy.bugfix;
this.build = dummy.build;
this.valid = dummy.valid;
}
public static Version create(final String versionString) {
String origString = Objects.requireNonNull(versionString).trim();
Version version = knownVersions.get(origString);
return version != null ? version : parseVersionString(origString);
}
private static Version parseVersionString(String origString) {
Integer major = null;
Integer minor = null;
Integer bugfix = null;
Integer build = null;
boolean valid = false;
Pattern p = Pattern.compile("([0-9]+)\\.?([0-9]+)?\\.?([0-9]+)?\\.?([0-9]+)?");
Matcher m = p.matcher(origString);
try {
if (m.find()) {
if (m.group(1) != null) {
major = Integer.valueOf(m.group(1));
}
if (m.group(2) != null) {
minor = Integer.valueOf(m.group(2));
}
if (m.group(3) != null) {
bugfix = Integer.valueOf(m.group(3));
}
if (m.group(4) != null) {
build = Integer.valueOf(m.group(4));
}
// We need a valid major version as minimum requirement for a Version object to be valid
valid = major != null;
}
} catch (NumberFormatException e) {
valid = false;
}
return new Version(origString, major, minor, bugfix, build, valid);
}
@Override
public String toString() {
return origString;
}
@Nullable
public Integer getMajor() {
return major;
}
@Nullable
public Integer getMinor() {
return minor;
}
@Nullable
public Integer getBugfix() {
return bugfix;
}
@Nullable
public Integer getBuild() {
return build;
}
public boolean isValid() {
return valid;
}
/**
* Returns a normalized form of the parsed version information
*
* @return
*/
public String getNormalizedString() {
if (isValid()) {
StringBuilder sb = new StringBuilder();
sb.append(major);
if (minor != null) {
sb.append(".").append(minor);
}
if (bugfix != null) {
sb.append(".").append(bugfix);
}
if (build != null) {
sb.append(".").append(build);
}
return sb.toString();
} else {
return "invalid";
}
}
private int compareToWithNulls(@Nullable Integer i1, @Nullable Integer i2) {
return compareToWithNulls(i1, i2, false);
}
private int compareToWithNulls(@Nullable Integer i1, @Nullable Integer i2, boolean nullMeansEqual) {
if (i1 == null && i2 == null) {
return 0;
} else if (i1 == null) {
return nullMeansEqual ? 0 : -1;
} else if (i2 == null) {
return 1;
} else {
return i1.compareTo(i2);
}
}
@Override
public int compareTo(Version o) {
return compareTo(o, false);
}
public int compareTo(Version o, boolean nullMeansEqual) {
int curResult;
if (isValid() && o.isValid()) {
curResult = compareToWithNulls(getMajor(), o.getMajor(), nullMeansEqual);
if (curResult != 0) {
return curResult;
}
curResult = compareToWithNulls(getMinor(), o.getMinor(), nullMeansEqual);
if (curResult != 0) {
return curResult;
}
curResult = compareToWithNulls(getBugfix(), o.getBugfix(), nullMeansEqual);
if (curResult != 0) {
return curResult;
}
curResult = compareToWithNulls(getBuild(), o.getBuild(), nullMeansEqual);
if (curResult != 0) {
return curResult;
}
}
return 0;
}
private void versionsAreValid(Version v) throws InvalidVersionException {
if (!isValid()) {
throw new InvalidVersionException(this);
}
if (!v.isValid()) {
throw new InvalidVersionException(v);
}
}
/**
* Compares this version to a given version and returns true if this version is greater or equal than the given one
* If one of the version parts of the base version is null, this level is assumed equal no matter the comparing level's version part
* Throws an InvalidVersionException if either this or the given version are invalid
*
* @param v Version to compare with
* @return
* @throws InvalidVersionException
*/
public boolean isGreaterOrEqualThan(Version v) throws InvalidVersionException {
versionsAreValid(v);
return compareTo(v, true) >= 0;
}
public boolean isGreaterThan(Version v) throws InvalidVersionException {
versionsAreValid(v);
return compareTo(v) > 0;
}
/**
* Compares this version to a given version and returns true if this version is less or equal than the given one
* If one of the version parts of the base version is null, this level is assumed equal no matter the comparing level's version part
* Throws an InvalidVersionException if either this or the given version are invalid
*
* @param v Version to compare with
* @return
* @throws InvalidVersionException
*/
public boolean isLessOrEqualThan(Version v) throws InvalidVersionException {
versionsAreValid(v);
return compareTo(v, true) <= 0;
}
public boolean isLessThan(Version v) throws InvalidVersionException {
versionsAreValid(v);
return compareTo(v) < 0;
}
}