-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNuixVersion.java
More file actions
153 lines (130 loc) · 3.72 KB
/
NuixVersion.java
File metadata and controls
153 lines (130 loc) · 3.72 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
package com.nuix.nx;
import java.lang.Package;
import java.util.regex.Pattern;
public class NuixVersion implements Comparable<NuixVersion> {
public static NuixVersion currentVersion;
public static void setCurrentVersion(String version){
currentVersion = NuixVersion.parse(version);
}
public static Pattern previewVersionInfoRemovalPattern = Pattern.compile("[^0-9\\.].*$");
private int major = 0;
private int minor = 0;
private int bugfix = 0;
private int build = 0;
public NuixVersion(){
this(0,0,0,0);
}
public NuixVersion(int majorVersion){
this(majorVersion,0,0,0);
}
public NuixVersion(int majorVersion, int minorVersion){
this(majorVersion,minorVersion,0,0);
}
public NuixVersion(int majorVersion, int minorVersion, int bugfixVersion){
this(majorVersion,minorVersion,bugfixVersion,0);
}
public NuixVersion(int majorVersion, int minorVersion, int bugfixVersion, int buildVersion){
major = majorVersion;
minor = minorVersion;
bugfix = bugfixVersion;
build = buildVersion;
}
public static NuixVersion parse(String versionString){
try {
String[] versionParts = NuixVersion.previewVersionInfoRemovalPattern.matcher(versionString.trim()).replaceAll("").split("\\.");
int[] versionPartInts = new int[versionParts.length];
for(int i=0;i<versionParts.length;i++){
versionPartInts[i] = Integer.parseInt(versionParts[i]);
}
switch(versionParts.length){
case 1:
return new NuixVersion(versionPartInts[0]);
case 2:
return new NuixVersion(versionPartInts[0],versionPartInts[1]);
case 3:
return new NuixVersion(versionPartInts[0],versionPartInts[1],versionPartInts[2]);
case 4:
return new NuixVersion(versionPartInts[0],versionPartInts[1],versionPartInts[2],versionPartInts[3]);
default:
return new NuixVersion();
}
}catch(Exception exc){
System.out.println("Error while parsing version: "+versionString);
System.out.println("Pretending version is 100.0.0.0");
return new NuixVersion(100,0,0,0);
}
}
public int getMajor() {
return major;
}
public void setMajor(int major) {
this.major = major;
}
public int getMinor() {
return minor;
}
public void setMinor(int minor) {
this.minor = minor;
}
public int getBugfix() {
return bugfix;
}
public void setBugfix(int bugfix) {
this.bugfix = bugfix;
}
public int getBuild() {
return build;
}
public void setBuild(int build) {
this.build = build;
}
public static NuixVersion getCurrent(){
if(currentVersion != null){
return NuixVersion.currentVersion;
} else {
String versionString = "0.0.0.0";
for(Package p : Package.getPackages()){
if(p.getName().matches("com\\.nuix\\..*")){
versionString = p.getImplementationVersion();
break;
}
}
return NuixVersion.parse(versionString);
}
}
public boolean isLessThan(NuixVersion other){
return this.compareTo(other) < 0;
}
public boolean isAtLeast(NuixVersion other){
return this.compareTo(other) >= 0;
}
public boolean isLessThan(String other){
return isLessThan(parse(other));
}
public boolean isAtLeast(String other){
return isAtLeast(parse(other));
}
@Override
public int compareTo(NuixVersion other) {
if(this.major == other.major){
if(this.minor == other.minor){
if(this.bugfix == other.bugfix) {
return Integer.compare(this.build, other.build);
} else {
return Integer.compare(this.bugfix, other.bugfix);
}
} else{
return Integer.compare(this.minor, other.minor);
}
} else{
return Integer.compare(this.major, other.major);
}
}
@Override
public String toString(){
return Integer.toString(this.major) + "." +
Integer.toString(this.minor) + "." +
Integer.toString(this.bugfix) + "." +
Integer.toString(this.build);
}
}