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    *      http://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  /**
20   * <p>An enum representing all the versions of the Java specification.
21   * This is intended to mirror available values from the
22   * <em>java.specification.version</em> System property. </p>
23   *
24   * @since 3.0
25   * @version $Id: JavaVersion.java 1583482 2014-03-31 22:54:57Z niallp $
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       * The float value.
76       */
77      private final float value;
78      /**
79       * The standard name.
80       */
81      private final String name;
82  
83      /**
84       * Constructor.
85       *
86       * @param value  the float value
87       * @param name  the standard name, not null
88       */
89      JavaVersion(final float value, final String name) {
90          this.value = value;
91          this.name = name;
92      }
93  
94      //-----------------------------------------------------------------------
95      /**
96       * <p>Whether this version of Java is at least the version of Java passed in.</p>
97       *
98       * <p>For example:<br>
99       *  {@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 }