Version Check before try to Login #147
@ -25,6 +25,10 @@ public enum VersionTest {
|
||||
final Pattern pattern_dev_release = Pattern.compile("^(\\d).(\\d+).(\\d+)(\\D)(.+)");
|
||||
Matcher m ;
|
||||
|
||||
if (!pattern_stable_release.matcher(min).find() || !pattern_stable_release.matcher(last).find()) {
|
||||
throw new IllegalArgumentException("VersionCheck: wrong format for min or last version given");
|
||||
}
|
||||
|
||||
m = pattern_stable_release.matcher(value);
|
||||
if (m.find()) {
|
||||
|
||||
@ -60,46 +64,71 @@ public enum VersionTest {
|
||||
|
||||
//helper
|
||||
// 0 to less
|
||||
// 1 in spectrum
|
||||
// 1 in range
|
||||
// 2 at the top
|
||||
// 3 above
|
||||
private static int correlate(String min, String last, String value){
|
||||
//init
|
||||
int i_min = 0, i_last = 0, i_value = 0;
|
||||
|
||||
//go down each integer (separated by points) until it is the last one
|
||||
while (value.indexOf(".")>0) {
|
||||
|
||||
//prepare for checks
|
||||
if (min.indexOf(".") >= 0) i_min = Integer.valueOf(min.substring(0,min.indexOf(".")));
|
||||
if (last.indexOf(".") >= 0) i_last = Integer.valueOf(last.substring(0,last.indexOf(".")));
|
||||
if (value.indexOf(".") >= 0) i_value = Integer.valueOf(value.substring(0,value.indexOf(".")));
|
||||
|
||||
if ( i_min != i_last ) {
|
||||
//check
|
||||
if (i_value < i_min) return 0;
|
||||
if (i_value > i_last) return 3;
|
||||
}
|
||||
|
||||
//delete checked integer and move on to next one
|
||||
min = min.substring(min.indexOf(".")+1);
|
||||
last = last.substring(last.indexOf(".")+1);
|
||||
value = value.substring(value.indexOf(".")+1);
|
||||
int min_check = compareVersion(value,min);
|
||||
int max_check = compareVersion(value,last);
|
||||
int range_check = compareVersion(min,last);
|
||||
|
||||
switch (range_check) {
|
||||
case 2:
|
||||
throw new IllegalArgumentException("Minimum Version higher than Last Version");
|
||||
case 1: //min == last
|
||||
switch (min_check) {
|
||||
case 0:
|
||||
return 0;
|
||||
case 1:
|
||||
return 2;
|
||||
default:
|
||||
return 3;
|
||||
}
|
||||
default:
|
||||
if (max_check >1) return 3;
|
||||
if (max_check == 1) return 2;
|
||||
if (min_check < 1) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
i_min = Integer.valueOf(min);
|
||||
i_last = Integer.valueOf(last);
|
||||
i_value = Integer.valueOf(value);
|
||||
|
||||
//check last integer
|
||||
if (i_value < i_min) return 0;
|
||||
if (i_min < i_value && i_value < i_last) return 1;
|
||||
if (i_value == i_last) return 2;
|
||||
if (i_value > i_last) return 3;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @description compare doted formatted Versions
|
||||
* @param A doted formatted Versions
|
||||
* @param B doted formatted Versions
|
||||
* @return 0|1|2
|
||||
* 0 = less
|
||||
* 1 = same
|
||||
* 2 = more
|
||||
*/
|
||||
public static int compareVersion(String A, String B) {
|
||||
//throw new IllegalArgumentException
|
||||
if((!A.matches("[0-9]+(\\.[0-9]+)*")) || (!B.matches("[0-9]+(\\.[0-9]+)*"))) throw new IllegalArgumentException("Invalid version format");
|
||||
|
||||
if (A.contains(".") || B.contains(".")) {
|
||||
// example 2 vs 1.3
|
||||
if (!(A.contains(".") && B.contains("."))) {
|
||||
if (A.contains(".")) {
|
||||
return compareVersion(A,B + ".0");
|
||||
}
|
||||
if (B.contains(".")) {
|
||||
return compareVersion(A + ".0",B);
|
||||
}
|
||||
}
|
||||
|
||||
//normal compare
|
||||
int a = Integer.parseInt(A.substring(0,A.indexOf(".")));
|
||||
int b = Integer.parseInt(B.substring(0,B.indexOf(".")));
|
||||
if (a < b) return 0;
|
||||
if (a == b) return compareVersion(A.substring(A.indexOf(".")+1),B.substring(B.indexOf(".")+1));
|
||||
return 2; //if (a > b)
|
||||
} else {
|
||||
int a = Integer.parseInt(A);
|
||||
int b = Integer.parseInt(B);
|
||||
if (a < b) return 0;
|
||||
if (a == b) return 1;
|
||||
return 2; //if (a > b)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user