Apply suggestions from code review
This commit is contained in:
94
src/main/java/enovy/server/util/VersionUtils.java
Normal file
94
src/main/java/enovy/server/util/VersionUtils.java
Normal file
@ -0,0 +1,94 @@
|
||||
package enovy.server.util;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Implements a comparison algorithm between Envoy versions and defines minimal
|
||||
* and maximal client versions compatible with this server.
|
||||
* <p>
|
||||
* Project: <strong>envoy-server-standalone</strong><br>
|
||||
* File: <strong>VersionUtils.java</strong><br>
|
||||
* Created: <strong>23.06.2020</strong><br>
|
||||
*
|
||||
* @author KSKE
|
||||
* @since Envoy Server Standalone v0.1-beta
|
||||
*/
|
||||
public class VersionUtils {
|
||||
|
||||
/**
|
||||
* The minimal client version compatible with this server.
|
||||
*
|
||||
* @since Envoy Server Standalone v0.1-beta
|
||||
*/
|
||||
public static final String MIN_CLIENT_VERSION = "0.1-beta";
|
||||
|
||||
/**
|
||||
* The maximal client version compatible with this server.
|
||||
*
|
||||
* @since Envoy Server Standalone v0.1-beta
|
||||
*/
|
||||
public static final String MAX_CLIENT_VERSION = "0.1-beta";
|
||||
|
||||
private static final Pattern versionPattern = Pattern.compile("(?<major>\\d).(?<minor>\\d)(?:-(?<suffix>\\w+))?");
|
||||
|
||||
private VersionUtils() {}
|
||||
|
||||
/**
|
||||
* Parses an Envoy Client version string and checks whether that version is
|
||||
* compatible with this server.
|
||||
*
|
||||
* @param version the version string to parse
|
||||
* @return {@code true} if the given version is compatible with this server
|
||||
* @since Envoy Server Standalone v0.1-beta
|
||||
*/
|
||||
public static boolean verifyCompatibility(String version) {
|
||||
final var currentMatcher = versionPattern.matcher(version);
|
||||
|
||||
if (!currentMatcher.matches()) return false;
|
||||
|
||||
final var minMatcher = versionPattern.matcher(MIN_CLIENT_VERSION);
|
||||
final var maxMatcher = versionPattern.matcher(MAX_CLIENT_VERSION);
|
||||
|
||||
if (!minMatcher.matches() || !maxMatcher.matches()) throw new RuntimeException("Invalid min or max client version configured!");
|
||||
|
||||
// Compare suffixes
|
||||
{
|
||||
final var currentSuffix = convertSuffix(currentMatcher.group("suffix"));
|
||||
final var minSuffix = convertSuffix(minMatcher.group("suffix"));
|
||||
final var maxSuffix = convertSuffix(maxMatcher.group("suffix"));
|
||||
|
||||
if (currentSuffix < minSuffix || currentSuffix > maxSuffix) return false;
|
||||
}
|
||||
|
||||
// Compare major
|
||||
{
|
||||
final var currentMajor = Integer.parseInt(currentMatcher.group("major"));
|
||||
final var minMajor = Integer.parseInt(minMatcher.group("major"));
|
||||
final var maxMajor = Integer.parseInt(maxMatcher.group("major"));
|
||||
|
||||
if (currentMajor < minMajor || currentMajor > maxMajor) return false;
|
||||
}
|
||||
|
||||
// Compare minor
|
||||
{
|
||||
final var currentMinor = Integer.parseInt(currentMatcher.group("minor"));
|
||||
final var minMinor = Integer.parseInt(minMatcher.group("minor"));
|
||||
final var maxMinor = Integer.parseInt(maxMatcher.group("minor"));
|
||||
|
||||
if (currentMinor < minMinor || currentMinor > maxMinor) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static int convertSuffix(String suffix) {
|
||||
switch (suffix == null ? "" : suffix) {
|
||||
case "alpha":
|
||||
return 0;
|
||||
case "beta":
|
||||
return 1;
|
||||
default:
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
}
|
11
src/main/java/enovy/server/util/package-info.java
Normal file
11
src/main/java/enovy/server/util/package-info.java
Normal file
@ -0,0 +1,11 @@
|
||||
/**
|
||||
* This package contains utility classes used in Envoy Server.
|
||||
* <p>
|
||||
* Project: <strong>envoy-server-standalone</strong><br>
|
||||
* File: <strong>package-info.java</strong><br>
|
||||
* Created: <strong>23.06.2020</strong><br>
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since Envoy Server Standalone v0.1-beta
|
||||
*/
|
||||
package enovy.server.util;
|
Reference in New Issue
Block a user