diff --git a/src/java/org/apache/commons/lang/BooleanUtils.java b/src/java/org/apache/commons/lang/BooleanUtils.java
index a80736932..a162cc5fe 100644
--- a/src/java/org/apache/commons/lang/BooleanUtils.java
+++ b/src/java/org/apache/commons/lang/BooleanUtils.java
@@ -53,13 +53,15 @@
*/
package org.apache.commons.lang;
+import org.apache.commons.lang.math.NumberUtils;
+
/**
*
BooleanUtils
contains utility methods for working for
* boolean and Boolean objects.
*
* @author Stephen Colebourne
* @since 2.0
- * @version $Id: BooleanUtils.java,v 1.4 2003/02/04 22:50:31 scolebourne Exp $
+ * @version $Id: BooleanUtils.java,v 1.5 2003/06/24 21:14:50 scolebourne Exp $
*/
public class BooleanUtils {
diff --git a/src/java/org/apache/commons/lang/NumberUtils.java b/src/java/org/apache/commons/lang/NumberUtils.java
index 047d08268..7887198ab 100644
--- a/src/java/org/apache/commons/lang/NumberUtils.java
+++ b/src/java/org/apache/commons/lang/NumberUtils.java
@@ -55,6 +55,7 @@ package org.apache.commons.lang;
import java.math.BigInteger;
import java.math.BigDecimal;
+
/**
* Provides extra functionality for Java Number classes.
*
@@ -65,47 +66,14 @@ import java.math.BigDecimal;
* @author Eric Pugh
* @author Phil Steitz
* @since 1.0
- * @version $Id: NumberUtils.java,v 1.10 2003/06/08 14:14:01 scolebourne Exp $
+ * @version $Id: NumberUtils.java,v 1.11 2003/06/24 21:14:50 scolebourne Exp $
+ *
+ * @deprecated Moved to org.apache.commons.lang.math.
+ * Class will be removed in Commons Lang 3.0.
*/
public final class NumberUtils {
+ // DEPRECATED CLASS !!!
- /** Reusable Long constant for zero. */
- public static final Long LONG_ZERO = new Long(0L);
- /** Reusable Long constant for one. */
- public static final Long LONG_ONE = new Long(1L);
- /** Reusable Long constant for minus one. */
- public static final Long LONG_MINUS_ONE = new Long(-1L);
- /** Reusable Integer constant for zero. */
- public static final Integer INTEGER_ZERO = new Integer(0);
- /** Reusable Integer constant for one. */
- public static final Integer INTEGER_ONE = new Integer(1);
- /** Reusable Integer constant for minus one. */
- public static final Integer INTEGER_MINUS_ONE = new Integer(-1);
- /** Reusable Short constant for zero. */
- public static final Short SHORT_ZERO = new Short((short) 0);
- /** Reusable Short constant for one. */
- public static final Short SHORT_ONE = new Short((short) 1);
- /** Reusable Short constant for minus one. */
- public static final Short SHORT_MINUS_ONE = new Short((short) -1);
- /** Reusable Byte constant for zero. */
- public static final Byte BYTE_ZERO = new Byte((byte) 0);
- /** Reusable Byte constant for one. */
- public static final Byte BYTE_ONE = new Byte((byte) 1);
- /** Reusable Byte constant for minus one. */
- public static final Byte BYTE_MINUS_ONE = new Byte((byte) -1);
- /** Reusable Double constant for zero. */
- public static final Double DOUBLE_ZERO = new Double(0.0d);
- /** Reusable Double constant for one. */
- public static final Double DOUBLE_ONE = new Double(1.0d);
- /** Reusable Double constant for minus one. */
- public static final Double DOUBLE_MINUS_ONE = new Double(-1.0d);
- /** Reusable Float constant for zero. */
- public static final Float FLOAT_ZERO = new Float(0.0f);
- /** Reusable Float constant for one. */
- public static final Float FLOAT_ONE = new Float(1.0f);
- /** Reusable Float constant for minus one. */
- public static final Float FLOAT_MINUS_ONE = new Float(-1.0f);
-
/**
* NumberUtils
instances should NOT be constructed in standard programming.
* Instead, the class should be used as NumberUtils.stringToInt("6");
.
diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java
index 535385c62..f659491db 100644
--- a/src/java/org/apache/commons/lang/StringUtils.java
+++ b/src/java/org/apache/commons/lang/StringUtils.java
@@ -56,6 +56,8 @@ package org.apache.commons.lang;
import java.util.Iterator;
import java.util.StringTokenizer;
+import org.apache.commons.lang.math.NumberUtils;
+
/**
* Common String
manipulation routines.
*
@@ -77,7 +79,7 @@ import java.util.StringTokenizer;
* @author Arun Mammen Thomas
* @author Gary Gregory
* @since 1.0
- * @version $Id: StringUtils.java,v 1.50 2003/06/24 08:15:15 scolebourne Exp $
+ * @version $Id: StringUtils.java,v 1.51 2003/06/24 21:14:50 scolebourne Exp $
*/
public class StringUtils {
@@ -2292,7 +2294,7 @@ public class StringUtils {
}
// Step 6
- d[i][j] = NumberUtils.minimum(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
+ d[i][j] = NumberUtils.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
}
}
diff --git a/src/java/org/apache/commons/lang/builder/CompareToBuilder.java b/src/java/org/apache/commons/lang/builder/CompareToBuilder.java
index e62b14720..5a2d7656d 100644
--- a/src/java/org/apache/commons/lang/builder/CompareToBuilder.java
+++ b/src/java/org/apache/commons/lang/builder/CompareToBuilder.java
@@ -57,7 +57,8 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Comparator;
-import org.apache.commons.lang.NumberUtils;
+import org.apache.commons.lang.math.NumberUtils;
+
/**
* CompareTo
generation routines.
*
@@ -105,7 +106,7 @@ import org.apache.commons.lang.NumberUtils;
* @author Stephen Colebourne
* @author Gary Gregory
* @since 1.0
- * @version $Id: CompareToBuilder.java,v 1.14 2003/04/18 09:12:16 ggregory Exp $
+ * @version $Id: CompareToBuilder.java,v 1.15 2003/06/24 21:14:50 scolebourne Exp $
*/
public class CompareToBuilder {
diff --git a/src/java/org/apache/commons/lang/math/NumberUtils.java b/src/java/org/apache/commons/lang/math/NumberUtils.java
new file mode 100644
index 000000000..aa879fbb7
--- /dev/null
+++ b/src/java/org/apache/commons/lang/math/NumberUtils.java
@@ -0,0 +1,899 @@
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowlegement may appear in the software itself,
+ * if and wherever such third-party acknowlegements normally appear.
+ *
+ * 4. The names "The Jakarta Project", "Commons", and "Apache Software
+ * Foundation" must not be used to endorse or promote products derived
+ * from this software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ */
+package org.apache.commons.lang.math;
+
+import java.math.BigInteger;
+import java.math.BigDecimal;
+
+/**
+ * Provides extra functionality for Java Number classes.
+ *
+ * @author Henri Yandell
+ * @author Rand McNeely
+ * @author Stephen Colebourne
+ * @author Steve Downey
+ * @author Eric Pugh
+ * @author Phil Steitz
+ * @since 1.0
+ * @version $Id: NumberUtils.java,v 1.1 2003/06/24 21:14:50 scolebourne Exp $
+ */
+public final class NumberUtils {
+
+ /** Reusable Long constant for zero. */
+ public static final Long LONG_ZERO = new Long(0L);
+ /** Reusable Long constant for one. */
+ public static final Long LONG_ONE = new Long(1L);
+ /** Reusable Long constant for minus one. */
+ public static final Long LONG_MINUS_ONE = new Long(-1L);
+ /** Reusable Integer constant for zero. */
+ public static final Integer INTEGER_ZERO = new Integer(0);
+ /** Reusable Integer constant for one. */
+ public static final Integer INTEGER_ONE = new Integer(1);
+ /** Reusable Integer constant for minus one. */
+ public static final Integer INTEGER_MINUS_ONE = new Integer(-1);
+ /** Reusable Short constant for zero. */
+ public static final Short SHORT_ZERO = new Short((short) 0);
+ /** Reusable Short constant for one. */
+ public static final Short SHORT_ONE = new Short((short) 1);
+ /** Reusable Short constant for minus one. */
+ public static final Short SHORT_MINUS_ONE = new Short((short) -1);
+ /** Reusable Byte constant for zero. */
+ public static final Byte BYTE_ZERO = new Byte((byte) 0);
+ /** Reusable Byte constant for one. */
+ public static final Byte BYTE_ONE = new Byte((byte) 1);
+ /** Reusable Byte constant for minus one. */
+ public static final Byte BYTE_MINUS_ONE = new Byte((byte) -1);
+ /** Reusable Double constant for zero. */
+ public static final Double DOUBLE_ZERO = new Double(0.0d);
+ /** Reusable Double constant for one. */
+ public static final Double DOUBLE_ONE = new Double(1.0d);
+ /** Reusable Double constant for minus one. */
+ public static final Double DOUBLE_MINUS_ONE = new Double(-1.0d);
+ /** Reusable Float constant for zero. */
+ public static final Float FLOAT_ZERO = new Float(0.0f);
+ /** Reusable Float constant for one. */
+ public static final Float FLOAT_ONE = new Float(1.0f);
+ /** Reusable Float constant for minus one. */
+ public static final Float FLOAT_MINUS_ONE = new Float(-1.0f);
+
+ /**
+ * NumberUtils
instances should NOT be constructed in standard programming.
+ * Instead, the class should be used as NumberUtils.stringToInt("6");
.
+ *
+ * This constructor is public to permit tools that require a JavaBean instance
+ * to operate.
+ */
+ public NumberUtils() {
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Convert a String
to an int
, returning
+ * zero
if the conversion fails.
+ *
+ * @param str the string to convert
+ * @return the int represented by the string, or zero
if
+ * conversion fails
+ */
+ public static int stringToInt(String str) {
+ return stringToInt(str, 0);
+ }
+
+ /**
+ * Convert a String
to an int
, returning a
+ * default value if the conversion fails.
+ *
+ * @param str the string to convert
+ * @param defaultValue the default value
+ * @return the int represented by the string, or the default if conversion fails
+ */
+ public static int stringToInt(String str, int defaultValue) {
+ try {
+ return Integer.parseInt(str);
+ } catch (NumberFormatException nfe) {
+ return defaultValue;
+ }
+ }
+
+ //-----------------------------------------------------------------------
+ // must handle Long, Float, Integer, Float, Short,
+ // BigDecimal, BigInteger and Byte
+ // useful methods:
+ // Byte.decode(String)
+ // Byte.valueOf(String,int radix)
+ // Byte.valueOf(String)
+ // Double.valueOf(String)
+ // Float.valueOf(String)
+ // new Float(String)
+ // Integer.valueOf(String,int radix)
+ // Integer.valueOf(String)
+ // Integer.decode(String)
+ // Integer.getInteger(String)
+ // Integer.getInteger(String,int val)
+ // Integer.getInteger(String,Integer val)
+ // new Integer(String)
+ // new Double(String)
+ // new Byte(String)
+ // new Long(String)
+ // Long.getLong(String)
+ // Long.getLong(String,int)
+ // Long.getLong(String,Integer)
+ // Long.valueOf(String,int)
+ // Long.valueOf(String)
+ // new Short(String)
+ // Short.decode(String)
+ // Short.valueOf(String,int)
+ // Short.valueOf(String)
+ // new BigDecimal(String)
+ // new BigInteger(String)
+ // new BigInteger(String,int radix)
+ // Possible inputs:
+ // 45 45.5 45E7 4.5E7 Hex Oct Binary xxxF xxxD xxxf xxxd
+ // plus minus everything. Prolly more. A lot are not separable.
+
+ /**
+ * Turns a string value into a java.lang.Number.
+ *
+ * First, the value is examined for a type qualifier on the end
+ * ('f','F','d','D','l','L'
). If it is found, it starts
+ * trying to create succissively larger types from the type specified
+ * until one is found that can hold the value.
+ *
+ * If a type specifier is not found, it will check for a decimal point
+ * and then try successively larger types from Integer
to
+ * BigInteger
and from Float
to
+ * BigDecimal
.
+ *
+ * If the string starts with 0x
or -0x
, it
+ * will be interpreted as a hexadecimal integer. Values with leading
+ * 0
's will not be interpreted as octal.
+ *
+ * @param val String containing a number
+ * @return Number created from the string
+ * @throws NumberFormatException if the value cannot be converted
+ */
+ public static Number createNumber(String val) throws NumberFormatException {
+ if (val == null) {
+ return null;
+ }
+ if (val.length() == 0) {
+ throw new NumberFormatException("\"\" is not a valid number.");
+ }
+ if (val.startsWith("--")) {
+ // this is protection for poorness in java.lang.BigDecimal.
+ // it accepts this as a legal value, but it does not appear
+ // to be in specification of class. OS X Java parses it to
+ // a wrong value.
+ return null;
+ }
+ if (val.startsWith("0x") || val.startsWith("-0x")) {
+ return createInteger(val);
+ }
+ char lastChar = val.charAt(val.length() - 1);
+ String mant;
+ String dec;
+ String exp;
+ int decPos = val.indexOf('.');
+ int expPos = val.indexOf('e') + val.indexOf('E') + 1;
+
+ if (decPos > -1) {
+
+ if (expPos > -1) {
+ if (expPos < decPos) {
+ throw new NumberFormatException(val + " is not a valid number.");
+ }
+ dec = val.substring(decPos + 1, expPos);
+ } else {
+ dec = val.substring(decPos + 1);
+ }
+ mant = val.substring(0, decPos);
+ } else {
+ if (expPos > -1) {
+ mant = val.substring(0, expPos);
+ } else {
+ mant = val;
+ }
+ dec = null;
+ }
+ if (!Character.isDigit(lastChar)) {
+ if (expPos > -1 && expPos < val.length() - 1) {
+ exp = val.substring(expPos + 1, val.length() - 1);
+ } else {
+ exp = null;
+ }
+ //Requesting a specific type..
+ String numeric = val.substring(0, val.length() - 1);
+ boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
+ switch (lastChar) {
+ case 'l' :
+ case 'L' :
+ if (dec == null
+ && exp == null
+ && isDigits(numeric.substring(1))
+ && (numeric.charAt(0) == '-' || Character.isDigit(numeric.charAt(0)))) {
+ try {
+ return createLong(numeric);
+ } catch (NumberFormatException nfe) {
+ //Too big for a long
+ }
+ return createBigInteger(numeric);
+
+ }
+ throw new NumberFormatException(val + " is not a valid number.");
+ case 'f' :
+ case 'F' :
+ try {
+ Float f = NumberUtils.createFloat(numeric);
+ if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
+ //If it's too big for a float or the float value = 0 and the string
+ //has non-zeros in it, then float doens't have the presision we want
+ return f;
+ }
+
+ } catch (NumberFormatException nfe) {
+ }
+ //Fall through
+ case 'd' :
+ case 'D' :
+ try {
+ Double d = NumberUtils.createDouble(numeric);
+ if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) {
+ return d;
+ }
+ } catch (NumberFormatException nfe) {
+ }
+ try {
+ return createBigDecimal(numeric);
+ } catch (NumberFormatException e) {
+ }
+ //Fall through
+ default :
+ throw new NumberFormatException(val + " is not a valid number.");
+
+ }
+ } else {
+ //User doesn't have a preference on the return type, so let's start
+ //small and go from there...
+ if (expPos > -1 && expPos < val.length() - 1) {
+ exp = val.substring(expPos + 1, val.length());
+ } else {
+ exp = null;
+ }
+ if (dec == null && exp == null) {
+ //Must be an int,long,bigint
+ try {
+ return createInteger(val);
+ } catch (NumberFormatException nfe) {
+ }
+ try {
+ return createLong(val);
+ } catch (NumberFormatException nfe) {
+ }
+ return createBigInteger(val);
+
+ } else {
+ //Must be a float,double,BigDec
+ boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
+ try {
+ Float f = createFloat(val);
+ if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
+ return f;
+ }
+ } catch (NumberFormatException nfe) {
+ }
+ try {
+ Double d = createDouble(val);
+ if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
+ return d;
+ }
+ } catch (NumberFormatException nfe) {
+ }
+
+ return createBigDecimal(val);
+
+ }
+ }
+ }
+
+ /**
+ * Utility method for {@link #createNumber(java.lang.String)}.
+ *
+ * Returns true
if s is null
.
+ *
+ * @param s the String to check
+ * @return if it is all zeros or null
+ */
+ private static boolean isAllZeros(String s) {
+ if (s == null) {
+ return true;
+ }
+ for (int i = s.length() - 1; i >= 0; i--) {
+ if (s.charAt(i) != '0') {
+ return false;
+ }
+ }
+ return s.length() > 0;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Convert a String
to a Float
.
+ *
+ * @param val a String
to convert
+ * @return converted Float
+ * @throws NumberFormatException if the value cannot be converted
+ */
+ public static Float createFloat(String val) {
+ return Float.valueOf(val);
+ }
+
+ /**
+ * Convert a String
to a Double
.
+ *
+ * @param val a String
to convert
+ * @return converted Double
+ * @throws NumberFormatException if the value cannot be converted
+ */
+ public static Double createDouble(String val) {
+ return Double.valueOf(val);
+ }
+
+ /**
+ * Convert a String
to a Integer
, handling
+ * hex and octal notations.
+ *
+ * @param val a String
to convert
+ * @return converted Integer
+ * @throws NumberFormatException if the value cannot be converted
+ */
+ public static Integer createInteger(String val) {
+ // decode() handles 0xAABD and 0777 (hex and octal) as well.
+ return Integer.decode(val);
+ }
+
+ /**
+ * Convert a String
to a Long
.
+ *
+ * @param val a String
to convert
+ * @return converted Long
+ * @throws NumberFormatException if the value cannot be converted
+ */
+ public static Long createLong(String val) {
+ return Long.valueOf(val);
+ }
+
+ /**
+ * Convert a String
to a BigInteger
.
+ *
+ * @param val a String
to convert
+ * @return converted BigInteger
+ * @throws NumberFormatException if the value cannot be converted
+ */
+ public static BigInteger createBigInteger(String val) {
+ BigInteger bi = new BigInteger(val);
+ return bi;
+ }
+
+ /**
+ * Convert a String
to a BigDecimal
.
+ *
+ * @param val a String
to convert
+ * @return converted BigDecimal
+ * @throws NumberFormatException if the value cannot be converted
+ */
+ public static BigDecimal createBigDecimal(String val) {
+ BigDecimal bd = new BigDecimal(val);
+ return bd;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the minimum of three long
values.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the smallest of the values
+ */
+ public static long min(long a, long b, long c) {
+ if (b < a) {
+ a = b;
+ }
+ if (c < a) {
+ a = c;
+ }
+ return a;
+ }
+
+ /**
+ * Gets the minimum of three int
values.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the smallest of the values
+ */
+ public static int min(int a, int b, int c) {
+ if (b < a) {
+ a = b;
+ }
+ if (c < a) {
+ a = c;
+ }
+ return a;
+ }
+
+ /**
+ * Gets the minimum of three short
values.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the smallest of the values
+ */
+ public static short min(short a, short b, short c) {
+ if (b < a) {
+ a = b;
+ }
+ if (c < a) {
+ a = c;
+ }
+ return a;
+ }
+
+ /**
+ * Gets the minimum of three byte
values.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the smallest of the values
+ */
+ public static byte min(byte a, byte b, byte c) {
+ if (b < a) {
+ a = b;
+ }
+ if (c < a) {
+ a = c;
+ }
+ return a;
+ }
+
+ /**
+ * Gets the minimum of three double
values.
+ *
+ * If any value is NaN, NaN is returned. Infinity is handled.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the smallest of the values
+ */
+ public static double min(double a, double b, double c) {
+ return Math.min(Math.min(a, b), c);
+ }
+
+ /**
+ * Gets the minimum of three float
values.
+ *
+ * If any value is NaN, NaN is returned. Infinity is handled.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the smallest of the values
+ */
+ public static float min(float a, float b, float c) {
+ return Math.min(Math.min(a, b), c);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the maximum of three long
values.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the largest of the values
+ */
+ public static long max(long a, long b, long c) {
+ if (b > a) {
+ a = b;
+ }
+ if (c > a) {
+ a = c;
+ }
+ return a;
+ }
+
+ /**
+ * Gets the maximum of three int
values.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the largest of the values
+ */
+ public static int max(int a, int b, int c) {
+ if (b > a) {
+ a = b;
+ }
+ if (c > a) {
+ a = c;
+ }
+ return a;
+ }
+
+ /**
+ * Gets the maximum of three short
values.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the largest of the values
+ */
+ public static short max(short a, short b, short c) {
+ if (b > a) {
+ a = b;
+ }
+ if (c > a) {
+ a = c;
+ }
+ return a;
+ }
+
+ /**
+ * Gets the maximum of three byte
values.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the largest of the values
+ */
+ public static byte max(byte a, byte b, byte c) {
+ if (b > a) {
+ a = b;
+ }
+ if (c > a) {
+ a = c;
+ }
+ return a;
+ }
+
+ /**
+ * Gets the maximum of three double
values.
+ *
+ * If any value is NaN, NaN is returned. Infinity is handled.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the largest of the values
+ */
+ public static double max(double a, double b, double c) {
+ return Math.max(Math.max(a, b), c);
+ }
+
+ /**
+ * Gets the maximum of three float
values.
+ *
+ * If any value is NaN, NaN is returned. Infinity is handled.
+ *
+ * @param a value 1
+ * @param b value 2
+ * @param c value 3
+ * @return the largest of the values
+ */
+ public static float max(float a, float b, float c) {
+ return Math.max(Math.max(a, b), c);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares two doubles
for order.
+ *
+ * This method is more comprehensive than the standard Java greater
+ * than, less than and equals operators.
+ *
+ * - It returns
-1
if the first value is less than the second.
+ * - It returns
+1
if the first value is greater than the second.
+ * - It returns
0
if the values are equal.
+ *
+ *
+ *
+ * The ordering is as follows, largest to smallest:
+ *
+ * - NaN
+ *
- Positive infinity
+ *
- Maximum double
+ *
- Normal positve numbers
+ *
- +0.0
+ *
- -0.0
+ *
- Normal negative numbers
+ *
- Minimum double (-Double.MAX_VALUE)
+ *
- Negative infinity
+ *
+ *
+ *
+ * Comparing NaN
with NaN
will
+ * return 0
.
+ *
+ * @param lhs the first double
+ * @param rhs the second double
+ * @return -1
if lhs is less, +1
if greater,
+ * 0
if equal to rhs
+ */
+ public static int compare(double lhs, double rhs) {
+ if (lhs < rhs) {
+ return -1;
+ }
+ if (lhs > rhs) {
+ return +1;
+ }
+ // Need to compare bits to handle 0.0 == -0.0 being true
+ // compare should put -0.0 < +0.0
+ // Two NaNs are also == for compare purposes
+ // where NaN == NaN is false
+ long lhsBits = Double.doubleToLongBits(lhs);
+ long rhsBits = Double.doubleToLongBits(rhs);
+ if (lhsBits == rhsBits) {
+ return 0;
+ }
+ // Something exotic! A comparison to NaN or 0.0 vs -0.0
+ // Fortunately NaN's long is > than everything else
+ // Also negzeros bits < poszero
+ // NAN: 9221120237041090560
+ // MAX: 9218868437227405311
+ // NEGZERO: -9223372036854775808
+ if (lhsBits < rhsBits) {
+ return -1;
+ } else {
+ return +1;
+ }
+ }
+
+ /**
+ * Compares two floats for order.
+ *
+ * This method is more comprhensive than the standard Java greater than,
+ * less than and equals operators.
+ *
+ * - It returns
-1
if the first value is less than the second.
+ * - It returns
+1
if the first value is greater than the second.
+ * - It returns
0
if the values are equal.
+ *
+ *
+ * The ordering is as follows, largest to smallest:
+ *
+ * - NaN
+ *
- Positive infinity
+ *
- Maximum float
+ *
- Normal positve numbers
+ *
- +0.0
+ *
- -0.0
+ *
- Normal negative numbers
+ *
- Minimum float (-Float.MAX_VALUE)
+ *
- Negative infinity
+ *
+ *
+ * Comparing NaN
with NaN
will return
+ * 0
.
+ *
+ * @param lhs the first float
+ * @param rhs the second float
+ * @return -1
if lhs is less, +1
if greater,
+ * 0
if equal to rhs
+ */
+ public static int compare(float lhs, float rhs) {
+ if (lhs < rhs) {
+ return -1;
+ }
+ if (lhs > rhs) {
+ return +1;
+ }
+ //Need to compare bits to handle 0.0 == -0.0 being true
+ // compare should put -0.0 < +0.0
+ // Two NaNs are also == for compare purposes
+ // where NaN == NaN is false
+ int lhsBits = Float.floatToIntBits(lhs);
+ int rhsBits = Float.floatToIntBits(rhs);
+ if (lhsBits == rhsBits) {
+ return 0;
+ }
+ //Something exotic! A comparison to NaN or 0.0 vs -0.0
+ //Fortunately NaN's int is > than everything else
+ //Also negzeros bits < poszero
+ //NAN: 2143289344
+ //MAX: 2139095039
+ //NEGZERO: -2147483648
+ if (lhsBits < rhsBits) {
+ return -1;
+ } else {
+ return +1;
+ }
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Checks whether the String
contains only
+ * digit characters.
+ *
+ * Null
and empty String will return
+ * false
.
+ *
+ * @param str the String
to check
+ * @return true
if str contains only unicode numeric
+ */
+ public static boolean isDigits(String str) {
+ if ((str == null) || (str.length() == 0)) {
+ return false;
+ }
+ for (int i = 0; i < str.length(); i++) {
+ if (!Character.isDigit(str.charAt(i))) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Checks whether the String a valid Java number.
+ *
+ * Valid numbers include hexadecimal marked with the 0x
+ * qualifier, scientific notation and numbers marked with a type
+ * qualifier (e.g. 123L).
+ *
+ * Null
and empty String will return
+ * false
.
+ *
+ * @param str the String
to check
+ * @return true
if the string is a correctly formatted number
+ */
+ public static boolean isNumber(String str) {
+ if ((str == null) || (str.length() == 0)) {
+ return false;
+ }
+ char[] chars = str.toCharArray();
+ int sz = chars.length;
+ boolean hasExp = false;
+ boolean hasDecPoint = false;
+ boolean allowSigns = false;
+ boolean foundDigit = false;
+ // deal with any possible sign up front
+ int start = (chars[0] == '-') ? 1 : 0;
+ if (sz > start + 1) {
+ if (chars[start] == '0' && chars[start + 1] == 'x') {
+ int i = start + 2;
+ if (i == sz) {
+ return false; // str == "0x"
+ }
+ // checking hex (it can't be anything else)
+ for (; i < chars.length; i++) {
+ if ((chars[i] < '0' || chars[i] > '9')
+ && (chars[i] < 'a' || chars[i] > 'f')
+ && (chars[i] < 'A' || chars[i] > 'F')) {
+ return false;
+ }
+ }
+ return true;
+ }
+ }
+ sz--; // don't want to loop to the last char, check it afterwords
+ // for type qualifiers
+ int i = start;
+ // loop to the next to last char or to the last char if we need another digit to
+ // make a valid number (e.g. chars[0..5] = "1234E")
+ while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
+ if (chars[i] >= '0' && chars[i] <= '9') {
+ foundDigit = true;
+ allowSigns = false;
+
+ } else if (chars[i] == '.') {
+ if (hasDecPoint || hasExp) {
+ // two decimal points or dec in exponent
+ return false;
+ }
+ hasDecPoint = true;
+ } else if (chars[i] == 'e' || chars[i] == 'E') {
+ // we've already taken care of hex.
+ if (hasExp) {
+ // two E's
+ return false;
+ }
+ if (!foundDigit) {
+ return false;
+ }
+ hasExp = true;
+ allowSigns = true;
+ } else if (chars[i] == '+' || chars[i] == '-') {
+ if (!allowSigns) {
+ return false;
+ }
+ allowSigns = false;
+ foundDigit = false; // we need a digit after the E
+ } else {
+ return false;
+ }
+ i++;
+ }
+ if (i < chars.length) {
+ if (chars[i] >= '0' && chars[i] <= '9') {
+ // no type qualifier, OK
+ return true;
+ }
+ if (chars[i] == 'e' || chars[i] == 'E') {
+ // can't have an E at the last byte
+ return false;
+ }
+ if (!allowSigns
+ && (chars[i] == 'd'
+ || chars[i] == 'D'
+ || chars[i] == 'f'
+ || chars[i] == 'F')) {
+ return foundDigit;
+ }
+ if (chars[i] == 'l'
+ || chars[i] == 'L') {
+ // not allowing L with an exponoent
+ return foundDigit && !hasExp;
+ }
+ // last character is illegal
+ return false;
+ }
+ // allowSigns is true iff the val ends in 'E'
+ // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
+ return !allowSigns && foundDigit;
+ }
+
+}
diff --git a/src/java/org/apache/commons/lang/math/Range.java b/src/java/org/apache/commons/lang/math/Range.java
index 151758630..ea1e7226e 100644
--- a/src/java/org/apache/commons/lang/math/Range.java
+++ b/src/java/org/apache/commons/lang/math/Range.java
@@ -53,8 +53,6 @@
*/
package org.apache.commons.lang.math;
-import org.apache.commons.lang.NumberUtils;
-
/**
* Range
represents a range of numbers of the same type.
*
@@ -64,7 +62,7 @@ import org.apache.commons.lang.NumberUtils;
*
* @author Stephen Colebourne
* @since 2.0
- * @version $Id: Range.java,v 1.2 2003/03/23 17:51:15 scolebourne Exp $
+ * @version $Id: Range.java,v 1.3 2003/06/24 21:14:50 scolebourne Exp $
*/
public abstract class Range {
diff --git a/src/test/org/apache/commons/lang/NumberUtilsTest.java b/src/test/org/apache/commons/lang/NumberUtilsTest.java
index daf17aee3..8952fe9da 100644
--- a/src/test/org/apache/commons/lang/NumberUtilsTest.java
+++ b/src/test/org/apache/commons/lang/NumberUtilsTest.java
@@ -68,7 +68,7 @@ import junit.framework.TestSuite;
* @author Eric Pugh
* @author Phil Steitz
* @author Stephen Colebourne
- * @version $Id: NumberUtilsTest.java,v 1.7 2003/06/08 14:14:01 scolebourne Exp $
+ * @version $Id: NumberUtilsTest.java,v 1.8 2003/06/24 21:14:51 scolebourne Exp $
*/
public class NumberUtilsTest extends TestCase {
@@ -534,43 +534,4 @@ public class NumberUtilsTest extends TestCase {
}
}
- public void testConstants() {
- assertTrue(NumberUtils.LONG_ZERO instanceof Long);
- assertTrue(NumberUtils.LONG_ONE instanceof Long);
- assertTrue(NumberUtils.LONG_MINUS_ONE instanceof Long);
- assertTrue(NumberUtils.INTEGER_ZERO instanceof Integer);
- assertTrue(NumberUtils.INTEGER_ONE instanceof Integer);
- assertTrue(NumberUtils.INTEGER_MINUS_ONE instanceof Integer);
- assertTrue(NumberUtils.SHORT_ZERO instanceof Short);
- assertTrue(NumberUtils.SHORT_ONE instanceof Short);
- assertTrue(NumberUtils.SHORT_MINUS_ONE instanceof Short);
- assertTrue(NumberUtils.BYTE_ZERO instanceof Byte);
- assertTrue(NumberUtils.BYTE_ONE instanceof Byte);
- assertTrue(NumberUtils.BYTE_MINUS_ONE instanceof Byte);
- assertTrue(NumberUtils.DOUBLE_ZERO instanceof Double);
- assertTrue(NumberUtils.DOUBLE_ONE instanceof Double);
- assertTrue(NumberUtils.DOUBLE_MINUS_ONE instanceof Double);
- assertTrue(NumberUtils.FLOAT_ZERO instanceof Float);
- assertTrue(NumberUtils.FLOAT_ONE instanceof Float);
- assertTrue(NumberUtils.FLOAT_MINUS_ONE instanceof Float);
-
- assertTrue(NumberUtils.LONG_ZERO.longValue() == 0);
- assertTrue(NumberUtils.LONG_ONE.longValue() == 1);
- assertTrue(NumberUtils.LONG_MINUS_ONE.longValue() == -1);
- assertTrue(NumberUtils.INTEGER_ZERO.intValue() == 0);
- assertTrue(NumberUtils.INTEGER_ONE.intValue() == 1);
- assertTrue(NumberUtils.INTEGER_MINUS_ONE.intValue() == -1);
- assertTrue(NumberUtils.SHORT_ZERO.shortValue() == 0);
- assertTrue(NumberUtils.SHORT_ONE.shortValue() == 1);
- assertTrue(NumberUtils.SHORT_MINUS_ONE.shortValue() == -1);
- assertTrue(NumberUtils.BYTE_ZERO.byteValue() == 0);
- assertTrue(NumberUtils.BYTE_ONE.byteValue() == 1);
- assertTrue(NumberUtils.BYTE_MINUS_ONE.byteValue() == -1);
- assertTrue(NumberUtils.DOUBLE_ZERO.doubleValue() == 0.0d);
- assertTrue(NumberUtils.DOUBLE_ONE.doubleValue() == 1.0d);
- assertTrue(NumberUtils.DOUBLE_MINUS_ONE.doubleValue() == -1.0d);
- assertTrue(NumberUtils.FLOAT_ZERO.floatValue() == 0.0f);
- assertTrue(NumberUtils.FLOAT_ONE.floatValue() == 1.0f);
- assertTrue(NumberUtils.FLOAT_MINUS_ONE.floatValue() == -1.0f);
- }
}
diff --git a/src/test/org/apache/commons/lang/math/MathTestSuite.java b/src/test/org/apache/commons/lang/math/MathTestSuite.java
index c938d6701..cfa9e4167 100644
--- a/src/test/org/apache/commons/lang/math/MathTestSuite.java
+++ b/src/test/org/apache/commons/lang/math/MathTestSuite.java
@@ -1,7 +1,7 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2002 The Apache Software Foundation. All rights
+ * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -61,7 +61,7 @@ import junit.textui.TestRunner;
* Test suite for the Math package.
*
* @author Stephen Colebourne
- * @version $Id: MathTestSuite.java,v 1.3 2003/05/14 02:41:26 bayard Exp $
+ * @version $Id: MathTestSuite.java,v 1.4 2003/06/24 21:14:51 scolebourne Exp $
*/
public class MathTestSuite extends TestCase {
@@ -91,6 +91,7 @@ public class MathTestSuite extends TestCase {
suite.addTest(IntRangeTest.suite());
suite.addTest(LongRangeTest.suite());
suite.addTest(NumberRangeTest.suite());
+ suite.addTest(NumberUtilsTest.suite());
suite.addTest(RandomUtilsTest.suite());
return suite;
}
diff --git a/src/test/org/apache/commons/lang/math/NumberUtilsTest.java b/src/test/org/apache/commons/lang/math/NumberUtilsTest.java
new file mode 100644
index 000000000..d3f702cf9
--- /dev/null
+++ b/src/test/org/apache/commons/lang/math/NumberUtilsTest.java
@@ -0,0 +1,615 @@
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowlegement may appear in the software itself,
+ * if and wherever such third-party acknowlegements normally appear.
+ *
+ * 4. The names "The Jakarta Project", "Commons", and "Apache Software
+ * Foundation" must not be used to endorse or promote products derived
+ * from this software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * .
+ */
+package org.apache.commons.lang.math;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.lang.SystemUtils;
+
+/**
+ * Unit tests {@link org.apache.commons.lang.math.NumberUtils}.
+ *
+ * @author Rand McNeely
+ * @author Ringo De Smet
+ * @author Eric Pugh
+ * @author Phil Steitz
+ * @author Stephen Colebourne
+ * @version $Id: NumberUtilsTest.java,v 1.1 2003/06/24 21:14:51 scolebourne Exp $
+ */
+public class NumberUtilsTest extends TestCase {
+
+ public NumberUtilsTest(String name) {
+ super(name);
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(NumberUtilsTest.class);
+ suite.setName("NumberUtils Tests");
+ return suite;
+ }
+
+ //---------------------------------------------------------------------
+
+ /**
+ * Test for int stringToInt(String)
+ */
+ public void testStringToIntString() {
+ assertTrue("stringToInt(String) 1 failed", NumberUtils.stringToInt("12345") == 12345);
+ assertTrue("stringToInt(String) 2 failed", NumberUtils.stringToInt("abc") == 0);
+ }
+
+ /**
+ * Test for int stringToInt(String, int)
+ */
+ public void testStringToIntStringI() {
+ assertTrue("stringToInt(String,int) 1 failed", NumberUtils.stringToInt("12345", 5) == 12345);
+ assertTrue("stringToInt(String,int) 2 failed", NumberUtils.stringToInt("1234.5", 5) == 5);
+ }
+
+ public void testCreateNumber() {
+ //a lot of things can go wrong
+ assertEquals("createNumber(String) 1 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5"));
+ assertEquals("createNumber(String) 2 failed", new Integer("12345"), NumberUtils.createNumber("12345"));
+ assertEquals("createNumber(String) 3 failed", new Double("1234.5"), NumberUtils.createNumber("1234.5D"));
+ assertEquals("createNumber(String) 4 failed", new Float("1234.5"), NumberUtils.createNumber("1234.5F"));
+ assertEquals("createNumber(String) 5 failed", new Long(Integer.MAX_VALUE + 1L), NumberUtils.createNumber("" + (Integer.MAX_VALUE + 1L)));
+ assertEquals("createNumber(String) 6 failed", new Long(12345), NumberUtils.createNumber("12345L"));
+ assertEquals("createNumber(String) 7 failed", new Float("-1234.5"), NumberUtils.createNumber("-1234.5"));
+ assertEquals("createNumber(String) 8 failed", new Integer("-12345"), NumberUtils.createNumber("-12345"));
+ assertTrue("createNumber(String) 9 failed", 0xFADE == NumberUtils.createNumber("0xFADE").intValue());
+ assertTrue("createNumber(String) 10 failed", -0xFADE == NumberUtils.createNumber("-0xFADE").intValue());
+ assertEquals("createNumber(String) 11 failed", new Double("1.1E200"), NumberUtils.createNumber("1.1E200"));
+ assertEquals("createNumber(String) 12 failed", new Float("1.1E20"), NumberUtils.createNumber("1.1E20"));
+ assertEquals("createNumber(String) 13 failed", new Double("-1.1E200"), NumberUtils.createNumber("-1.1E200"));
+ assertEquals("createNumber(String) 14 failed", new Double("1.1E-200"), NumberUtils.createNumber("1.1E-200"));
+
+ // jdk 1.2 doesn't support this. unsure about jdk 1.2.2
+ if(SystemUtils.isJavaVersionAtLeast(1.3f)) {
+ assertEquals("createNumber(String) 15 failed", new BigDecimal("1.1E-700"), NumberUtils.createNumber("1.1E-700F"));
+ }
+ assertEquals(
+ "createNumber(String) 16 failed",
+ new Long("10" + Integer.MAX_VALUE),
+ NumberUtils.createNumber("10" + Integer.MAX_VALUE + "L"));
+ assertEquals(
+ "createNumber(String) 17 failed",
+ new Long("10" + Integer.MAX_VALUE),
+ NumberUtils.createNumber("10" + Integer.MAX_VALUE));
+ assertEquals(
+ "createNumber(String) 18 failed",
+ new BigInteger("10" + Long.MAX_VALUE),
+ NumberUtils.createNumber("10" + Long.MAX_VALUE));
+
+ }
+
+ public void testCreateFloat() {
+ assertEquals("createFloat(String) failed", new Float("1234.5"), NumberUtils.createFloat("1234.5"));
+ }
+
+ public void testCreateDouble() {
+ assertEquals("createDouble(String) failed", new Double("1234.5"), NumberUtils.createDouble("1234.5"));
+ }
+
+ public void testCreateInteger() {
+ assertEquals("createInteger(String) failed", new Integer("12345"), NumberUtils.createInteger("12345"));
+ }
+
+ public void testCreateLong() {
+ assertEquals("createInteger(String) failed", new Long("12345"), NumberUtils.createLong("12345"));
+ }
+
+ public void testCreateBigInteger() {
+ assertEquals("createBigInteger(String) failed", new BigInteger("12345"), NumberUtils.createBigInteger("12345"));
+ }
+
+ public void testCreateBigDecimal() {
+ assertEquals("createBigDecimal(String) failed", new BigDecimal("1234.5"), NumberUtils.createBigDecimal("1234.5"));
+ }
+
+ public void testMinimumLong() {
+ assertEquals("minimum(long,long,long) 1 failed", 12345L, NumberUtils.min(12345L, 12345L + 1L, 12345L + 2L));
+ assertEquals("minimum(long,long,long) 2 failed", 12345L, NumberUtils.min(12345L + 1L, 12345L, 12345 + 2L));
+ assertEquals("minimum(long,long,long) 3 failed", 12345L, NumberUtils.min(12345L + 1L, 12345L + 2L, 12345L));
+ assertEquals("minimum(long,long,long) 4 failed", 12345L, NumberUtils.min(12345L + 1L, 12345L, 12345L));
+ assertEquals("minimum(long,long,long) 5 failed", 12345L, NumberUtils.min(12345L, 12345L, 12345L));
+ }
+
+ public void testMinimumInt() {
+ assertEquals("minimum(int,int,int) 1 failed", 12345, NumberUtils.min(12345, 12345 + 1, 12345 + 2));
+ assertEquals("minimum(int,int,int) 2 failed", 12345, NumberUtils.min(12345 + 1, 12345, 12345 + 2));
+ assertEquals("minimum(int,int,int) 3 failed", 12345, NumberUtils.min(12345 + 1, 12345 + 2, 12345));
+ assertEquals("minimum(int,int,int) 4 failed", 12345, NumberUtils.min(12345 + 1, 12345, 12345));
+ assertEquals("minimum(int,int,int) 5 failed", 12345, NumberUtils.min(12345, 12345, 12345));
+ }
+
+ public void testMinimumShort() {
+ short low = 1234;
+ short mid = 1234 + 1;
+ short high = 1234 + 2;
+ assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(low, mid, high));
+ assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(mid, low, high));
+ assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(mid, high, low));
+ assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(low, mid, low));
+ }
+
+ public void testMinimumByte() {
+ byte low = 123;
+ byte mid = 123 + 1;
+ byte high = 123 + 2;
+ assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(low, mid, high));
+ assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(mid, low, high));
+ assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(mid, high, low));
+ assertEquals("minimum(int,int,int) 1 failed", low, NumberUtils.min(low, mid, low));
+ }
+
+ public void testMaximumLong() {
+ assertEquals("maximum(long,long,long) 1 failed", 12345L, NumberUtils.max(12345L, 12345L - 1L, 12345L - 2L));
+ assertEquals("maximum(long,long,long) 2 failed", 12345L, NumberUtils.max(12345L - 1L, 12345L, 12345L - 2L));
+ assertEquals("maximum(long,long,long) 3 failed", 12345L, NumberUtils.max(12345L - 1L, 12345L - 2L, 12345L));
+ assertEquals("maximum(long,long,long) 4 failed", 12345L, NumberUtils.max(12345L - 1L, 12345L, 12345L));
+ assertEquals("maximum(long,long,long) 5 failed", 12345L, NumberUtils.max(12345L, 12345L, 12345L));
+ }
+
+ public void testMaximumInt() {
+ assertEquals("maximum(int,int,int) 1 failed", 12345, NumberUtils.max(12345, 12345 - 1, 12345 - 2));
+ assertEquals("maximum(int,int,int) 2 failed", 12345, NumberUtils.max(12345 - 1, 12345, 12345 - 2));
+ assertEquals("maximum(int,int,int) 3 failed", 12345, NumberUtils.max(12345 - 1, 12345 - 2, 12345));
+ assertEquals("maximum(int,int,int) 4 failed", 12345, NumberUtils.max(12345 - 1, 12345, 12345));
+ assertEquals("maximum(int,int,int) 5 failed", 12345, NumberUtils.max(12345, 12345, 12345));
+ }
+
+ public void testMaximumShort() {
+ short low = 1234;
+ short mid = 1234 + 1;
+ short high = 1234 + 2;
+ assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(low, mid, high));
+ assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(mid, low, high));
+ assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(mid, high, low));
+ assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(high, mid, high));
+ }
+
+ public void testMaximumByte() {
+ byte low = 123;
+ byte mid = 123 + 1;
+ byte high = 123 + 2;
+ assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(low, mid, high));
+ assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(mid, low, high));
+ assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(mid, high, low));
+ assertEquals("minimum(int,int,int) 1 failed", high, NumberUtils.max(high, mid, high));
+ }
+
+ public void testCompareDouble() {
+ assertTrue(NumberUtils.compare(Double.NaN, Double.NaN) == 0);
+ assertTrue(NumberUtils.compare(Double.NaN, Double.POSITIVE_INFINITY) == +1);
+ assertTrue(NumberUtils.compare(Double.NaN, Double.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(Double.NaN, 1.2d) == +1);
+ assertTrue(NumberUtils.compare(Double.NaN, 0.0d) == +1);
+ assertTrue(NumberUtils.compare(Double.NaN, -0.0d) == +1);
+ assertTrue(NumberUtils.compare(Double.NaN, -1.2d) == +1);
+ assertTrue(NumberUtils.compare(Double.NaN, -Double.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(Double.NaN, Double.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.NaN) == -1);
+ assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY) == 0);
+ assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, 1.2d) == +1);
+ assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, 0.0d) == +1);
+ assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -0.0d) == +1);
+ assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -1.2d) == +1);
+ assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, -Double.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.NaN) == -1);
+ assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.MAX_VALUE) == 0);
+ assertTrue(NumberUtils.compare(Double.MAX_VALUE, 1.2d) == +1);
+ assertTrue(NumberUtils.compare(Double.MAX_VALUE, 0.0d) == +1);
+ assertTrue(NumberUtils.compare(Double.MAX_VALUE, -0.0d) == +1);
+ assertTrue(NumberUtils.compare(Double.MAX_VALUE, -1.2d) == +1);
+ assertTrue(NumberUtils.compare(Double.MAX_VALUE, -Double.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(Double.MAX_VALUE, Double.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(1.2d, Double.NaN) == -1);
+ assertTrue(NumberUtils.compare(1.2d, Double.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(1.2d, Double.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(1.2d, 1.2d) == 0);
+ assertTrue(NumberUtils.compare(1.2d, 0.0d) == +1);
+ assertTrue(NumberUtils.compare(1.2d, -0.0d) == +1);
+ assertTrue(NumberUtils.compare(1.2d, -1.2d) == +1);
+ assertTrue(NumberUtils.compare(1.2d, -Double.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(1.2d, Double.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(0.0d, Double.NaN) == -1);
+ assertTrue(NumberUtils.compare(0.0d, Double.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(0.0d, Double.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(0.0d, 1.2d) == -1);
+ assertTrue(NumberUtils.compare(0.0d, 0.0d) == 0);
+ assertTrue(NumberUtils.compare(0.0d, -0.0d) == +1);
+ assertTrue(NumberUtils.compare(0.0d, -1.2d) == +1);
+ assertTrue(NumberUtils.compare(0.0d, -Double.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(0.0d, Double.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(-0.0d, Double.NaN) == -1);
+ assertTrue(NumberUtils.compare(-0.0d, Double.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(-0.0d, Double.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(-0.0d, 1.2d) == -1);
+ assertTrue(NumberUtils.compare(-0.0d, 0.0d) == -1);
+ assertTrue(NumberUtils.compare(-0.0d, -0.0d) == 0);
+ assertTrue(NumberUtils.compare(-0.0d, -1.2d) == +1);
+ assertTrue(NumberUtils.compare(-0.0d, -Double.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(-0.0d, Double.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(-1.2d, Double.NaN) == -1);
+ assertTrue(NumberUtils.compare(-1.2d, Double.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(-1.2d, Double.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(-1.2d, 1.2d) == -1);
+ assertTrue(NumberUtils.compare(-1.2d, 0.0d) == -1);
+ assertTrue(NumberUtils.compare(-1.2d, -0.0d) == -1);
+ assertTrue(NumberUtils.compare(-1.2d, -1.2d) == 0);
+ assertTrue(NumberUtils.compare(-1.2d, -Double.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(-1.2d, Double.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.NaN) == -1);
+ assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(-Double.MAX_VALUE, 1.2d) == -1);
+ assertTrue(NumberUtils.compare(-Double.MAX_VALUE, 0.0d) == -1);
+ assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -0.0d) == -1);
+ assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -1.2d) == -1);
+ assertTrue(NumberUtils.compare(-Double.MAX_VALUE, -Double.MAX_VALUE) == 0);
+ assertTrue(NumberUtils.compare(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.NaN) == -1);
+ assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, 1.2d) == -1);
+ assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, 0.0d) == -1);
+ assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -0.0d) == -1);
+ assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -1.2d) == -1);
+ assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, -Double.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY) == 0);
+ }
+
+ public void testCompareFloat() {
+ assertTrue(NumberUtils.compare(Float.NaN, Float.NaN) == 0);
+ assertTrue(NumberUtils.compare(Float.NaN, Float.POSITIVE_INFINITY) == +1);
+ assertTrue(NumberUtils.compare(Float.NaN, Float.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(Float.NaN, 1.2f) == +1);
+ assertTrue(NumberUtils.compare(Float.NaN, 0.0f) == +1);
+ assertTrue(NumberUtils.compare(Float.NaN, -0.0f) == +1);
+ assertTrue(NumberUtils.compare(Float.NaN, -1.2f) == +1);
+ assertTrue(NumberUtils.compare(Float.NaN, -Float.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(Float.NaN, Float.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.NaN) == -1);
+ assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY) == 0);
+ assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, 1.2f) == +1);
+ assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, 0.0f) == +1);
+ assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -0.0f) == +1);
+ assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -1.2f) == +1);
+ assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, -Float.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.NaN) == -1);
+ assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.MAX_VALUE) == 0);
+ assertTrue(NumberUtils.compare(Float.MAX_VALUE, 1.2f) == +1);
+ assertTrue(NumberUtils.compare(Float.MAX_VALUE, 0.0f) == +1);
+ assertTrue(NumberUtils.compare(Float.MAX_VALUE, -0.0f) == +1);
+ assertTrue(NumberUtils.compare(Float.MAX_VALUE, -1.2f) == +1);
+ assertTrue(NumberUtils.compare(Float.MAX_VALUE, -Float.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(Float.MAX_VALUE, Float.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(1.2f, Float.NaN) == -1);
+ assertTrue(NumberUtils.compare(1.2f, Float.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(1.2f, Float.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(1.2f, 1.2f) == 0);
+ assertTrue(NumberUtils.compare(1.2f, 0.0f) == +1);
+ assertTrue(NumberUtils.compare(1.2f, -0.0f) == +1);
+ assertTrue(NumberUtils.compare(1.2f, -1.2f) == +1);
+ assertTrue(NumberUtils.compare(1.2f, -Float.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(1.2f, Float.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(0.0f, Float.NaN) == -1);
+ assertTrue(NumberUtils.compare(0.0f, Float.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(0.0f, Float.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(0.0f, 1.2f) == -1);
+ assertTrue(NumberUtils.compare(0.0f, 0.0f) == 0);
+ assertTrue(NumberUtils.compare(0.0f, -0.0f) == +1);
+ assertTrue(NumberUtils.compare(0.0f, -1.2f) == +1);
+ assertTrue(NumberUtils.compare(0.0f, -Float.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(0.0f, Float.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(-0.0f, Float.NaN) == -1);
+ assertTrue(NumberUtils.compare(-0.0f, Float.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(-0.0f, Float.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(-0.0f, 1.2f) == -1);
+ assertTrue(NumberUtils.compare(-0.0f, 0.0f) == -1);
+ assertTrue(NumberUtils.compare(-0.0f, -0.0f) == 0);
+ assertTrue(NumberUtils.compare(-0.0f, -1.2f) == +1);
+ assertTrue(NumberUtils.compare(-0.0f, -Float.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(-0.0f, Float.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(-1.2f, Float.NaN) == -1);
+ assertTrue(NumberUtils.compare(-1.2f, Float.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(-1.2f, Float.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(-1.2f, 1.2f) == -1);
+ assertTrue(NumberUtils.compare(-1.2f, 0.0f) == -1);
+ assertTrue(NumberUtils.compare(-1.2f, -0.0f) == -1);
+ assertTrue(NumberUtils.compare(-1.2f, -1.2f) == 0);
+ assertTrue(NumberUtils.compare(-1.2f, -Float.MAX_VALUE) == +1);
+ assertTrue(NumberUtils.compare(-1.2f, Float.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.NaN) == -1);
+ assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(-Float.MAX_VALUE, 1.2f) == -1);
+ assertTrue(NumberUtils.compare(-Float.MAX_VALUE, 0.0f) == -1);
+ assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -0.0f) == -1);
+ assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -1.2f) == -1);
+ assertTrue(NumberUtils.compare(-Float.MAX_VALUE, -Float.MAX_VALUE) == 0);
+ assertTrue(NumberUtils.compare(-Float.MAX_VALUE, Float.NEGATIVE_INFINITY) == +1);
+
+ assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.NaN) == -1);
+ assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY) == -1);
+ assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, 1.2f) == -1);
+ assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, 0.0f) == -1);
+ assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -0.0f) == -1);
+ assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -1.2f) == -1);
+ assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, -Float.MAX_VALUE) == -1);
+ assertTrue(NumberUtils.compare(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY) == 0);
+ }
+
+ public void testIsDigits() {
+ assertEquals("isDigits(null) failed", false, NumberUtils.isDigits(null));
+ assertEquals("isDigits('') failed", false, NumberUtils.isDigits(""));
+ assertEquals("isDigits(String) failed", true, NumberUtils.isDigits("12345"));
+ assertEquals("isDigits(String) neg 1 failed", false, NumberUtils.isDigits("1234.5"));
+ assertEquals("isDigits(String) neg 3 failed", false, NumberUtils.isDigits("1ab"));
+ assertEquals("isDigits(String) neg 4 failed", false, NumberUtils.isDigits("abc"));
+ }
+
+ /**
+ * Tests isNumber(String) and tests that createNumber(String) returns
+ * a valid number iff isNumber(String) returns false.
+ */
+ public void testIsNumber() {
+ String val = "12345";
+ assertTrue("isNumber(String) 1 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 1 failed", checkCreateNumber(val));
+ val = "1234.5";
+ assertTrue("isNumber(String) 2 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 2 failed", checkCreateNumber(val));
+ val = ".12345";
+ assertTrue("isNumber(String) 3 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 3 failed", checkCreateNumber(val));
+ val = "1234E5";
+ assertTrue("isNumber(String) 4 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 4 failed", checkCreateNumber(val));
+ val = "1234E+5";
+ assertTrue("isNumber(String) 5 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 5 failed", checkCreateNumber(val));
+ val = "1234E-5";
+ assertTrue("isNumber(String) 6 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 6 failed", checkCreateNumber(val));
+ val = "123.4E5";
+ assertTrue("isNumber(String) 7 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 7 failed", checkCreateNumber(val));
+ val = "-1234";
+ assertTrue("isNumber(String) 8 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 8 failed", checkCreateNumber(val));
+ val = "-1234.5";
+ assertTrue("isNumber(String) 9 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 9 failed", checkCreateNumber(val));
+ val = "-.12345";
+ assertTrue("isNumber(String) 10 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 10 failed", checkCreateNumber(val));
+ val = "-1234E5";
+ assertTrue("isNumber(String) 11 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 11 failed", checkCreateNumber(val));
+ val = "0";
+ assertTrue("isNumber(String) 12 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 12 failed", checkCreateNumber(val));
+ val = "-0";
+ assertTrue("isNumber(String) 13 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 13 failed", checkCreateNumber(val));
+ val = "01234";
+ assertTrue("isNumber(String) 14 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 14 failed", checkCreateNumber(val));
+ val = "-01234";
+ assertTrue("isNumber(String) 15 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 15 failed", checkCreateNumber(val));
+ val = "0xABC123";
+ assertTrue("isNumber(String) 16 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 16 failed", checkCreateNumber(val));
+ val = "0x0";
+ assertTrue("isNumber(String) 17 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 17 failed", checkCreateNumber(val));
+ val = "123.4E21D";
+ assertTrue("isNumber(String) 19 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 19 failed", checkCreateNumber(val));
+ val = "-221.23F";
+ assertTrue("isNumber(String) 20 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 20 failed", checkCreateNumber(val));
+ val = "22338L";
+ assertTrue("isNumber(String) 21 failed", NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 21 failed", checkCreateNumber(val));
+ val = null;
+ assertTrue("isNumber(String) 1 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 1 Neg failed", !checkCreateNumber(val));
+ val = "";
+ assertTrue("isNumber(String) 2 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 2 Neg failed", !checkCreateNumber(val));
+ val = "--2.3";
+ assertTrue("isNumber(String) 3 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 3 Neg failed", !checkCreateNumber(val));
+ val = ".12.3";
+ assertTrue("isNumber(String) 4 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 4 Neg failed", !checkCreateNumber(val));
+ val = "-123E";
+ assertTrue("isNumber(String) 5 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 5 Neg failed", !checkCreateNumber(val));
+ val = "-123E+-212";
+ assertTrue("isNumber(String) 6 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 6 Neg failed", !checkCreateNumber(val));
+ val = "-123E2.12";
+ assertTrue("isNumber(String) 7 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 7 Neg failed", !checkCreateNumber(val));
+ val = "0xGF";
+ assertTrue("isNumber(String) 8 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 8 Neg failed", !checkCreateNumber(val));
+ val = "0xFAE-1";
+ assertTrue("isNumber(String) 9 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 9 Neg failed", !checkCreateNumber(val));
+ val = ".";
+ assertTrue("isNumber(String) 10 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 10 Neg failed", !checkCreateNumber(val));
+ val = "-0ABC123";
+ assertTrue("isNumber(String) 11 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 11 Neg failed", !checkCreateNumber(val));
+ val = "123.4E-D";
+ assertTrue("isNumber(String) 12 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 12 Neg failed", !checkCreateNumber(val));
+ val = "123.4ED";
+ assertTrue("isNumber(String) 13 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 13 Neg failed", !checkCreateNumber(val));
+ val = "1234E5l";
+ assertTrue("isNumber(String) 14 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 14 Neg failed", !checkCreateNumber(val));
+ val = "11a";
+ assertTrue("isNumber(String) 15 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 15 Neg failed", !checkCreateNumber(val));
+ val = "1a";
+ assertTrue("isNumber(String) 16 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 16 Neg failed", !checkCreateNumber(val));
+ val = "a";
+ assertTrue("isNumber(String) 17 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 17 Neg failed", !checkCreateNumber(val));
+ val = "11g";
+ assertTrue("isNumber(String) 18 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 18 Neg failed", !checkCreateNumber(val));
+ val = "11z";
+ assertTrue("isNumber(String) 19 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 19 Neg failed", !checkCreateNumber(val));
+ val = "11def";
+ assertTrue("isNumber(String) 20 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 20 Neg failed", !checkCreateNumber(val));
+ val = "11d11";
+ assertTrue("isNumber(String) 21 Neg failed", !NumberUtils.isNumber(val));
+ assertTrue("isNumber(String)/createNumber(String) 21 Neg failed", !checkCreateNumber(val));
+
+ }
+
+ private boolean checkCreateNumber(String val) {
+ try {
+ Object obj = NumberUtils.createNumber(val);
+ if (obj == null) {
+ return false;
+ }
+ return true;
+ } catch (NumberFormatException e) {
+ return false;
+ } catch (NullPointerException e) {
+ return false;
+ }
+ }
+
+ public void testConstants() {
+ assertTrue(NumberUtils.LONG_ZERO instanceof Long);
+ assertTrue(NumberUtils.LONG_ONE instanceof Long);
+ assertTrue(NumberUtils.LONG_MINUS_ONE instanceof Long);
+ assertTrue(NumberUtils.INTEGER_ZERO instanceof Integer);
+ assertTrue(NumberUtils.INTEGER_ONE instanceof Integer);
+ assertTrue(NumberUtils.INTEGER_MINUS_ONE instanceof Integer);
+ assertTrue(NumberUtils.SHORT_ZERO instanceof Short);
+ assertTrue(NumberUtils.SHORT_ONE instanceof Short);
+ assertTrue(NumberUtils.SHORT_MINUS_ONE instanceof Short);
+ assertTrue(NumberUtils.BYTE_ZERO instanceof Byte);
+ assertTrue(NumberUtils.BYTE_ONE instanceof Byte);
+ assertTrue(NumberUtils.BYTE_MINUS_ONE instanceof Byte);
+ assertTrue(NumberUtils.DOUBLE_ZERO instanceof Double);
+ assertTrue(NumberUtils.DOUBLE_ONE instanceof Double);
+ assertTrue(NumberUtils.DOUBLE_MINUS_ONE instanceof Double);
+ assertTrue(NumberUtils.FLOAT_ZERO instanceof Float);
+ assertTrue(NumberUtils.FLOAT_ONE instanceof Float);
+ assertTrue(NumberUtils.FLOAT_MINUS_ONE instanceof Float);
+
+ assertTrue(NumberUtils.LONG_ZERO.longValue() == 0);
+ assertTrue(NumberUtils.LONG_ONE.longValue() == 1);
+ assertTrue(NumberUtils.LONG_MINUS_ONE.longValue() == -1);
+ assertTrue(NumberUtils.INTEGER_ZERO.intValue() == 0);
+ assertTrue(NumberUtils.INTEGER_ONE.intValue() == 1);
+ assertTrue(NumberUtils.INTEGER_MINUS_ONE.intValue() == -1);
+ assertTrue(NumberUtils.SHORT_ZERO.shortValue() == 0);
+ assertTrue(NumberUtils.SHORT_ONE.shortValue() == 1);
+ assertTrue(NumberUtils.SHORT_MINUS_ONE.shortValue() == -1);
+ assertTrue(NumberUtils.BYTE_ZERO.byteValue() == 0);
+ assertTrue(NumberUtils.BYTE_ONE.byteValue() == 1);
+ assertTrue(NumberUtils.BYTE_MINUS_ONE.byteValue() == -1);
+ assertTrue(NumberUtils.DOUBLE_ZERO.doubleValue() == 0.0d);
+ assertTrue(NumberUtils.DOUBLE_ONE.doubleValue() == 1.0d);
+ assertTrue(NumberUtils.DOUBLE_MINUS_ONE.doubleValue() == -1.0d);
+ assertTrue(NumberUtils.FLOAT_ZERO.floatValue() == 0.0f);
+ assertTrue(NumberUtils.FLOAT_ONE.floatValue() == 1.0f);
+ assertTrue(NumberUtils.FLOAT_MINUS_ONE.floatValue() == -1.0f);
+ }
+
+}