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
019/**
020 * <p>An enum representing all the versions of the Java specification.
021 * This is intended to mirror available values from the
022 * <em>java.specification.version</em> System property. </p>
023 *
024 * @since 3.0
025 * @version $Id: $
026 */
027public enum JavaVersion {
028    
029    /**
030     * The Java version reported by Android. This is not an official Java version number.
031     */
032    JAVA_0_9(1.5f, "0.9"),
033    
034    /**
035     * Java 1.1.
036     */
037    JAVA_1_1(1.1f, "1.1"),
038
039    /**
040     * Java 1.2.
041     */
042    JAVA_1_2(1.2f, "1.2"),
043
044    /**
045     * Java 1.3.
046     */
047    JAVA_1_3(1.3f, "1.3"),
048
049    /**
050     * Java 1.4.
051     */
052    JAVA_1_4(1.4f, "1.4"),
053
054    /**
055     * Java 1.5.
056     */
057    JAVA_1_5(1.5f, "1.5"),
058
059    /**
060     * Java 1.6.
061     */
062    JAVA_1_6(1.6f, "1.6"),
063
064    /**
065     * Java 1.7.
066     */
067    JAVA_1_7(1.7f, "1.7"),
068
069    /**
070     * Java 1.8.
071     */
072    JAVA_1_8(1.8f, "1.8");
073
074    /**
075     * The float value.
076     */
077    private float value;
078    /**
079     * The standard name.
080     */
081    private String name;
082
083    /**
084     * Constructor.
085     *
086     * @param value  the float value
087     * @param name  the standard name, not null
088     */
089    JavaVersion(final float value, final String name) {
090        this.value = value;
091        this.name = name;
092    }
093
094    //-----------------------------------------------------------------------
095    /**
096     * <p>Whether this version of Java is at least the version of Java passed in.</p>
097     *
098     * <p>For example:<br />
099     *  {@code myVersion.atLeast(JavaVersion.JAVA_1_4)}<p>
100     *
101     * @param requiredVersion  the version to check against, not null
102     * @return true if this version is equal to or greater than the specified version
103     */
104    public boolean atLeast(final JavaVersion requiredVersion) {
105        return this.value >= requiredVersion.value;
106    }
107
108    /**
109     * Transforms the given string with a Java version number to the
110     * corresponding constant of this enumeration class. This method is used
111     * internally.
112     *
113     * @param nom the Java version as string
114     * @return the corresponding enumeration constant or <b>null</b> if the
115     * version is unknown
116     */
117    // helper for static importing
118    static JavaVersion getJavaVersion(final String nom) {
119        return get(nom);
120    }
121
122    /**
123     * Transforms the given string with a Java version number to the
124     * corresponding constant of this enumeration class. This method is used
125     * internally.
126     *
127     * @param nom the Java version as string
128     * @return the corresponding enumeration constant or <b>null</b> if the
129     * version is unknown
130     */
131    static JavaVersion get(final String nom) {
132        if ("0.9".equals(nom)) {
133            return JAVA_0_9;
134        } else if ("1.1".equals(nom)) {
135            return JAVA_1_1;
136        } else if ("1.2".equals(nom)) {
137            return JAVA_1_2;
138        } else if ("1.3".equals(nom)) {
139            return JAVA_1_3;
140        } else if ("1.4".equals(nom)) {
141            return JAVA_1_4;
142        } else if ("1.5".equals(nom)) {
143            return JAVA_1_5;
144        } else if ("1.6".equals(nom)) {
145            return JAVA_1_6;
146        } else if ("1.7".equals(nom)) {
147            return JAVA_1_7;
148        } else if ("1.8".equals(nom)) {
149            return JAVA_1_8;
150        } else {
151            return null;
152        }
153    }
154
155    //-----------------------------------------------------------------------
156    /**
157     * <p>The string value is overridden to return the standard name.</p>
158     *
159     * <p>For example, <code>"1.5"</code>.</p>
160     *
161     * @return the name, not null
162     */
163    @Override
164    public String toString() {
165        return name;
166    }
167
168}