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