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 */
017 package 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 */
027 public enum JavaVersion {
028 /**
029 * The Java version reported by Android. This is not an official Java version number.
030 */
031 JAVA_0_9(1.5f, "0.9"),
032 JAVA_1_1(1.1f, "1.1"),
033 JAVA_1_2(1.2f, "1.2"),
034 JAVA_1_3(1.3f, "1.3"),
035 JAVA_1_4(1.4f, "1.4"),
036 JAVA_1_5(1.5f, "1.5"),
037 JAVA_1_6(1.6f, "1.6"),
038 JAVA_1_7(1.7f, "1.7"),
039 JAVA_1_8(1.8f, "1.8");
040
041 /**
042 * The float value.
043 */
044 private float value;
045 /**
046 * The standard name.
047 */
048 private String name;
049
050 /**
051 * Constructor.
052 *
053 * @param value the float value
054 * @param name the standard name, not null
055 */
056 JavaVersion(final float value, final String name) {
057 this.value = value;
058 this.name = name;
059 }
060
061 //-----------------------------------------------------------------------
062 /**
063 * <p>Whether this version of Java is at least the version of Java passed in.</p>
064 *
065 * <p>For example:<br />
066 * {@code myVersion.atLeast(JavaVersion.JAVA_1_4)}<p>
067 *
068 * @param requiredVersion the version to check against, not null
069 * @return true if this version is equal to or greater than the specified version
070 */
071 public boolean atLeast(JavaVersion requiredVersion) {
072 return this.value >= requiredVersion.value;
073 }
074
075 /**
076 * Transforms the given string with a Java version number to the
077 * corresponding constant of this enumeration class. This method is used
078 * internally.
079 *
080 * @param nom the Java version as string
081 * @return the corresponding enumeration constant or <b>null</b> if the
082 * version is unknown
083 */
084 // helper for static importing
085 static JavaVersion getJavaVersion(final String nom) {
086 return get(nom);
087 }
088
089 /**
090 * Transforms the given string with a Java version number to the
091 * corresponding constant of this enumeration class. This method is used
092 * internally.
093 *
094 * @param nom the Java version as string
095 * @return the corresponding enumeration constant or <b>null</b> if the
096 * version is unknown
097 */
098 static JavaVersion get(final String nom) {
099 if ("0.9".equals(nom)) {
100 return JAVA_0_9;
101 } else if ("1.1".equals(nom)) {
102 return JAVA_1_1;
103 } else if ("1.2".equals(nom)) {
104 return JAVA_1_2;
105 } else if ("1.3".equals(nom)) {
106 return JAVA_1_3;
107 } else if ("1.4".equals(nom)) {
108 return JAVA_1_4;
109 } else if ("1.5".equals(nom)) {
110 return JAVA_1_5;
111 } else if ("1.6".equals(nom)) {
112 return JAVA_1_6;
113 } else if ("1.7".equals(nom)) {
114 return JAVA_1_7;
115 } else if ("1.8".equals(nom)) {
116 return JAVA_1_8;
117 } else {
118 return null;
119 }
120 }
121
122 //-----------------------------------------------------------------------
123 /**
124 * <p>The string value is overridden to return the standard name.</p>
125 *
126 * <p>For example, <code>"1.5"</code>.</p>
127 *
128 * @return the name, not null
129 */
130 @Override
131 public String toString() {
132 return name;
133 }
134
135 }