1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3;
18
19 import org.apache.commons.lang3.math.NumberUtils;
20
21
22
23
24
25
26
27
28 public enum JavaVersion {
29
30
31
32
33 JAVA_0_9(1.5f, "0.9"),
34
35
36
37
38 JAVA_1_1(1.1f, "1.1"),
39
40
41
42
43 JAVA_1_2(1.2f, "1.2"),
44
45
46
47
48 JAVA_1_3(1.3f, "1.3"),
49
50
51
52
53 JAVA_1_4(1.4f, "1.4"),
54
55
56
57
58 JAVA_1_5(1.5f, "1.5"),
59
60
61
62
63 JAVA_1_6(1.6f, "1.6"),
64
65
66
67
68 JAVA_1_7(1.7f, "1.7"),
69
70
71
72
73 JAVA_1_8(1.8f, "1.8"),
74
75
76
77
78
79
80 @Deprecated
81 JAVA_1_9(9.0f, "9"),
82
83
84
85
86
87
88 JAVA_9(9.0f, "9"),
89
90
91
92
93
94
95 JAVA_10(10.0f, "10"),
96
97
98
99
100
101
102 JAVA_11(11.0f, "11"),
103
104
105
106
107
108
109 JAVA_12(12.0f, "12"),
110
111
112
113
114
115
116 JAVA_13(13.0f, "13"),
117
118
119
120
121
122
123 JAVA_14(14.0f, "14"),
124
125
126
127
128
129
130 JAVA_15(15.0f, "15"),
131
132
133
134
135
136
137 JAVA_16(16.0f, "16"),
138
139
140
141
142
143
144 JAVA_17(17.0f, "17"),
145
146
147
148
149
150
151 JAVA_18(18.0f, "18"),
152
153
154
155
156
157
158 JAVA_19(19.0f, "19"),
159
160
161
162
163
164
165 JAVA_20(20, "20"),
166
167
168
169
170
171
172 JAVA_21(21, "21"),
173
174
175
176
177
178
179 JAVA_22(22, "22"),
180
181
182
183
184 JAVA_RECENT(maxVersion(), Float.toString(maxVersion()));
185
186
187
188
189 private static final String VERSION_SPLIT_REGEX = "\\.";
190
191
192
193
194
195
196
197
198
199
200 static JavaVersion get(final String versionStr) {
201 if (versionStr == null) {
202 return null;
203 }
204 switch (versionStr) {
205 case "0.9":
206 return JAVA_0_9;
207 case "1.1":
208 return JAVA_1_1;
209 case "1.2":
210 return JAVA_1_2;
211 case "1.3":
212 return JAVA_1_3;
213 case "1.4":
214 return JAVA_1_4;
215 case "1.5":
216 return JAVA_1_5;
217 case "1.6":
218 return JAVA_1_6;
219 case "1.7":
220 return JAVA_1_7;
221 case "1.8":
222 return JAVA_1_8;
223 case "9":
224 return JAVA_9;
225 case "10":
226 return JAVA_10;
227 case "11":
228 return JAVA_11;
229 case "12":
230 return JAVA_12;
231 case "13":
232 return JAVA_13;
233 case "14":
234 return JAVA_14;
235 case "15":
236 return JAVA_15;
237 case "16":
238 return JAVA_16;
239 case "17":
240 return JAVA_17;
241 case "18":
242 return JAVA_18;
243 case "19":
244 return JAVA_19;
245 case "20":
246 return JAVA_20;
247 case "21":
248 return JAVA_21;
249 case "22":
250 return JAVA_22;
251 default:
252 final float v = toFloatVersion(versionStr);
253 if (v - 1. < 1.) {
254 final int firstComma = Math.max(versionStr.indexOf('.'), versionStr.indexOf(','));
255 final int end = Math.max(versionStr.length(), versionStr.indexOf(',', firstComma));
256 if (Float.parseFloat(versionStr.substring(firstComma + 1, end)) > .9f) {
257 return JAVA_RECENT;
258 }
259 } else if (v > 10) {
260 return JAVA_RECENT;
261 }
262 return null;
263 }
264 }
265
266
267
268
269
270
271
272
273
274
275 static JavaVersion getJavaVersion(final String versionStr) {
276 return get(versionStr);
277 }
278
279
280
281
282
283
284 private static float maxVersion() {
285 final float v = toFloatVersion(SystemProperties.getJavaSpecificationVersion("99.0"));
286 return v > 0 ? v : 99f;
287 }
288
289 static String[] split(final String value) {
290 return value.split(VERSION_SPLIT_REGEX);
291 }
292
293
294
295
296
297
298
299 private static float toFloatVersion(final String value) {
300 final int defaultReturnValue = -1;
301 if (!value.contains(".")) {
302 return NumberUtils.toFloat(value, defaultReturnValue);
303 }
304 final String[] toParse = split(value);
305 if (toParse.length >= 2) {
306 return NumberUtils.toFloat(toParse[0] + '.' + toParse[1], defaultReturnValue);
307 }
308 return defaultReturnValue;
309 }
310
311
312
313
314 private final float value;
315
316
317
318
319 private final String name;
320
321
322
323
324
325
326
327 JavaVersion(final float value, final String name) {
328 this.value = value;
329 this.name = name;
330 }
331
332
333
334
335
336
337
338
339
340
341 public boolean atLeast(final JavaVersion requiredVersion) {
342 return this.value >= requiredVersion.value;
343 }
344
345
346
347
348
349
350
351
352
353
354
355 public boolean atMost(final JavaVersion requiredVersion) {
356 return this.value <= requiredVersion.value;
357 }
358
359
360
361
362
363
364
365
366 @Override
367 public String toString() {
368 return name;
369 }
370 }