1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package edu.internet2.middleware.grouperInstaller;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import edu.internet2.middleware.grouperInstaller.util.GrouperInstallerUtils;
28
29
30
31
32
33
34
35 public class GiGrouperVersion {
36
37
38
39
40
41
42
43 public static String stringValueOrNull(String versionString) {
44 if (GrouperInstallerUtils.isBlank(versionString)) {
45 return null;
46 }
47 return valueOfIgnoreCase(versionString, true).toString();
48 }
49
50
51
52
53 @Override
54 public boolean equals(Object obj) {
55 if (!(obj instanceof GiGrouperVersion)) {
56 return false;
57 }
58 GiGrouperVersion../edu/internet2/middleware/grouperInstaller/GiGrouperVersion.html#GiGrouperVersion">GiGrouperVersion other = (GiGrouperVersion)obj;
59 if (this.build != other.build) {
60 return false;
61 }
62 if (this.major != other.major) {
63 return false;
64 }
65 if (this.minor != other.minor) {
66 return false;
67 }
68 if (!GrouperInstallerUtils.equals(this.rc, other.rc)) {
69 return false;
70 }
71 return true;
72 }
73
74
75
76
77
78
79 @Override
80 public int hashCode() {
81
82 return ("" + this.major + "" + this.minor + "" + this.build + "" + this.rc).hashCode();
83
84 }
85
86
87
88
89
90
91 @Override
92 public String toString() {
93 String result = this.major + "." + this.minor + "." + this.build;
94 if (this.rc != null) {
95 result+= "rc" + this.rc;
96 }
97 return result;
98 }
99
100
101
102
103 private static GiGrouperVersion currentVersion = null;
104
105
106
107
108
109 public static GiGrouperVersion currentVersion() {
110 if (currentVersion == null) {
111 currentVersion = valueOfIgnoreCase(GrouperInstallerUtils.grouperInstallerVersion());
112 }
113 return currentVersion;
114 }
115
116
117
118 private int major;
119
120
121 private int minor;
122
123
124 private int build;
125
126
127 private Integer rc;
128
129
130 private static Map<String, GiGrouperVersion> versionCache = new HashMap<String, GiGrouperVersion>();
131
132
133
134
135
136
137
138 public static GiGrouperVersion valueOfIgnoreCase(String string) {
139 return valueOfIgnoreCase(string, true);
140 }
141
142
143
144
145
146
147
148
149 public static GiGrouperVersion valueOfIgnoreCase(String string, boolean exceptionOnNull) {
150
151 if (GrouperInstallerUtils.isBlank(string)) {
152 if (exceptionOnNull) {
153 throw new RuntimeException("Not expecting a blank string for version");
154 }
155 return null;
156 }
157
158
159 GiGrouperVersion grouperVersion = versionCache.get(string);
160 if (grouperVersion != null) {
161 return grouperVersion;
162 }
163
164 grouperVersion = new GiGrouperVersion(string);
165
166
167 if (versionCache.size() < 1000) {
168 versionCache.put(string, grouperVersion);
169 }
170 return grouperVersion;
171 }
172
173
174
175
176
177 public GiGrouperVersion(String versionString) {
178 Matcher grouperMatcher = shortPattern.matcher(versionString);
179
180
181 if (!grouperMatcher.matches()) {
182
183 grouperMatcher = pattern.matcher(versionString);
184
185
186 if (!grouperMatcher.matches()) {
187
188
189 grouperMatcher = rcPattern.matcher(versionString);
190
191
192 if (!grouperMatcher.matches()) {
193
194
195 throw new RuntimeException("Invalid version: " + versionString
196 + ", expecting something like: 1.1 or 1.2.3 or 1.2.3rc4");
197 }
198 this.rc = GrouperInstallerUtils.intValue(grouperMatcher.group(4));
199 }
200 this.build = GrouperInstallerUtils.intValue(grouperMatcher.group(3));
201
202 }
203
204 this.major = GrouperInstallerUtils.intValue(grouperMatcher.group(1));
205 this.minor = GrouperInstallerUtils.intValue(grouperMatcher.group(2));
206
207 }
208
209
210
211
212
213
214 public static boolean grouperVersionGreaterOrEqual(String version) {
215 return _grouperVersionGreaterOrEqualHelper(currentVersion().toString(), version);
216 }
217
218
219
220
221
222
223 public boolean greaterOrEqualToArg(String version) {
224 return this.thisGreaterThanArgument(new GiGrouperVersion(version), true);
225 }
226
227
228
229
230
231
232
233 public boolean lessThanArg(GiGrouperVersion other, boolean orEqual) {
234
235 return other.thisGreaterThanArgument(this, orEqual);
236 }
237
238
239
240
241
242
243
244 public boolean lessThanMajorMinorArg(GiGrouperVersion other, boolean orEqual) {
245
246 GiGrouperVersion thisMajorMinor = valueOfIgnoreCase(this.major + "." + this.minor + ".0");
247 GiGrouperVersion otherMajorMinor = valueOfIgnoreCase(other.major + "." + other.minor + ".0");
248
249 return thisMajorMinor.lessThanArg(otherMajorMinor, orEqual);
250
251 }
252
253
254
255
256
257
258 public boolean lessThanArg(GiGrouperVersion other) {
259 return this.lessThanArg(other, false);
260 }
261
262
263
264
265
266
267 public boolean greaterOrEqualToArg(GiGrouperVersion version) {
268 return this.thisGreaterThanArgument(version, true);
269 }
270
271
272
273
274
275
276
277
278 private static Pattern pattern = Pattern.compile("^[vV]?(\\d+)[\\._](\\d+)[\\._](\\d+)$");
279
280
281
282
283
284
285
286
287 private static Pattern shortPattern = Pattern.compile("^[vV]?(\\d+)[\\._](\\d+)$");
288
289
290
291
292
293
294
295
296
297 private static Pattern rcPattern = Pattern.compile("^[vV]?(\\d+)[\\._](\\d+)[\\._](\\d+)\\-?rc(\\d+)$");
298
299
300
301
302
303
304
305 public static boolean _grouperVersionGreaterOrEqualHelper(String grouperVersion, String anotherVersion) {
306 GiGrouperVersioner/GiGrouperVersion.html#GiGrouperVersion">GiGrouperVersion grouper = new GiGrouperVersion(grouperVersion);
307 GiGrouperVersioner/GiGrouperVersion.html#GiGrouperVersion">GiGrouperVersion another = new GiGrouperVersion(anotherVersion);
308
309 return grouper.thisGreaterThanArgument(another, true);
310 }
311
312
313
314
315
316
317
318 private boolean thisGreaterThanArgument(GiGrouperVersion anotherVersion, boolean orEqual) {
319
320 if (this.major > anotherVersion.major) {
321 return true;
322 } else if (this.major < anotherVersion.major) {
323 return false;
324 }
325 if (this.minor > anotherVersion.minor) {
326 return true;
327 } else if (this.minor < anotherVersion.minor) {
328 return false;
329 }
330
331 if (this.build > anotherVersion.build) {
332 return true;
333 } else if (this.build < anotherVersion.build) {
334 return false;
335 }
336 if (GrouperInstallerUtils.equals(this.rc, anotherVersion.rc)) {
337 return orEqual;
338 }
339
340 if (this.rc == null) {
341 return true;
342 }
343 if (anotherVersion.rc == null) {
344 return false;
345 }
346 return this.rc > anotherVersion.rc;
347 }
348 }