|
14 | 14 | package org.eclipse.esmf.samm; |
15 | 15 |
|
16 | 16 | import java.util.Arrays; |
| 17 | +import java.util.Comparator; |
17 | 18 | import java.util.List; |
18 | 19 | import java.util.Optional; |
19 | 20 |
|
20 | 21 | /** |
21 | 22 | * The known versions of the Aspect Meta Model |
22 | 23 | */ |
23 | 24 | public enum KnownVersion { |
24 | | - SAMM_1_0_0, |
25 | | - SAMM_2_0_0, |
26 | | - SAMM_2_1_0, |
27 | | - SAMM_2_2_0; |
| 25 | + SAMM_1_0_0( 1, 0, 0 ), |
| 26 | + SAMM_2_0_0( 2, 0, 0 ), |
| 27 | + SAMM_2_1_0( 2, 1, 0 ), |
| 28 | + SAMM_2_2_0( 2, 2, 0 ); |
| 29 | + |
| 30 | + private final int major; |
| 31 | + private final int minor; |
| 32 | + private final int patch; |
| 33 | + private final String versionString; |
| 34 | + |
| 35 | + KnownVersion( int major, int minor, int patch ) { |
| 36 | + this.major = major; |
| 37 | + this.minor = minor; |
| 38 | + this.patch = patch; |
| 39 | + this.versionString = String.format( "%d.%d.%d", major, minor, patch ); |
| 40 | + } |
28 | 41 |
|
29 | 42 | /** |
30 | 43 | * Returns this version as a standard version string, e.g. 1.2.3 |
31 | 44 | * |
32 | 45 | * @return version string representation |
33 | 46 | */ |
34 | 47 | public String toVersionString() { |
35 | | - return toString().replaceFirst( "SAMM_(\\d+)_(\\d+)_(\\d+)", "$1.$2.$3" ); |
| 48 | + return versionString; |
36 | 49 | } |
37 | 50 |
|
38 | 51 | public static Optional<KnownVersion> fromVersionString( final String version ) { |
39 | | - return Arrays.stream( KnownVersion.values() ) |
40 | | - .filter( value -> value.toVersionString().equals( version ) ) |
| 52 | + return Arrays.stream( values() ) |
| 53 | + .filter( v -> v.versionString.equals( version ) ) |
41 | 54 | .findAny(); |
42 | 55 | } |
43 | 56 |
|
44 | 57 | public static KnownVersion getLatest() { |
45 | | - return Arrays.asList( KnownVersion.values() ).get( KnownVersion.values().length - 1 ); |
| 58 | + return Arrays.stream( values() ) |
| 59 | + .max( Comparator.comparingInt( KnownVersion::versionCode ) ) |
| 60 | + .orElseThrow(); // Should never be empty |
46 | 61 | } |
47 | 62 |
|
48 | 63 | public static List<KnownVersion> getVersions() { |
49 | | - return Arrays.asList( values() ); |
| 64 | + return List.of( values() ); |
| 65 | + } |
| 66 | + |
| 67 | + public boolean isNewerThan( final KnownVersion other ) { |
| 68 | + return this.versionCode() > other.versionCode(); |
50 | 69 | } |
51 | 70 |
|
52 | | - public boolean isNewerThan( final KnownVersion otherVersion ) { |
53 | | - return toVersionString().compareTo( otherVersion.toVersionString() ) > 0; |
| 71 | + public boolean isOlderThan( final KnownVersion other ) { |
| 72 | + return this.versionCode() < other.versionCode(); |
54 | 73 | } |
55 | 74 |
|
56 | | - public boolean isOlderThan( final KnownVersion otherVersion ) { |
57 | | - return toVersionString().compareTo( otherVersion.toVersionString() ) < 0; |
| 75 | + private int versionCode() { |
| 76 | + return major * 1_000_000 + minor * 1_000 + patch; |
58 | 77 | } |
59 | 78 | } |
0 commit comments