diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 63a94a318..092d4c591 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -57,6 +57,7 @@ The type attribute can be add,update,fix,remove. ArrayUtils#add confusing example in javadoc StringUtils#isAnyEmpty and #isAnyBlank should return false for an empty array Add StringUtils#unwrap + JavaVersion class depends on Apache Commons Math class NumberUtils Add support for recursive comparison to EqualsBuilder#reflectionEquals Implementation of a Memomizer Add ArrayUtils#toStringArray method diff --git a/src/main/java/org/apache/commons/lang3/JavaVersion.java b/src/main/java/org/apache/commons/lang3/JavaVersion.java index 8c992f2ae..964ec4a9a 100644 --- a/src/main/java/org/apache/commons/lang3/JavaVersion.java +++ b/src/main/java/org/apache/commons/lang3/JavaVersion.java @@ -16,8 +16,6 @@ */ package org.apache.commons.lang3; -import org.apache.commons.lang3.math.NumberUtils; - /** *

An enum representing all the versions of the Java specification. * This is intended to mirror available values from the @@ -220,11 +218,45 @@ public enum JavaVersion { if (value.contains(".")) { final String[] toParse = value.split("\\."); if (toParse.length >= 2) { - return NumberUtils.toFloat(toParse[0] + '.' + toParse[1], defaultReturnValue); + return toFloat(toParse[0] + '.' + toParse[1], defaultReturnValue); } } else { - return NumberUtils.toFloat(value, defaultReturnValue); + return toFloat(value, defaultReturnValue); } return defaultReturnValue; } + + /** + *

Convert a String to a float, returning a + * default value if the conversion fails.

+ * + *

If the string str is null, the default + * value is returned.

+ * + *
+     *   NumberUtils.toFloat(null, 1.1f)   = 1.0f
+     *   NumberUtils.toFloat("", 1.1f)     = 1.1f
+     *   NumberUtils.toFloat("1.5", 0.0f)  = 1.5f
+     * 
+ * + * @param str the string to convert, may be null + * @param defaultValue the default value + * @return the float represented by the string, or defaultValue + * if conversion fails + * + *

+ * Copied from Apache Commons Math. + *

+ */ + private static float toFloat(final String str, final float defaultValue) { + if (str == null) { + return defaultValue; + } + try { + return Float.parseFloat(str); + } catch (final NumberFormatException nfe) { + return defaultValue; + } + } + }