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    JAVA_1_9(9.0f, "9"),
081
082    /**
083     * Java 9
084     */
085    JAVA_9(9.0f, "9"),
086
087    /**
088     * The most recent java version. Mainly introduced to avoid to break when a new version of Java is used.
089     */
090    JAVA_RECENT(maxVersion(), Float.toString(maxVersion()));
091
092    /**
093     * The float value.
094     */
095    private final float value;
096    /**
097     * The standard name.
098     */
099    private final String name;
100
101    /**
102     * Constructor.
103     *
104     * @param value  the float value
105     * @param name  the standard name, not null
106     */
107    JavaVersion(final float value, final String name) {
108        this.value = value;
109        this.name = name;
110    }
111
112    //-----------------------------------------------------------------------
113    /**
114     * <p>Whether this version of Java is at least the version of Java passed in.</p>
115     *
116     * <p>For example:<br>
117     *  {@code myVersion.atLeast(JavaVersion.JAVA_1_4)}<p>
118     *
119     * @param requiredVersion  the version to check against, not null
120     * @return true if this version is equal to or greater than the specified version
121     */
122    public boolean atLeast(final JavaVersion requiredVersion) {
123        return this.value >= requiredVersion.value;
124    }
125
126    /**
127     * Transforms the given string with a Java version number to the
128     * corresponding constant of this enumeration class. This method is used
129     * internally.
130     *
131     * @param nom the Java version as string
132     * @return the corresponding enumeration constant or <b>null</b> if the
133     * version is unknown
134     */
135    // helper for static importing
136    static JavaVersion getJavaVersion(final String nom) {
137        return get(nom);
138    }
139
140    /**
141     * Transforms the given string with a Java version number to the
142     * corresponding constant of this enumeration class. This method is used
143     * internally.
144     *
145     * @param nom the Java version as string
146     * @return the corresponding enumeration constant or <b>null</b> if the
147     * version is unknown
148     */
149    static JavaVersion get(final String nom) {
150        if ("0.9".equals(nom)) {
151            return JAVA_0_9;
152        } else if ("1.1".equals(nom)) {
153            return JAVA_1_1;
154        } else if ("1.2".equals(nom)) {
155            return JAVA_1_2;
156        } else if ("1.3".equals(nom)) {
157            return JAVA_1_3;
158        } else if ("1.4".equals(nom)) {
159            return JAVA_1_4;
160        } else if ("1.5".equals(nom)) {
161            return JAVA_1_5;
162        } else if ("1.6".equals(nom)) {
163            return JAVA_1_6;
164        } else if ("1.7".equals(nom)) {
165            return JAVA_1_7;
166        } else if ("1.8".equals(nom)) {
167            return JAVA_1_8;
168        } else if ("9".equals(nom)) {
169            return JAVA_9;
170        }
171        if (nom == null) {
172            return null;
173        }
174        final float v = toFloatVersion(nom);
175        if ((v - 1.) < 1.) { // then we need to check decimals > .9
176            final int firstComma = Math.max(nom.indexOf('.'), nom.indexOf(','));
177            final int end = Math.max(nom.length(), nom.indexOf(',', firstComma));
178            if (Float.parseFloat(nom.substring(firstComma + 1, end)) > .9f) {
179                return JAVA_RECENT;
180            }
181        }
182        return null;
183    }
184
185    //-----------------------------------------------------------------------
186    /**
187     * <p>The string value is overridden to return the standard name.</p>
188     *
189     * <p>For example, <code>"1.5"</code>.</p>
190     *
191     * @return the name, not null
192     */
193    @Override
194    public String toString() {
195        return name;
196    }
197
198    /**
199     * Gets the Java Version from the system or 99.0 if the {@code java.specification.version} system property is not set.
200     * 
201     * @return the value of {@code java.specification.version} system property or 99.0 if it is not set.
202     */
203    private static float maxVersion() {
204        final float v = toFloatVersion(System.getProperty("java.specification.version", "99.0"));
205        if (v > 0) {
206            return v;
207        }
208        return 99f;
209    }
210
211    /**
212     * Parses a float value from a String.
213     * 
214     * @param value the String to parse.
215     * @return the float value represented by the string or -1 if the given String can not be parsed.
216     */
217    private static float toFloatVersion(final String value) {
218        final int defaultReturnValue = -1;
219        if (value.contains(".")) {
220            final String[] toParse = value.split("\\.");
221            if (toParse.length >= 2) {
222                return NumberUtils.toFloat(toParse[0] + '.' + toParse[1], defaultReturnValue);
223            }
224        } else {
225            return NumberUtils.toFloat(value, defaultReturnValue);
226        }
227        return defaultReturnValue;
228    }
229}