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

132 lines
3.7 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()) {
2019-10-20 20:32:25 +00:00
throw new IllegalArgumentException("VersionCheck: wrong format for min or last version given");
2019-10-20 19:58:29 +00:00
}
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){
2019-10-20 20:17:00 +00:00
int min_check = compare(value,min);
int max_check = compare(value,last);
int range_check = compare(min,last);
switch (range_check) {
case 2:
2019-10-20 20:32:25 +00:00
throw new IllegalArgumentException("Minimum Version higher than Last Version");
2019-10-20 20:17:00 +00:00
case 1: //min == last
switch (min_check) {
2019-10-20 19:58:29 +00:00
case 0:
return 0;
case 1:
2019-10-20 20:17:00 +00:00
return 2;
default:
2019-10-20 19:58:29 +00:00
return 3;
}
2019-10-20 20:17:00 +00:00
default:
if (max_check >1) return 3;
if (max_check == 1) return 2;
if (min_check < 1) return 0;
2019-10-20 19:58:29 +00:00
return 1;
2019-10-13 13:05:37 +00:00
}
2019-10-20 19:58:29 +00:00
}
2019-10-13 13:05:37 +00:00
2019-10-20 20:02:03 +00:00
/**
* @description compare doted formatted Versions
* @return 0|1|2
* 0 = less
* 1 = same
* 2 = more
*/
public static int compare(String A, String B) {
2019-10-20 20:32:25 +00:00
//throw new IllegalArgumentException
if((!A.matches("[0-9]+(\\.[0-9]+)*")) || (!B.matches("[0-9]+(\\.[0-9]+)*"))) throw new IllegalArgumentException("Invalid version format");
2019-10-20 20:02:03 +00:00
if (A.contains(".") || B.contains(".")) {
// example 2 vs 1.3
if (!(A.contains(".") && B.contains("."))) {
if (A.contains(".")) {
return compare(A,B + ".0");
}
if (B.contains(".")) {
return compare(A + ".0",B);
}
}
2019-10-13 13:05:37 +00:00
2019-10-20 20:02:03 +00:00
//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 compare(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)
}
2019-10-13 13:05:37 +00:00
}
2019-10-20 20:02:03 +00:00
}