001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3;
018
019import org.apache.commons.lang3.math.NumberUtils;
020
021/**
022 * <p>An enum representing all the versions of the Java specification.
023 * This is intended to mirror available values from the
024 * <em>java.specification.version</em> System property. </p>
025 *
026 * @since 3.0
027 */
028public enum JavaVersion {
029
030    /**
031     * The Java version reported by Android. This is not an official Java version number.
032     */
033    JAVA_0_9(1.5f, "0.9"),
034
035    /**
036     * Java 1.1.
037     */
038    JAVA_1_1(1.1f, "1.1"),
039
040    /**
041     * Java 1.2.
042     */
043    JAVA_1_2(1.2f, "1.2"),
044
045    /**
046     * Java 1.3.
047     */
048    JAVA_1_3(1.3f, "1.3"),
049
050    /**
051     * Java 1.4.
052     */
053    JAVA_1_4(1.4f, "1.4"),
054
055    /**
056     * Java 1.5.
057     */
058    JAVA_1_5(1.5f, "1.5"),
059
060    /**
061     * Java 1.6.
062     */
063    JAVA_1_6(1.6f, "1.6"),
064
065    /**
066     * Java 1.7.
067     */
068    JAVA_1_7(1.7f, "1.7"),
069
070    /**
071     * Java 1.8.
072     */
073    JAVA_1_8(1.8f, "1.8"),
074
075    /**
076     * Java 1.9.
077     *
078     * @deprecated As of release 3.5, replaced by {@link #JAVA_9}
079     */
080    @Deprecated
081    JAVA_1_9(9.0f, "9"),
082
083    /**
084     * Java 9
085     *
086     * @since 3.5
087     */
088    JAVA_9(9.0f, "9"),
089
090    /**
091     * Java 10
092     *
093     * @since 3.7
094     */
095    JAVA_10(10.0f, "10"),
096
097    /**
098     * Java 11
099     *
100     * @since 3.8
101     */
102    JAVA_11(11.0f, "11"),
103
104    /**
105     * Java 12
106     *
107     * @since 3.9
108     */
109    JAVA_12(12.0f, "12"),
110
111    /**
112     * Java 13
113     *
114     * @since 3.9
115     */
116    JAVA_13(13.0f, "13"),
117
118    /**
119     * Java 14
120     *
121     * @since 3.11
122     */
123    JAVA_14(14.0f, "14"),
124
125    /**
126     * Java 15
127     *
128     * @since 3.11
129     */
130    JAVA_15(15.0f, "15"),
131
132    /**
133     * Java 16
134     *
135     * @since 3.11
136     */
137    JAVA_16(16.0f, "16"),
138
139    /**
140     * The most recent java version. Mainly introduced to avoid to break when a new version of Java is used.
141     */
142    JAVA_RECENT(maxVersion(), Float.toString(maxVersion()));
143
144    /**
145     * The float value.
146     */
147    private final float value;
148
149    /**
150     * The standard name.
151     */
152    private final String name;
153
154    /**
155     * Constructor.
156     *
157     * @param value  the float value
158     * @param name  the standard name, not null
159     */
160    JavaVersion(final float value, final String name) {
161        this.value = value;
162        this.name = name;
163    }
164
165    //-----------------------------------------------------------------------
166    /**
167     * <p>Whether this version of Java is at least the version of Java passed in.</p>
168     *
169     * <p>For example:<br>
170     *  {@code myVersion.atLeast(JavaVersion.JAVA_1_4)}<p>
171     *
172     * @param requiredVersion  the version to check against, not null
173     * @return true if this version is equal to or greater than the specified version
174     */
175    public boolean atLeast(final JavaVersion requiredVersion) {
176        return this.value >= requiredVersion.value;
177    }
178
179    //-----------------------------------------------------------------------
180    /**
181     * <p>Whether this version of Java is at most the version of Java passed in.</p>
182     *
183     * <p>For example:<br>
184     *  {@code myVersion.atMost(JavaVersion.JAVA_1_4)}<p>
185     *
186     * @param requiredVersion  the version to check against, not null
187     * @return true if this version is equal to or greater than the specified version
188     * @since 3.9
189     */
190    public boolean atMost(final JavaVersion requiredVersion) {
191        return this.value <= requiredVersion.value;
192    }
193
194    /**
195     * Transforms the given string with a Java version number to the
196     * corresponding constant of this enumeration class. This method is used
197     * internally.
198     *
199     * @param nom the Java version as string
200     * @return the corresponding enumeration constant or <b>null</b> if the
201     * version is unknown
202     */
203    // helper for static importing
204    static JavaVersion getJavaVersion(final String nom) {
205        return get(nom);
206    }
207
208    /**
209     * Transforms the given string with a Java version number to the
210     * corresponding constant of this enumeration class. This method is used
211     * internally.
212     *
213     * @param nom the Java version as string
214     * @return the corresponding enumeration constant or <b>null</b> if the
215     * version is unknown
216     */
217    static JavaVersion get(final String nom) {
218        if (nom == null) {
219            return null;
220        } else if ("0.9".equals(nom)) {
221            return JAVA_0_9;
222        } else if ("1.1".equals(nom)) {
223            return JAVA_1_1;
224        } else if ("1.2".equals(nom)) {
225            return JAVA_1_2;
226        } else if ("1.3".equals(nom)) {
227            return JAVA_1_3;
228        } else if ("1.4".equals(nom)) {
229            return JAVA_1_4;
230        } else if ("1.5".equals(nom)) {
231            return JAVA_1_5;
232        } else if ("1.6".equals(nom)) {
233            return JAVA_1_6;
234        } else if ("1.7".equals(nom)) {
235            return JAVA_1_7;
236        } else if ("1.8".equals(nom)) {
237            return JAVA_1_8;
238        } else if ("9".equals(nom)) {
239            return JAVA_9;
240        } else if ("10".equals(nom)) {
241            return JAVA_10;
242        } else if ("11".equals(nom)) {
243            return JAVA_11;
244        } else if ("12".equals(nom)) {
245            return JAVA_12;
246        } else if ("13".equals(nom)) {
247            return JAVA_13;
248        } else if ("14".equals(nom)) {
249            return JAVA_14;
250        } else if ("15".equals(nom)) {
251            return JAVA_15;
252        } else if ("16".equals(nom)) {
253            return JAVA_16;
254        }
255        final float v = toFloatVersion(nom);
256        if ((v - 1.) < 1.) { // then we need to check decimals > .9
257            final int firstComma = Math.max(nom.indexOf('.'), nom.indexOf(','));
258            final int end = Math.max(nom.length(), nom.indexOf(',', firstComma));
259            if (Float.parseFloat(nom.substring(firstComma + 1, end)) > .9f) {
260                return JAVA_RECENT;
261            }
262        } else if (v > 10) {
263            return JAVA_RECENT;
264        }
265        return null;
266    }
267
268    //-----------------------------------------------------------------------
269    /**
270     * <p>The string value is overridden to return the standard name.</p>
271     *
272     * <p>For example, {@code "1.5"}.</p>
273     *
274     * @return the name, not null
275     */
276    @Override
277    public String toString() {
278        return name;
279    }
280
281    /**
282     * Gets the Java Version from the system or 99.0 if the {@code java.specification.version} system property is not set.
283     *
284     * @return the value of {@code java.specification.version} system property or 99.0 if it is not set.
285     */
286    private static float maxVersion() {
287        final float v = toFloatVersion(System.getProperty("java.specification.version", "99.0"));
288        if (v > 0) {
289            return v;
290        }
291        return 99f;
292    }
293
294    /**
295     * Parses a float value from a String.
296     *
297     * @param value the String to parse.
298     * @return the float value represented by the string or -1 if the given String can not be parsed.
299     */
300    private static float toFloatVersion(final String value) {
301        final int defaultReturnValue = -1;
302        if (value.contains(".")) {
303            final String[] toParse = value.split("\\.");
304            if (toParse.length >= 2) {
305                return NumberUtils.toFloat(toParse[0] + '.' + toParse[1], defaultReturnValue);
306            }
307        } else {
308            return NumberUtils.toFloat(value, defaultReturnValue);
309        }
310        return defaultReturnValue;
311    }
312}