View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3;
18  
19  import org.apache.commons.lang3.math.NumberUtils;
20  
21  /**
22   * Enumerates all known versions of the Java specification. This is intended to mirror available values from the <em>java.specification.version</em> System
23   * property.
24   *
25   * @since 3.0
26   */
27  public enum JavaVersion {
28  
29      /**
30       * The Java version reported by Android. This is not an official Java version number.
31       */
32      JAVA_0_9(1.5f, "0.9"),
33  
34      /**
35       * Java 1.1.
36       */
37      JAVA_1_1(1.1f, "1.1"),
38  
39      /**
40       * Java 1.2.
41       */
42      JAVA_1_2(1.2f, "1.2"),
43  
44      /**
45       * Java 1.3.
46       */
47      JAVA_1_3(1.3f, "1.3"),
48  
49      /**
50       * Java 1.4.
51       */
52      JAVA_1_4(1.4f, "1.4"),
53  
54      /**
55       * Java 1.5.
56       */
57      JAVA_1_5(1.5f, "1.5"),
58  
59      /**
60       * Java 1.6.
61       */
62      JAVA_1_6(1.6f, "1.6"),
63  
64      /**
65       * Java 1.7.
66       */
67      JAVA_1_7(1.7f, "1.7"),
68  
69      /**
70       * Java 1.8.
71       */
72      JAVA_1_8(1.8f, "1.8"),
73  
74      /**
75       * Java 1.9.
76       *
77       * @deprecated As of release 3.5, replaced by {@link #JAVA_9}.
78       */
79      @Deprecated
80      JAVA_1_9(9.0f, "9"),
81  
82      /**
83       * Java 9.
84       *
85       * @since 3.5
86       */
87      JAVA_9(9.0f, "9"),
88  
89      /**
90       * Java 10.
91       *
92       * @since 3.7
93       */
94      JAVA_10(10.0f, "10"),
95  
96      /**
97       * Java 11.
98       *
99       * @since 3.8
100      */
101     JAVA_11(11.0f, "11"),
102 
103     /**
104      * Java 12.
105      *
106      * @since 3.9
107      */
108     JAVA_12(12.0f, "12"),
109 
110     /**
111      * Java 13.
112      *
113      * @since 3.9
114      */
115     JAVA_13(13.0f, "13"),
116 
117     /**
118      * Java 14.
119      *
120      * @since 3.11
121      */
122     JAVA_14(14.0f, "14"),
123 
124     /**
125      * Java 15.
126      *
127      * @since 3.11
128      */
129     JAVA_15(15.0f, "15"),
130 
131     /**
132      * Java 16.
133      *
134      * @since 3.11
135      */
136     JAVA_16(16.0f, "16"),
137 
138     /**
139      * Java 17.
140      *
141      * @since 3.12.0
142      */
143     JAVA_17(17.0f, "17"),
144 
145     /**
146      * Java 18.
147      *
148      * @since 3.13.0
149      */
150     JAVA_18(18.0f, "18"),
151 
152     /**
153      * Java 19.
154      *
155      * @since 3.13.0
156      */
157     JAVA_19(19.0f, "19"),
158 
159     /**
160      * Java 20.
161      *
162      * @since 3.13.0
163      */
164     JAVA_20(20, "20"),
165 
166     /**
167      * Java 21.
168      *
169      * @since 3.13.0
170      */
171     JAVA_21(21, "21"),
172 
173     /**
174      * Java 22.
175      *
176      * @since 3.15.0
177      */
178     JAVA_22(22, "22"),
179 
180     /**
181      * Java 23.
182      *
183      * @since 3.18.0
184      */
185     JAVA_23(23, "23"),
186 
187     /**
188      * Java 24.
189      *
190      * @since 3.18.0
191      */
192     JAVA_24(24, "24"),
193 
194     /**
195      * Java 25.
196      *
197      * @since 3.20.0
198      */
199     JAVA_25(25, "25"),
200 
201     /**
202      * Java 26.
203      *
204      * @since 3.20.0
205      */
206     JAVA_26(26, "26"),
207 
208     /**
209      * Java 27.
210      *
211      * @since 3.21.0
212      */
213     JAVA_27(27, "27"),
214 
215     /**
216      * The most recent Java version. Mainly introduced to avoid to break when a new version of Java is used.
217      */
218     JAVA_RECENT(maxVersion(), Float.toString(maxVersion()));
219 
220     /**
221      * Transforms the given string with a Java version number to the corresponding constant of this enumeration class. This method is used internally.
222      *
223      * @param versionStr the Java version as string.
224      * @return the corresponding enumeration constant or <strong>null</strong> if the version is unknown.
225      */
226     static JavaVersion get(final String versionStr) {
227         if (versionStr == null) {
228             return null;
229         }
230         switch (versionStr) {
231         case "0.9":
232             return JAVA_0_9;
233         case "1.1":
234             return JAVA_1_1;
235         case "1.2":
236             return JAVA_1_2;
237         case "1.3":
238             return JAVA_1_3;
239         case "1.4":
240             return JAVA_1_4;
241         case "1.5":
242             return JAVA_1_5;
243         case "1.6":
244             return JAVA_1_6;
245         case "1.7":
246             return JAVA_1_7;
247         case "1.8":
248             return JAVA_1_8;
249         case "9":
250             return JAVA_9;
251         case "10":
252             return JAVA_10;
253         case "11":
254             return JAVA_11;
255         case "12":
256             return JAVA_12;
257         case "13":
258             return JAVA_13;
259         case "14":
260             return JAVA_14;
261         case "15":
262             return JAVA_15;
263         case "16":
264             return JAVA_16;
265         case "17":
266             return JAVA_17;
267         case "18":
268             return JAVA_18;
269         case "19":
270             return JAVA_19;
271         case "20":
272             return JAVA_20;
273         case "21":
274             return JAVA_21;
275         case "22":
276             return JAVA_22;
277         case "23":
278             return JAVA_23;
279         case "24":
280             return JAVA_24;
281         case "25":
282             return JAVA_25;
283         case "26":
284             return JAVA_26;
285         case "27":
286             return JAVA_27;
287         default:
288             final float v = toFloatVersion(versionStr);
289             if (v - 1. < 1.) { // then we need to check decimals > .9
290                 final int firstComma = Math.max(versionStr.indexOf('.'), versionStr.indexOf(','));
291                 final int end = Math.max(versionStr.length(), versionStr.indexOf(',', firstComma));
292                 if (Float.parseFloat(versionStr.substring(firstComma + 1, end)) > .9f) {
293                     return JAVA_RECENT;
294                 }
295             } else if (v > 10) {
296                 return JAVA_RECENT;
297             }
298             return null;
299         }
300     }
301 
302     /**
303      * Transforms the given string with a Java version number to the corresponding constant of this enumeration class. This method is used internally.
304      *
305      * @param versionStr the Java version as string.
306      * @return the corresponding enumeration constant or <strong>null</strong> if the version is unknown.
307      */
308     static JavaVersion getJavaVersion(final String versionStr) {
309         return get(versionStr);
310     }
311 
312     /**
313      * Gets the Java Version from the system or 99.0 if the {@code java.specification.version} system property is not set.
314      *
315      * @return the value of {@code java.specification.version} system property or 99.0 if it is not set.
316      */
317     private static float maxVersion() {
318         final float v = toFloatVersion(SystemProperties.getJavaSpecificationVersion("99.0"));
319         return v > 0 ? v : 99f;
320     }
321 
322     static String[] split(final String value) {
323         return RegExUtils.VERSION_SPLIT_PATTERN.split(value);
324     }
325 
326     /**
327      * Parses a float value from a String.
328      *
329      * @param value the String to parse.
330      * @return the float value represented by the string or -1 if the given String cannot be parsed.
331      */
332     private static float toFloatVersion(final String value) {
333         final int defaultReturnValue = -1;
334         if (!value.contains(".")) {
335             return NumberUtils.toFloat(value, defaultReturnValue);
336         }
337         final String[] toParse = split(value);
338         if (toParse.length >= 2) {
339             return NumberUtils.toFloat(toParse[0] + '.' + toParse[1], defaultReturnValue);
340         }
341         return defaultReturnValue;
342     }
343 
344     /**
345      * The float value.
346      */
347     private final float value;
348 
349     /**
350      * The standard name.
351      */
352     private final String name;
353 
354     /**
355      * Constructs a new instance.
356      *
357      * @param value the float value.
358      * @param name  the standard name, not null.
359      */
360     JavaVersion(final float value, final String name) {
361         this.value = value;
362         this.name = name;
363     }
364 
365     /**
366      * Tests whether this version of Java is at least the version of Java passed in.
367      *
368      * <p>
369      * For example:
370      * </p>
371      *
372      * <pre>
373      *  {@code
374      * myVersion.atLeast(JavaVersion.JAVA_1_8)
375      * }</pre>
376      *
377      * @param requiredVersion the version to check against, not null.
378      * @return true if this version is equal to or greater than the specified version.
379      */
380     public boolean atLeast(final JavaVersion requiredVersion) {
381         return this.value >= requiredVersion.value;
382     }
383 
384     /**
385      * Tests whether this version of Java is at most the version of Java passed in.
386      *
387      * <p>
388      * For example:
389      * </p>
390      *
391      * <pre>
392      *  {@code
393      * myVersion.atMost(JavaVersion.JAVA_1_4)
394      * }</pre>
395      *
396      * @param requiredVersion the version to check against, not null.
397      * @return true if this version is equal to or greater than the specified version.
398      * @since 3.9
399      */
400     public boolean atMost(final JavaVersion requiredVersion) {
401         return this.value <= requiredVersion.value;
402     }
403 
404     /**
405      * The string value is overridden to return the standard name.
406      *
407      * <p>
408      * For example, {@code "1.5"}.
409      * </p>
410      *
411      * @return the name, not null.
412      */
413     @Override
414     public String toString() {
415         return name;
416     }
417 }