Java_Version_x.y.z_tester/src/com/company/Version.java

124 lines
3.4 KiB
Java
Raw Normal View History

2019-10-13 13:05:37 +00:00
package com.company;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2019-10-14 10:22:16 +00:00
/**
* Author 6543
* Datum 2019-10-14
* License GPL3
*/
2019-10-13 13:12:48 +00:00
public enum Version {
UNKNOWN,
SUPPORTED_LATEST,
SUPPORTED_OLD,
DEVELOPMENT,
UNSUPPORTED_OLD,
UNSUPPORTED_NEW;
2019-10-13 13:05:37 +00:00
2019-10-13 13:12:48 +00:00
public static Version check(String min, String last, String value) {
2019-10-13 13:05:37 +00:00
final Pattern pattern_stable_release = Pattern.compile("^(\\d)\\.(\\d+)\\.(\\d+)$");
final Pattern pattern_dev_release = Pattern.compile("^(\\d).(\\d+).(\\d+)(\\D)(.+)");
Matcher m ;
2019-10-20 19:58:29 +00:00
if (!pattern_stable_release.matcher(min).find() || !pattern_stable_release.matcher(last).find()) {
throw new Error("VersionCheck: wrong format for min or last version given");
}
2019-10-13 13:05:37 +00:00
m = pattern_stable_release.matcher(value);
if (m.find()) {
switch (correlate(min, last, m.group())){
case 0:
2019-10-13 13:12:48 +00:00
return UNSUPPORTED_OLD;
2019-10-13 13:05:37 +00:00
case 1:
2019-10-13 13:12:48 +00:00
return SUPPORTED_OLD;
2019-10-13 13:05:37 +00:00
case 2:
2019-10-13 13:12:48 +00:00
return SUPPORTED_LATEST;
2019-10-13 13:05:37 +00:00
default:
2019-10-13 13:12:48 +00:00
return UNSUPPORTED_NEW;
2019-10-13 13:05:37 +00:00
}
}
m = pattern_dev_release.matcher(value);
if (m.find()) {
m = Pattern.compile("^(\\d)\\.(\\d+)\\.(\\d+)").matcher(value);
m.find();
if (correlate(min, last, m.group())>0) {
2019-10-13 13:12:48 +00:00
return DEVELOPMENT;
2019-10-13 13:05:37 +00:00
} else {
2019-10-13 13:12:48 +00:00
return UNSUPPORTED_OLD;
2019-10-13 13:05:37 +00:00
}
}
2019-10-13 13:12:48 +00:00
return UNKNOWN;
2019-10-13 13:05:37 +00:00
}
//helper
// 0 to less
2019-10-20 19:58:29 +00:00
// 1 in range
2019-10-13 13:05:37 +00:00
// 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;
2019-10-13 13:21:11 +00:00
//go down each integer (separated by points) until it is the last one
2019-10-20 19:58:29 +00:00
while (value.contains(".")) {
2019-10-13 13:05:37 +00:00
//prepare for checks
2019-10-20 19:58:29 +00:00
i_min = Integer.parseInt(min.substring(0,min.indexOf(".")));
i_last = Integer.parseInt(last.substring(0,last.indexOf(".")));
i_value = Integer.parseInt(value.substring(0,value.indexOf(".")));
if (i_value < i_min) return 0;
if (i_value > i_last) return 3;
//check in range correlation
int result = correlate(min.substring(min.indexOf(".")+1),last.substring(last.indexOf(".")+1),value.substring(value.indexOf(".")+1));
if (i_value == i_last) {
switch (result){
case 0:
return 0;
case 1:
return 1;
case 2:
if (i_value < i_last ) return 1;
if (i_value == i_last) return 2;
case 3:
return 3;
}
} else { //value in range
return 1;
2019-10-13 13:05:37 +00:00
}
}
2019-10-20 19:58:29 +00:00
i_min = Integer.parseInt(min);
i_last = Integer.parseInt(last);
i_value = Integer.parseInt(value);
2019-10-13 13:05:37 +00:00
2019-10-13 13:21:11 +00:00
//check last integer
2019-10-20 19:58:29 +00:00
if (i_min > i_last){
if (i_value == i_last) return 2;
if (i_value < i_last) return 1;
}
2019-10-13 13:05:37 +00:00
if (i_value < i_min) return 0;
2019-10-20 19:58:29 +00:00
if (i_value >= i_min && i_value < i_last) return 1;
2019-10-13 13:05:37 +00:00
if (i_value == i_last) return 2;
2019-10-20 19:58:29 +00:00
return 3;
}
2019-10-13 13:05:37 +00:00
return 0;
}
}