This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/server/src/main/java/envoy/server/util/VersionUtil.java

97 lines
2.7 KiB
Java

package envoy.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.
*
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-beta
*/
public final class VersionUtil {
/**
* 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.2-beta";
private static final Pattern versionPattern =
Pattern.compile("(?<major>\\d).(?<minor>\\d)(?:-(?<suffix>\\w+))?");
private VersionUtil() {}
/**
* 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;
}
}
}