From cacc6a2b951308e25b204d0418519096182ba57a Mon Sep 17 00:00:00 2001 From: Pepich Date: Fri, 10 Mar 2017 13:44:00 +0100 Subject: [PATCH] Expanded functionality of VersionHelper --- src/com/redstoner/misc/VersionHelper.java | 108 ++++++++++++++++++++-- 1 file changed, 98 insertions(+), 10 deletions(-) diff --git a/src/com/redstoner/misc/VersionHelper.java b/src/com/redstoner/misc/VersionHelper.java index 86a5381..f815c7c 100644 --- a/src/com/redstoner/misc/VersionHelper.java +++ b/src/com/redstoner/misc/VersionHelper.java @@ -1,34 +1,79 @@ package com.redstoner.misc; +import java.lang.annotation.Annotation; + import com.redstoner.annotations.Version; import com.redstoner.exceptions.MissingVersionException; /** This class can be used to compare modules against the loader version or against each other to prevent dependency issues. * * @author Pepich */ -@Version(major = 1, minor = 0, revision = 0, compatible = -1) +@Version(major = 2, minor = 1, revision = 0, compatible = 0) public final class VersionHelper { private VersionHelper() {} - /** Checks two modules versions for compatibility. + /** Checks two classes versions for compatibility. * - * @param base The base to compare to. + * @param base The API to compare to. * @param module The module to compare. - * @return true, when the Major version of the base is bigger than the compatible version of the module, and the Version number of the base is smaller or equal to the Version number of the module. + * @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 base, Class module) throws MissingVersionException + public static boolean isCompatible(Class api, Class module) throws MissingVersionException { - if (!base.isAnnotationPresent(Version.class)) - throw new MissingVersionException("The base object is not annotated with a version."); + 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 baseVersion = base.getClass().getAnnotation(Version.class); + Version apiVersion = api.getClass().getAnnotation(Version.class); Version moduleVersion = module.getClass().getAnnotation(Version.class); - if (baseVersion.major() > moduleVersion.major()) + 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(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.getClass().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.getClass().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. + * @throws MissingVersionException When one of the parameters is not annotated with a @Version annotation. */ + public static boolean isCompatible(Version apiVersion, Version moduleVersion) + { + if (apiVersion.major() >= moduleVersion.compatible()) + return true; + if (apiVersion.compatible() == -1) return false; - return baseVersion.major() >= moduleVersion.compatible(); + if (apiVersion.compatible() <= moduleVersion.major()) + return true; + return false; } /** Returns the version of a given class as a String. @@ -43,4 +88,47 @@ public final class VersionHelper Version ver = clazz.getAnnotation(Version.class); return ver.major() + "." + ver.minor() + "." + ver.revision() + "." + ver.compatible(); } + + /** 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 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; + } + }; + } }