1 | |
package org.apache.maven.plugin.ear.util; |
2 | |
|
3 | |
import java.util.HashMap; |
4 | |
import java.util.Map; |
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
public class JavaEEVersion |
12 | |
implements Comparable |
13 | |
{ |
14 | |
|
15 | |
private static final String VERSION_1_3 = "1.3"; |
16 | |
|
17 | |
private static final String VERSION_1_4 = "1.4"; |
18 | |
|
19 | |
private static final String VERSION_5 = "5"; |
20 | |
|
21 | |
private static final String VERSION_6 = "6"; |
22 | |
|
23 | 0 | private static final Map versionsMap = new HashMap(); |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public static final JavaEEVersion OneDotThree = new JavaEEVersion( new Integer( 0 ), VERSION_1_3 ); |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public static final JavaEEVersion OneDotFour = new JavaEEVersion( new Integer( 1 ), VERSION_1_4 ); |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public static final JavaEEVersion Five = new JavaEEVersion( new Integer( 2 ), VERSION_5 ); |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public static final JavaEEVersion Six = new JavaEEVersion( new Integer( 3 ), VERSION_6 ); |
45 | |
|
46 | |
|
47 | |
private final Integer index; |
48 | |
|
49 | |
private final String version; |
50 | |
|
51 | |
private JavaEEVersion( Integer index, String version ) |
52 | 0 | { |
53 | 0 | this.index = index; |
54 | 0 | this.version = version; |
55 | 0 | versionsMap.put( version, this ); |
56 | 0 | } |
57 | |
|
58 | |
public static JavaEEVersion getJavaEEVersion( String version ) |
59 | |
throws InvalidJavaEEVersion |
60 | |
{ |
61 | 0 | if ( !isValid( version ) ) |
62 | |
{ |
63 | 0 | throw new InvalidJavaEEVersion( "Invalid version [" + version + "]", version ); |
64 | |
} |
65 | 0 | return (JavaEEVersion) versionsMap.get( version ); |
66 | |
} |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public String getVersion() |
74 | |
{ |
75 | 0 | return version; |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public boolean ge( JavaEEVersion version ) |
85 | |
{ |
86 | 0 | return this.compareTo( version ) >= 0; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public boolean gt( JavaEEVersion version ) |
96 | |
{ |
97 | 0 | return this.compareTo( version ) > 0; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public boolean eq( JavaEEVersion version ) |
107 | |
{ |
108 | 0 | return this.compareTo( version ) == 0; |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public boolean le( JavaEEVersion version ) |
118 | |
{ |
119 | 0 | return this.compareTo( version ) <= 0; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public boolean lt( JavaEEVersion version ) |
130 | |
{ |
131 | 0 | return this.compareTo( version ) < 0; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
private static boolean isValid( String version ) |
141 | |
{ |
142 | 0 | if ( version == null ) |
143 | |
{ |
144 | 0 | throw new IllegalArgumentException( "version could not be null." ); |
145 | |
} |
146 | 0 | return VERSION_1_3.equals( version ) || VERSION_1_4.equals( version ) || VERSION_5.equals( version ) || |
147 | |
VERSION_6.equals( version ); |
148 | |
} |
149 | |
|
150 | |
public int compareTo( Object other ) |
151 | |
{ |
152 | 0 | if ( other == null ) |
153 | |
{ |
154 | 0 | throw new IllegalArgumentException( "other object to compare to could not be null." ); |
155 | |
} |
156 | 0 | if ( !( other instanceof JavaEEVersion ) ) |
157 | |
{ |
158 | 0 | throw new IllegalArgumentException( |
159 | |
"other object to compare must be a JavaEEVersion but was [" + other.getClass().getName() + "]" ); |
160 | |
} |
161 | 0 | final JavaEEVersion otherVersion = (JavaEEVersion) other; |
162 | 0 | return index.compareTo( otherVersion.index ); |
163 | |
} |
164 | |
} |