Coverage Report - org.apache.commons.lang3.JavaVersion
 
Classes in this File Line Coverage Branch Coverage Complexity
JavaVersion
91%
52/57
84%
27/32
5,286
 
 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 1654140 2015-01-23 08:41:18Z britter $
 26  
  */
 27  1
 public enum JavaVersion {
 28  
     
 29  
     /**
 30  
      * The Java version reported by Android. This is not an official Java version number.
 31  
      */
 32  1
     JAVA_0_9(1.5f, "0.9"),
 33  
     
 34  
     /**
 35  
      * Java 1.1.
 36  
      */
 37  1
     JAVA_1_1(1.1f, "1.1"),
 38  
 
 39  
     /**
 40  
      * Java 1.2.
 41  
      */
 42  1
     JAVA_1_2(1.2f, "1.2"),
 43  
 
 44  
     /**
 45  
      * Java 1.3.
 46  
      */
 47  1
     JAVA_1_3(1.3f, "1.3"),
 48  
 
 49  
     /**
 50  
      * Java 1.4.
 51  
      */
 52  1
     JAVA_1_4(1.4f, "1.4"),
 53  
 
 54  
     /**
 55  
      * Java 1.5.
 56  
      */
 57  1
     JAVA_1_5(1.5f, "1.5"),
 58  
 
 59  
     /**
 60  
      * Java 1.6.
 61  
      */
 62  1
     JAVA_1_6(1.6f, "1.6"),
 63  
 
 64  
     /**
 65  
      * Java 1.7.
 66  
      */
 67  1
     JAVA_1_7(1.7f, "1.7"),
 68  
 
 69  
     /**
 70  
      * Java 1.8.
 71  
      */
 72  1
     JAVA_1_8(1.8f, "1.8"),
 73  
 
 74  
     /**
 75  
      * Java 1.9.
 76  
      */
 77  1
     JAVA_1_9(1.9f, "1.9"),
 78  
 
 79  
     /**
 80  
      * Java 1.x, x &gt; 9. Mainly introduced to avoid to break when a new version of Java is used.
 81  
      */
 82  1
     JAVA_RECENT(maxVersion(), Float.toString(maxVersion()));
 83  
 
 84  
     /**
 85  
      * The float value.
 86  
      */
 87  
     private final float value;
 88  
     /**
 89  
      * The standard name.
 90  
      */
 91  
     private final String name;
 92  
 
 93  
     /**
 94  
      * Constructor.
 95  
      *
 96  
      * @param value  the float value
 97  
      * @param name  the standard name, not null
 98  
      */
 99  11
     JavaVersion(final float value, final String name) {
 100  11
         this.value = value;
 101  11
         this.name = name;
 102  11
     }
 103  
 
 104  
     //-----------------------------------------------------------------------
 105  
     /**
 106  
      * <p>Whether this version of Java is at least the version of Java passed in.</p>
 107  
      *
 108  
      * <p>For example:<br>
 109  
      *  {@code myVersion.atLeast(JavaVersion.JAVA_1_4)}<p>
 110  
      *
 111  
      * @param requiredVersion  the version to check against, not null
 112  
      * @return true if this version is equal to or greater than the specified version
 113  
      */
 114  
     public boolean atLeast(final JavaVersion requiredVersion) {
 115  4889
         return this.value >= requiredVersion.value;
 116  
     }
 117  
 
 118  
     /**
 119  
      * Transforms the given string with a Java version number to the
 120  
      * corresponding constant of this enumeration class. This method is used
 121  
      * internally.
 122  
      *
 123  
      * @param nom the Java version as string
 124  
      * @return the corresponding enumeration constant or <b>null</b> if the
 125  
      * version is unknown
 126  
      */
 127  
     // helper for static importing
 128  
     static JavaVersion getJavaVersion(final String nom) {
 129  1
         return get(nom);
 130  
     }
 131  
 
 132  
     /**
 133  
      * Transforms the given string with a Java version number to the
 134  
      * corresponding constant of this enumeration class. This method is used
 135  
      * internally.
 136  
      *
 137  
      * @param nom the Java version as string
 138  
      * @return the corresponding enumeration constant or <b>null</b> if the
 139  
      * version is unknown
 140  
      */
 141  
     static JavaVersion get(final String nom) {
 142  14
         if ("0.9".equals(nom)) {
 143  1
             return JAVA_0_9;
 144  13
         } else if ("1.1".equals(nom)) {
 145  1
             return JAVA_1_1;
 146  12
         } else if ("1.2".equals(nom)) {
 147  1
             return JAVA_1_2;
 148  11
         } else if ("1.3".equals(nom)) {
 149  1
             return JAVA_1_3;
 150  10
         } else if ("1.4".equals(nom)) {
 151  1
             return JAVA_1_4;
 152  9
         } else if ("1.5".equals(nom)) {
 153  3
             return JAVA_1_5;
 154  6
         } else if ("1.6".equals(nom)) {
 155  1
             return JAVA_1_6;
 156  5
         } else if ("1.7".equals(nom)) {
 157  2
             return JAVA_1_7;
 158  3
         } else if ("1.8".equals(nom)) {
 159  1
             return JAVA_1_8;
 160  2
         } else if ("1.9".equals(nom)) {
 161  1
             return JAVA_1_9;
 162  
         }
 163  1
         if (nom == null) {
 164  0
             return null;
 165  
         }
 166  1
         final float v = toFloatVersion(nom);
 167  1
         if ((v - 1.) < 1.) { // then we need to check decimals > .9
 168  1
             final int firstComma = Math.max(nom.indexOf('.'), nom.indexOf(','));
 169  1
             final int end = Math.max(nom.length(), nom.indexOf(',', firstComma));
 170  1
             if (Float.parseFloat(nom.substring(firstComma + 1, end)) > .9f) {
 171  1
                 return JAVA_RECENT;
 172  
             }
 173  
         }
 174  0
         return null;
 175  
     }
 176  
 
 177  
     //-----------------------------------------------------------------------
 178  
     /**
 179  
      * <p>The string value is overridden to return the standard name.</p>
 180  
      *
 181  
      * <p>For example, <code>"1.5"</code>.</p>
 182  
      *
 183  
      * @return the name, not null
 184  
      */
 185  
     @Override
 186  
     public String toString() {
 187  1
         return name;
 188  
     }
 189  
 
 190  
     /**
 191  
      * Gets the Java Version from the system or 2.0 if the {@code java.version} system property is not set.
 192  
      * 
 193  
      * @return the value of {@code java.version} system property or 2.0 if it is not set.
 194  
      */
 195  
     private static float maxVersion() {
 196  2
         final float v = toFloatVersion(System.getProperty("java.version", "2.0"));
 197  2
         if (v > 0) {
 198  2
             return v;
 199  
         }
 200  0
         return 2f;
 201  
     }
 202  
 
 203  
     /**
 204  
      * Parses a float value from a String.
 205  
      * 
 206  
      * @param value the String to parse.
 207  
      * @return the float value represented by teh string or -1 if the given String can not be parsed.
 208  
      */
 209  
     private static float toFloatVersion(final String value) {
 210  3
         final String[] toParse = value.split("\\.");
 211  3
         if (toParse.length >= 2) {
 212  
             try {
 213  3
                 return Float.parseFloat(toParse[0] + '.' + toParse[1]);
 214  0
             } catch (final NumberFormatException nfe) {
 215  
                 // no-op, let use default
 216  
             }
 217  
         }
 218  0
         return -1;
 219  
     }
 220  
 }