-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathVersion.java
More file actions
99 lines (79 loc) · 2.69 KB
/
Version.java
File metadata and controls
99 lines (79 loc) · 2.69 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
package nodebox.node;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Version {
private static final Pattern VERSION_PATTERN = Pattern.compile("^([0-9]+\\.){0,2}[0-9]+$");
private static Logger logger = Logger.getLogger("nodebox.node.Version");
private int major, minor, revision;
public static Version parseVersionString(String s) throws IllegalArgumentException {
return new Version(s);
}
public Version() {
major = 1;
minor = 0;
revision = 0;
}
public Version(int major, int minor) {
this(major, minor, 0);
}
public Version(int major, int minor, int revision) {
this.major = major;
this.minor = minor;
this.revision = revision;
}
public Version(String versionString) throws IllegalArgumentException {
Matcher m = VERSION_PATTERN.matcher(versionString);
if (!m.matches()) {
throw new IllegalArgumentException("Version string " + versionString + " is not a valid version (expection 1.0.0)");
}
String[] bits = versionString.split("\\.");
if (bits.length >= 1)
major = Integer.parseInt(bits[0]);
if (bits.length >= 2)
minor = Integer.parseInt(bits[1]);
if (bits.length >= 3)
revision = Integer.parseInt(bits[2]);
}
public int getMajor() {
return major;
}
public int getMinor() {
return minor;
}
public int getRevision() {
return revision;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Version)) return false;
Version v = (Version) o;
return major == v.major &&
minor == v.minor &&
revision == v.revision;
}
public boolean largerThan(Version other) {
if (major > other.major) return true;
if (major == other.major && minor > other.minor) return true;
if (major == other.major && minor == other.minor && revision > other.revision) return true;
return false;
}
public boolean largerOrEqualThan(Version other) {
if (largerThan(other)) return true;
return major == other.major && minor == other.minor && revision == other.revision;
}
public boolean smallerOrEqualThan(Version other) {
return !largerThan(other);
}
public boolean smallerThan(Version other) {
return !largerOrEqualThan(other);
}
public String toString() {
return major + "." + minor + "." + revision;
}
@Override
public Version clone() {
return new Version(major, minor, revision);
}
}