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 * https://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 * Enumerates all known versions of the Java specification. This is intended to mirror available values from the <em>java.specification.version</em> System 023 * property. 024 * 025 * @since 3.0 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 * Java 1.9. 076 * 077 * @deprecated As of release 3.5, replaced by {@link #JAVA_9}. 078 */ 079 @Deprecated 080 JAVA_1_9(9.0f, "9"), 081 082 /** 083 * Java 9. 084 * 085 * @since 3.5 086 */ 087 JAVA_9(9.0f, "9"), 088 089 /** 090 * Java 10. 091 * 092 * @since 3.7 093 */ 094 JAVA_10(10.0f, "10"), 095 096 /** 097 * Java 11. 098 * 099 * @since 3.8 100 */ 101 JAVA_11(11.0f, "11"), 102 103 /** 104 * Java 12. 105 * 106 * @since 3.9 107 */ 108 JAVA_12(12.0f, "12"), 109 110 /** 111 * Java 13. 112 * 113 * @since 3.9 114 */ 115 JAVA_13(13.0f, "13"), 116 117 /** 118 * Java 14. 119 * 120 * @since 3.11 121 */ 122 JAVA_14(14.0f, "14"), 123 124 /** 125 * Java 15. 126 * 127 * @since 3.11 128 */ 129 JAVA_15(15.0f, "15"), 130 131 /** 132 * Java 16. 133 * 134 * @since 3.11 135 */ 136 JAVA_16(16.0f, "16"), 137 138 /** 139 * Java 17. 140 * 141 * @since 3.12.0 142 */ 143 JAVA_17(17.0f, "17"), 144 145 /** 146 * Java 18. 147 * 148 * @since 3.13.0 149 */ 150 JAVA_18(18.0f, "18"), 151 152 /** 153 * Java 19. 154 * 155 * @since 3.13.0 156 */ 157 JAVA_19(19.0f, "19"), 158 159 /** 160 * Java 20. 161 * 162 * @since 3.13.0 163 */ 164 JAVA_20(20, "20"), 165 166 /** 167 * Java 21. 168 * 169 * @since 3.13.0 170 */ 171 JAVA_21(21, "21"), 172 173 /** 174 * Java 22. 175 * 176 * @since 3.15.0 177 */ 178 JAVA_22(22, "22"), 179 180 /** 181 * Java 23. 182 * 183 * @since 3.18.0 184 */ 185 JAVA_23(23, "23"), 186 187 /** 188 * Java 24. 189 * 190 * @since 3.18.0 191 */ 192 JAVA_24(24, "24"), 193 194 /** 195 * Java 25. 196 * 197 * @since 3.20.0 198 */ 199 JAVA_25(25, "25"), 200 201 /** 202 * The most recent Java version. Mainly introduced to avoid to break when a new version of Java is used. 203 */ 204 JAVA_RECENT(maxVersion(), Float.toString(maxVersion())); 205 206 /** 207 * Transforms the given string with a Java version number to the corresponding constant of this enumeration class. This method is used internally. 208 * 209 * @param versionStr the Java version as string 210 * @return the corresponding enumeration constant or <strong>null</strong> if the version is unknown 211 */ 212 static JavaVersion get(final String versionStr) { 213 if (versionStr == null) { 214 return null; 215 } 216 switch (versionStr) { 217 case "0.9": 218 return JAVA_0_9; 219 case "1.1": 220 return JAVA_1_1; 221 case "1.2": 222 return JAVA_1_2; 223 case "1.3": 224 return JAVA_1_3; 225 case "1.4": 226 return JAVA_1_4; 227 case "1.5": 228 return JAVA_1_5; 229 case "1.6": 230 return JAVA_1_6; 231 case "1.7": 232 return JAVA_1_7; 233 case "1.8": 234 return JAVA_1_8; 235 case "9": 236 return JAVA_9; 237 case "10": 238 return JAVA_10; 239 case "11": 240 return JAVA_11; 241 case "12": 242 return JAVA_12; 243 case "13": 244 return JAVA_13; 245 case "14": 246 return JAVA_14; 247 case "15": 248 return JAVA_15; 249 case "16": 250 return JAVA_16; 251 case "17": 252 return JAVA_17; 253 case "18": 254 return JAVA_18; 255 case "19": 256 return JAVA_19; 257 case "20": 258 return JAVA_20; 259 case "21": 260 return JAVA_21; 261 case "22": 262 return JAVA_22; 263 case "23": 264 return JAVA_23; 265 case "24": 266 return JAVA_24; 267 case "25": 268 return JAVA_25; 269 default: 270 final float v = toFloatVersion(versionStr); 271 if (v - 1. < 1.) { // then we need to check decimals > .9 272 final int firstComma = Math.max(versionStr.indexOf('.'), versionStr.indexOf(',')); 273 final int end = Math.max(versionStr.length(), versionStr.indexOf(',', firstComma)); 274 if (Float.parseFloat(versionStr.substring(firstComma + 1, end)) > .9f) { 275 return JAVA_RECENT; 276 } 277 } else if (v > 10) { 278 return JAVA_RECENT; 279 } 280 return null; 281 } 282 } 283 284 /** 285 * Transforms the given string with a Java version number to the corresponding constant of this enumeration class. This method is used internally. 286 * 287 * @param versionStr the Java version as string 288 * @return the corresponding enumeration constant or <strong>null</strong> if the version is unknown 289 */ 290 static JavaVersion getJavaVersion(final String versionStr) { 291 return get(versionStr); 292 } 293 294 /** 295 * Gets the Java Version from the system or 99.0 if the {@code java.specification.version} system property is not set. 296 * 297 * @return the value of {@code java.specification.version} system property or 99.0 if it is not set. 298 */ 299 private static float maxVersion() { 300 final float v = toFloatVersion(SystemProperties.getJavaSpecificationVersion("99.0")); 301 return v > 0 ? v : 99f; 302 } 303 304 static String[] split(final String value) { 305 return RegExUtils.VERSION_SPLIT_PATTERN.split(value); 306 } 307 308 /** 309 * Parses a float value from a String. 310 * 311 * @param value the String to parse. 312 * @return the float value represented by the string or -1 if the given String cannot be parsed. 313 */ 314 private static float toFloatVersion(final String value) { 315 final int defaultReturnValue = -1; 316 if (!value.contains(".")) { 317 return NumberUtils.toFloat(value, defaultReturnValue); 318 } 319 final String[] toParse = split(value); 320 if (toParse.length >= 2) { 321 return NumberUtils.toFloat(toParse[0] + '.' + toParse[1], defaultReturnValue); 322 } 323 return defaultReturnValue; 324 } 325 326 /** 327 * The float value. 328 */ 329 private final float value; 330 331 /** 332 * The standard name. 333 */ 334 private final String name; 335 336 /** 337 * Constructs a new instance. 338 * 339 * @param value the float value 340 * @param name the standard name, not null 341 */ 342 JavaVersion(final float value, final String name) { 343 this.value = value; 344 this.name = name; 345 } 346 347 /** 348 * Tests whether this version of Java is at least the version of Java passed in. 349 * 350 * <p> 351 * For example: 352 * </p> 353 * 354 * <pre> 355 * {@code 356 * myVersion.atLeast(JavaVersion.JAVA_1_8) 357 * }</pre> 358 * 359 * @param requiredVersion the version to check against, not null 360 * @return true if this version is equal to or greater than the specified version 361 */ 362 public boolean atLeast(final JavaVersion requiredVersion) { 363 return this.value >= requiredVersion.value; 364 } 365 366 /** 367 * Tests whether this version of Java is at most the version of Java passed in. 368 * 369 * <p> 370 * For example: 371 * </p> 372 * 373 * <pre> 374 * {@code 375 * myVersion.atMost(JavaVersion.JAVA_1_4) 376 * }</pre> 377 * 378 * @param requiredVersion the version to check against, not null 379 * @return true if this version is equal to or greater than the specified version 380 * @since 3.9 381 */ 382 public boolean atMost(final JavaVersion requiredVersion) { 383 return this.value <= requiredVersion.value; 384 } 385 386 /** 387 * The string value is overridden to return the standard name. 388 * 389 * <p> 390 * For example, {@code "1.5"}. 391 * </p> 392 * 393 * @return the name, not null 394 */ 395 @Override 396 public String toString() { 397 return name; 398 } 399}