-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComparableVersion.pde
45 lines (38 loc) · 1.02 KB
/
ComparableVersion.pde
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
class ComparableVersion implements Comparable<ComparableVersion> {
private int[] numbers;
String versionString;
ComparableVersion(String versionString) {
this.versionString = versionString;
String[] strs = versionString.split("\\.", 3);
numbers = new int[strs.length];
for (int i=0; i<strs.length; i++) {
numbers[i] = Integer.parseInt(strs[i]);
}
}
int number(int level) {
if (level >= numbers.length) {
return -1;
}
return numbers[level];
}
int compareTo(ComparableVersion other, int level) {
int thisNum = this.number(level);
int otherNum = other.number(level);
int res = Integer.compare(thisNum, otherNum);
if (res == 0) {
if (thisNum >= 0 && otherNum >= 0) {
res = compareTo(other, level + 1);
}
}
return res;
}
int compareTo(ComparableVersion other) {
return compareTo(other, 0);
}
int compareTo(String other) {
return compareTo(new ComparableVersion(other));
}
String toString() {
return versionString;
}
}