-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathVersion.java
More file actions
98 lines (81 loc) · 3.12 KB
/
Version.java
File metadata and controls
98 lines (81 loc) · 3.12 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
package nodebox.versioncheck;
public class Version implements Comparable {
private static final int LARGER_THAN = 1;
private static final int SMALLER_THAN = -1;
private static final int EQUAL = 0;
private String versionString;
public Version(String version) {
versionString = version;
}
@Override
public String toString() {
return versionString;
}
private static Integer parseInt(String s) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
return null;
}
}
public int compareTo(Object o) {
if (!(o instanceof Version)) return -1;
Version other = (Version) o;
String[] thisParts = this.versionString.split("\\.");
String[] otherParts = other.versionString.split("\\.");
if (this.versionString.equals(other.versionString))
return EQUAL;
int partCount = Math.min(thisParts.length, otherParts.length);
for (int i = 0; i < partCount; i++) {
String thisPart = thisParts[i];
String otherPart = otherParts[i];
Integer thisPartInt = parseInt(thisPart);
Integer otherPartInt = parseInt(otherPart);
if (thisPartInt != null && otherPartInt != null) {
// Two numbers, can compare.
if (thisPartInt > otherPartInt) {
return LARGER_THAN;
} else if (thisPartInt < otherPartInt) {
return SMALLER_THAN;
}
} else {
if (thisPartInt == null && otherPartInt != null) {
// This is a string, the other part is a number.
// String wins.
return LARGER_THAN;
} else if (thisPartInt != null && otherPartInt == null) {
// This is a number, the other part is not.
// String wins.
return SMALLER_THAN;
} else {
// Two strings. Compare them.
int comparison = thisPart.compareTo(otherPart);
// Only return if they are equal, otherwise keep on checking.
if (comparison != 0) {
return comparison;
}
}
}
}
// We're still here. Version string with most segments wins.
if (thisParts.length > otherParts.length) {
return LARGER_THAN;
} else if (thisParts.length < otherParts.length) {
return SMALLER_THAN;
} else {
throw new AssertionError("Parts appear to be equal but they are not. " + this.versionString + " -- " + other.versionString);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Version version = (Version) o;
if (!versionString.equals(version.versionString)) return false;
return true;
}
@Override
public int hashCode() {
return versionString.hashCode();
}
}