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