1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package Utilities;
21
22 public class Version {
23
24 private final int major;
25 private final int minor;
26 private final int revision;
27
28 public Version(int major, int minor, int revision) {
29 this.major = major;
30 this.minor = minor;
31 this.revision = revision;
32 }
33
34 public Version(String version) {
35 String[] versionArray = version.split("\\.");
36 this.major = versionArray.length >= 1 ? Integer.valueOf(versionArray[0]) : 0;
37 this.minor = versionArray.length >= 2 ? Integer.valueOf(versionArray[1]) : 0;
38 this.revision = versionArray.length >= 3 ? Integer.valueOf(versionArray[2]) : 0;
39 }
40
41 public int getMajor() {
42 return major;
43 }
44
45 public int getMinor() {
46 return minor;
47 }
48
49 public int getRevision() {
50 return revision;
51 }
52
53 public boolean greaterThan(String version) {
54 String[] versionArray = version.split("\\.");
55
56 int majorVal = versionArray.length >= 1 ? Integer.valueOf(versionArray[0]) : 0;
57 int minorVal = versionArray.length >= 2 ? Integer.valueOf(versionArray[1]) : 0;
58 int revisionVal = versionArray.length >= 3 ? Integer.valueOf(versionArray[2]) : 0;
59 return major > majorVal || (major >= majorVal && minor > minorVal) || (major >= majorVal && minor >= minorVal && revision > revisionVal);
60 }
61
62 public static String format(String version) {
63 return new Version(version).toString();
64 }
65
66 public boolean greaterThanOrEqual(String version) {
67 String[] versionArray = version.split("\\.");
68 int majorVal = versionArray.length >= 1 ? Integer.valueOf(versionArray[0]) : 0;
69 int minorVal = versionArray.length >= 2 ? Integer.valueOf(versionArray[1]) : 0;
70 int revisionVal = versionArray.length >= 3 ? Integer.valueOf(versionArray[2]) : 0;
71 return (major >= majorVal && minor >= minorVal && revision >= revisionVal) || (major > majorVal) || (major >= majorVal && minor > minorVal);
72 }
73
74 public boolean greaterThan(Version version) {
75
76 int majorVal = version.major;
77 int minorVal = version.minor;
78 int revisionVal = version.revision;
79 return major > majorVal || (major >= majorVal && minor > minorVal) || (major >= majorVal && minor >= minorVal && revision > revisionVal);
80 }
81
82 @Override
83 public boolean equals(Object version) {
84 if (version == null) return false;
85 if (version == this) return true;
86 if (!(version instanceof String) && !(version instanceof Version)) return false;
87 if (version instanceof Version) {
88 return (major == ((Version) version).major && minor == ((Version) version).minor && revision == ((Version) version).revision);
89
90 } else {
91 String[] versionArray = ((String) version).split("\\.");
92 int majorVal = versionArray.length >= 1 ? Integer.valueOf(versionArray[0]) : 0;
93 int minorVal = versionArray.length >= 2 ? Integer.valueOf(versionArray[1]) : 0;
94 int revisionVal = versionArray.length >= 3 ? Integer.valueOf(versionArray[2]) : 0;
95 return (major == majorVal && minor == minorVal && revision == revisionVal);
96
97 }
98 }
99
100 @Override
101 public int hashCode() {
102 return Integer.hashCode(major) + Integer.hashCode(minor) + Integer.hashCode(revision);
103 }
104
105 public boolean greaterThanOrEqual(Version version) {
106 int majorVal = version.major;
107 int minorVal = version.minor;
108 int revisionVal = version.revision;
109 return (major >= majorVal && minor >= minorVal && revision >= revisionVal) || (major > majorVal) || (major >= majorVal && minor > minorVal);
110 }
111
112 @Override
113 public String toString() {
114 return major + "." + minor + "." + revision;
115 }
116 }