0
This repository has been archived on 2024-08-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ModuleLoader/src/main/java/com/redstoner/misc/VersionHelper.java
2019-02-08 18:49:51 -05:00

164 lines
5.2 KiB
Java

package com.redstoner.misc;
import com.redstoner.annotations.Version;
import com.redstoner.exceptions.MissingVersionException;
import java.lang.annotation.Annotation;
/**
* This class can be used to compare modules against the loader version or against each other to prevent dependency issues.
*
* @author Pepich
*/
@Version (major = 2, minor = 1, revision = 3, compatible = 0)
public final class VersionHelper {
private VersionHelper() {}
/**
* Checks two classes versions for compatibility.
*
* @param base The API to compare to.
* @param module The module to compare.
*
* @return true, when the module is up to date with the API, or the API supports outdated modules.
*
* @throws MissingVersionException When one of the parameters is not annotated with a @Version annotation.
*/
public static boolean isCompatible(Class<?> api, Class<?> module) throws MissingVersionException {
if (!api.isAnnotationPresent(Version.class))
throw new MissingVersionException("The API is not annotated with a version.");
if (!module.isAnnotationPresent(Version.class))
throw new MissingVersionException("The module is not annotated with a version.");
Version apiVersion = api.getAnnotation(Version.class);
Version moduleVersion = module.getAnnotation(Version.class);
return isCompatible(apiVersion, moduleVersion);
}
/**
* Checks two versions for compatibility.
*
* @param base The API version to compare to.
* @param module The module version to compare.
*
* @return true, when the module is up to date with the API, or the API supports outdated modules.
*/
public static boolean isCompatible(Version apiVersion, Version moduleVersion) {
if (apiVersion.major() >= moduleVersion.compatible())
return true;
if (apiVersion.compatible() == -1)
return false;
if (apiVersion.compatible() <= moduleVersion.major())
return true;
return false;
}
/**
* Checks two classes versions for compatibility.
*
* @param base The API to compare to.
* @param module The module to compare.
*
* @return true, when the module is up to date with the API, or the API supports outdated modules.
*
* @throws MissingVersionException When one of the parameters is not annotated with a @Version annotation.
*/
public static boolean isCompatible(Version apiVersion, Class<?> module) throws MissingVersionException {
if (!module.isAnnotationPresent(Version.class))
throw new MissingVersionException("The module is not annotated with a version.");
Version moduleVersion = module.getAnnotation(Version.class);
return isCompatible(apiVersion, moduleVersion);
}
/**
* Checks two classes versions for compatibility.
*
* @param base The API to compare to.
* @param module The module to compare.
*
* @return true, when the module is up to date with the API, or the API supports outdated modules.
*
* @throws MissingVersionException When one of the parameters is not annotated with a @Version annotation.
*/
public static boolean isCompatible(Class<?> api, Version moduleVersion) throws MissingVersionException {
if (!api.isAnnotationPresent(Version.class))
throw new MissingVersionException("The API is not annotated with a version.");
Version apiVersion = api.getAnnotation(Version.class);
return isCompatible(apiVersion, moduleVersion);
}
/**
* Returns the version of a given class as a String.
*
* @param clazz The class to grab the version number from.
*
* @return The version number of the class in format major.minor.revision.compatible.
*
* @throws MissingVersionException If the class is not annotated with @Version.
*/
public static String getVersion(Class<?> clazz) throws MissingVersionException {
if (!clazz.isAnnotationPresent(Version.class))
throw new MissingVersionException("The given class is not associated with a version.");
Version ver = clazz.getAnnotation(Version.class);
return getString(ver);
}
/**
* Returns the String representation of a version.
*
* @param ver The version to be represented.
*
* @return The String representation.
*/
public static String getString(Version ver) {
return ver.major() + "." + ver.minor() + "." + ver.revision() + "." + ver.compatible();
}
public static Version getVersion(String ver) {
String[] raw = ver.split("\\.");
if (raw.length != 4)
return null;
return VersionHelper.create(Integer.parseInt(raw[0]), Integer.parseInt(raw[1]), Integer.parseInt(raw[2]),
Integer.parseInt(raw[3])
);
}
/**
* This method creates a new Version to use for compatibility checks.
*
* @param major The major version
* @param minor The minor version
* @param revision The revision
* @param compatible The compatibility tag
*
* @return
*/
public static Version create(int major, int minor, int revision, int compatible) {
return new Version() {
@Override
public Class<? extends Annotation> annotationType() {
return Version.class;
}
@Override
public int revision() {
return revision;
}
@Override
public int minor() {
return minor;
}
@Override
public int major() {
return major;
}
@Override
public int compatible() {
return compatible;
}
};
}
}