summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/virtualization/Version.java
blob: c823324366c74365de17d93082456971dcfed2f4 (plain) (blame)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package org.openslx.virtualization;

import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openslx.util.Util;

/**
 * Represents a version information.
 * 
 * The version information is used in the field of virtualization (for virtualizers, disk images,
 * virtualization configuration files, ...).
 * 
 * @author Manuel Bentele
 * @version 1.0
 */
public class Version implements Comparable<Version>
{
	/**
	 * Regular expression to parse a version from a {@link String}.
	 * <p>
	 * The regular expression matches a version if its textual version information is well-formed
	 * according to the following examples:
	 * 
	 * <pre>
	 *   52
	 *   4.31
	 *   5.10.13
	 * </pre>
	 */
	private static final Pattern VERSION_NUMBER_REGEX = Pattern.compile( "^(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?$" );

	/**
	 * Major number of the version.
	 */
	private final short major;

	/**
	 * Minor number of the version.
	 */
	private final short minor;

	/**
	 * Name or description of the version.
	 */
	private final String name;

	/**
	 * Creates a new version.
	 * 
	 * The version consists of a major version, whereas the minor version is set to the value
	 * <code>0</code> and the version name is undefined.
	 * 
	 * @param major major version.
	 */
	public Version( short major )
	{
		this( major, Short.valueOf( "0" ), null );
	}

	/**
	 * Creates a new version.
	 * 
	 * The version consists of a major version labeled with a version name, whereas the minor version
	 * is set to the value <code>0</code>.
	 * 
	 * @param major major version.
	 * @param name version name.
	 */
	public Version( short major, String name )
	{
		this( major, Short.valueOf( "0" ), name );
	}

	/**
	 * Creates a new version.
	 * 
	 * The version consists of a major and a minor version, whereas the version name is undefined.
	 * 
	 * @param major major version.
	 * @param minor minor version.
	 */
	public Version( short major, short minor )
	{
		this( major, minor, null );
	}

	/**
	 * Creates a new version.
	 * 
	 * The version consists of a major and a minor version labeled with a version name.
	 * 
	 * @param major major version.
	 * @param minor minor version.
	 * @param name version name.
	 */
	public Version( short major, short minor, String name )
	{
		this.major = major;
		this.minor = minor;
		this.name = name;
	}

	/**
	 * Returns the major version.
	 * 
	 * @return major version.
	 */
	public short getMajor()
	{
		return this.major;
	}

	/**
	 * Returns the minor version.
	 * 
	 * @return minor version.
	 */
	public short getMinor()
	{
		return this.minor;
	}

	/**
	 * Returns the full version as {@link Integer}.
	 * 
	 * The full version consists of the major and minor version where both are combined in one
	 * {@link Integer} value. The upper 16-bits of the value represent the major number, whereas
	 * the lower 16-bits represent the minor number.
	 * 
	 * @return full version as {@link Integer}.
	 */
	public int getVersion()
	{
		final int major = this.major;
		final int minor = this.minor;

		return ( major << Short.SIZE ) | minor;
	}

	/**
	 * Returns the name of the version.
	 * 
	 * @return name of the version.
	 */
	public String getName()
	{
		return this.name;
	}

	/**
	 * Checks if version is supported by a version from a list of supported versions.
	 * 
	 * @param supportedVersions list of supported versions.
	 * @return state whether version is supported by a version from the list of versions or not.
	 */
	public boolean isSupported( List<Version> supportedVersions )
	{
		return supportedVersions.contains( this );
	}

	/**
	 * Returns a version from a list of supported versions filtered by its given filter predicate.
	 * 
	 * @param byFilter filter predicate.
	 * @param supportedVersions list of supported versions.
	 * @return version from a list of supported versions filtered by its given filter predicate.
	 */
	private static Version getInstanceByPredicateFromVersions( Predicate<Version> byFilter,
			List<Version> supportedVersions )
	{
		return supportedVersions.stream().filter( byFilter ).findFirst().orElse( null );
	}

	/**
	 * Returns a version from a list of supported versions by its given major version.
	 * 
	 * @param major version.
	 * @param supportedVersions list of supported versions.
	 * @return version from a list of supported versions by its given major version.
	 */
	public static Version getInstanceByMajorFromVersions( short major, List<Version> supportedVersions )
	{
		final Predicate<Version> byMajor = version -> major == version.getMajor();
		return Version.getInstanceByPredicateFromVersions( byMajor, supportedVersions );
	}

	/**
	 * Returns a version from a list of supported versions by its given major and minor version.
	 * 
	 * @param major version.
	 * @param minor version.
	 * @param supportedVersions list of supported versions.
	 * @return version from a list of supported versions by its given major and minor version.
	 */
	public static Version getInstanceByMajorMinorFromVersions( short major, short minor,
			List<Version> supportedVersions )
	{
		final Predicate<Version> byMajorMinor = version -> major == version.getMajor() && minor == version.getMinor();
		return supportedVersions.stream().filter( byMajorMinor ).findFirst().orElse( null );
	}

	/**
	 * Checks if this version is smaller than a specified {@code version}.
	 * 
	 * @param version for comparison.
	 * @return state whether this version is smaller than the specified {@code version} or not.
	 */
	public boolean isSmallerThan( Version version )
	{
		return ( this.compareTo( version ) < 0 ) ? true : false;
	}

	/**
	 * Checks if this version is greater than a specified {@code version}.
	 * 
	 * @param version for comparison.
	 * @return state whether this version is greater than the specified {@code version} or not.
	 */
	public boolean isGreaterThan( Version version )
	{
		return ( this.compareTo( version ) > 0 ) ? true : false;
	}

	/**
	 * Creates a new version parsed from a {@link String}.
	 * 
	 * The version consists of a major and a minor version parsed from the specified {@link String}.
	 * 
	 * @param version textual information containing a version as {@link String}. The textual
	 *           version should be well-formed according to the defined regular expression
	 *           {@link #VERSION_NUMBER_REGEX}.
	 * @return version instance.
	 */
	public static Version valueOf( String version )
	{
		final Version parsedVersion;

		if ( Util.isEmptyString( version ) ) {
			parsedVersion = null;
		} else {
			final Matcher versionMatcher = VERSION_NUMBER_REGEX.matcher( version );

			if ( versionMatcher.find() ) {
				final String majorStr = versionMatcher.group( 1 );
				final String minorStr = versionMatcher.group( 2 );

				final short major = ( majorStr != null ) ? Short.valueOf( majorStr ) : 0;
				final short minor = ( minorStr != null ) ? Short.valueOf( minorStr ) : 0;

				parsedVersion = new Version( major, minor );

			} else {
				parsedVersion = null;
			}
		}

		return parsedVersion;
	}

	@Override
	public String toString()
	{
		if ( Util.isEmptyString( this.getName() ) ) {
			return String.format( "%d.%d", this.getMajor(), this.getMinor() );
		} else {
			return String.format( "%d.%d %s", this.getMajor(), this.getMinor(), this.getName() );
		}
	}

	@Override
	public boolean equals( Object obj )
	{
		if ( obj == null ) {
			return false;
		} else if ( this.getClass() != obj.getClass() ) {
			return false;
		} else if ( this.compareTo( Version.class.cast( obj ) ) == 0 ) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public int compareTo( Version v )
	{
		// compare the current version to the specified version
		if ( this.getMajor() < v.getMajor() ) {
			// current major version is smaller than the given major version
			return -1;
		} else if ( this.getMajor() > v.getMajor() ) {
			// current major version is larger than the given major version
			return 1;
		} else {
			// current major version is equal to the given major version
			// so compare the current minor version to the specified minor version
			if ( this.getMinor() < v.getMinor() ) {
				// current minor version is smaller than the given minor version
				// so the entire version is smaller than the given version
				return -1;
			} else if ( this.getMinor() > v.getMinor() ) {
				// current minor version is larger than the given minor version
				// so the entire version is larger than the given version
				return 1;
			} else {
				// current minor version is equal to the given minor version
				// so the entire version is equal to the given version
				return 0;
			}
		}
	}

	@Override
	public int hashCode()
	{
		return ( Short.valueOf( this.getMajor() ).hashCode() ) ^ ( Short.valueOf( this.getMinor() ).hashCode() );
	}
}