summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/misc/VersionHelper.java
blob: 6cfc87292d86764e249b58553dfd8dbc70dc5056 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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;
			}
		};
	}
}