/*
 * This file is part of Nuts Framework.
 * Copyright(C) 2009-2012 Nuts Develop Team.
 *
 * Nuts Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License any later version.
 * 
 * Nuts Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Nuts Framework. If not, see <http://www.gnu.org/licenses/>.
 */
package nuts.core.lang;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * Utility class for Number.
 */
public class Numbers {

	/** Reusable Long constant for zero. */
	public static final Long LONG_ZERO = Long.valueOf(0L);
	/** Reusable Long constant for one. */
	public static final Long LONG_ONE = Long.valueOf(1L);
	/** Reusable Long constant for minus one. */
	public static final Long LONG_MINUS_ONE = Long.valueOf(-1L);
	/** Reusable Integer constant for zero. */
	public static final Integer INTEGER_ZERO = Integer.valueOf(0);
	/** Reusable Integer constant for one. */
	public static final Integer INTEGER_ONE = Integer.valueOf(1);
	/** Reusable Integer constant for minus one. */
	public static final Integer INTEGER_MINUS_ONE = Integer.valueOf(-1);
	/** Reusable Short constant for zero. */
	public static final Short SHORT_ZERO = Short.valueOf((short) 0);
	/** Reusable Short constant for one. */
	public static final Short SHORT_ONE = Short.valueOf((short) 1);
	/** Reusable Short constant for minus one. */
	public static final Short SHORT_MINUS_ONE = Short.valueOf((short) -1);
	/** Reusable Byte constant for zero. */
	public static final Byte BYTE_ZERO = Byte.valueOf((byte) 0);
	/** Reusable Byte constant for one. */
	public static final Byte BYTE_ONE = Byte.valueOf((byte) 1);
	/** Reusable Byte constant for minus one. */
	public static final Byte BYTE_MINUS_ONE = Byte.valueOf((byte) -1);
	/** Reusable Double constant for zero. */
	public static final Double DOUBLE_ZERO = Double.valueOf(0.0d);
	/** Reusable Double constant for one. */
	public static final Double DOUBLE_ONE = Double.valueOf(1.0d);
	/** Reusable Double constant for minus one. */
	public static final Double DOUBLE_MINUS_ONE = Double.valueOf(-1.0d);
	/** Reusable Float constant for zero. */
	public static final Float FLOAT_ZERO = Float.valueOf(0.0f);
	/** Reusable Float constant for one. */
	public static final Float FLOAT_ONE = Float.valueOf(1.0f);
	/** Reusable Float constant for minus one. */
	public static final Float FLOAT_MINUS_ONE = Float.valueOf(-1.0f);

	/**
	 * <p>
	 * Checks if the value can safely be converted to a byte primitive.
	 * </p>
	 * 
	 * @param value The value validation is being performed on.
	 * @return true if the value can be converted to a Byte.
	 */
	public static boolean isByte(String value) {
		return (toByte(value) != null);
	}

	/**
	 * <p>
	 * Checks if the value can safely be converted to a short primitive.
	 * </p>
	 * 
	 * @param value The value validation is being performed on.
	 * @return true if the value can be converted to a Short.
	 */
	public static boolean isShort(String value) {
		return (toShort(value) != null);
	}

	/**
	 * <p>
	 * Checks if the value can safely be converted to a int primitive.
	 * </p>
	 * 
	 * @param value The value validation is being performed on.
	 * @return true if the value can be converted to an Integer.
	 */
	public static boolean isInt(String value) {
		return (toInt(value) != null);
	}

	/**
	 * <p>
	 * Checks if the value can safely be converted to a long primitive.
	 * </p>
	 * 
	 * @param value The value validation is being performed on.
	 * @return true if the value can be converted to a Long.
	 */
	public static boolean isLong(String value) {
		return (toLong(value) != null);
	}

	/**
	 * <p>
	 * Checks if the value can safely be converted to a float primitive.
	 * </p>
	 * 
	 * @param value The value validation is being performed on.
	 * @return true if the value can be converted to a Float.
	 */
	public static boolean isFloat(String value) {
		return (toFloat(value) != null);
	}

	/**
	 * <p>
	 * Checks if the value can safely be converted to a double primitive.
	 * </p>
	 * 
	 * @param value The value validation is being performed on.
	 * @return true if the value can be converted to a Double.
	 */
	public static boolean isDouble(String value) {
		return (toDouble(value) != null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to an <code>int</code>, returning <code>zero</code> if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, <code>null</code> is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toInt(null) = null
	 *   NumberUtils.toInt("")   = null
	 *   NumberUtils.toInt("1")  = 1
	 * </pre>
	 * 
	 * @param str the string to convert, may be null
	 * @return the int represented by the string, or <code>null</code> if conversion fails
	 */
	public static Integer toInt(String str) {
		return toInt(str, null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to an <code>int</code>, returning a default value if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, the default value is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toInt(null, 1) = 1
	 *   NumberUtils.toInt("", 1)   = 1
	 *   NumberUtils.toInt("1", 0)  = 1
	 * </pre>
	 * 
	 * @param str the string to convert, may be null
	 * @param defaultValue the default value
	 * @return the int represented by the string, or the default if conversion fails
	 */
	public static Integer toInt(String str, Integer defaultValue) {
		if (str == null) {
			return defaultValue;
		}
		try {
			return Integer.parseInt(str);
		}
		catch (NumberFormatException nfe) {
			return defaultValue;
		}
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>long</code>, returning <code>null</code> if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, <code>null</code> is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toLong(null) = null
	 *   NumberUtils.toLong("")   = null
	 *   NumberUtils.toLong("1")  = 1L
	 * </pre>
	 * 
	 * @param str the string to convert, may be null
	 * @return the long represented by the string, or <code>null</code> if conversion fails
	 */
	public static Long toLong(String str) {
		return toLong(str, null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>long</code>, returning a default value if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, the default value is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toLong(null, 1L) = 1L
	 *   NumberUtils.toLong("", 1L)   = 1L
	 *   NumberUtils.toLong("1", 0L)  = 1L
	 * </pre>
	 * 
	 * @param str the string to convert, may be null
	 * @param defaultValue the default value
	 * @return the long represented by the string, or the default if conversion fails
	 */
	public static Long toLong(String str, Long defaultValue) {
		if (str == null) {
			return defaultValue;
		}
		try {
			return Long.parseLong(str);
		}
		catch (NumberFormatException nfe) {
			return defaultValue;
		}
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>float</code>, returning <code>null</code> if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, <code>null</code> is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toFloat(null)   = null
	 *   NumberUtils.toFloat("")     = null
	 *   NumberUtils.toFloat("1.5")  = 1.5f
	 * </pre>
	 * 
	 * @param str the string to convert, may be <code>null</code>
	 * @return the float represented by the string, or <code>null</code> if conversion fails
	 */
	public static Float toFloat(String str) {
		return toFloat(str, null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>float</code>, returning a default value if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string <code>str</code> is <code>null</code>, the default value is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toFloat(null, 1.1f)   = 1.0f
	 *   NumberUtils.toFloat("", 1.1f)     = 1.1f
	 *   NumberUtils.toFloat("1.5", 0.0f)  = 1.5f
	 * </pre>
	 * 
	 * @param str the string to convert, may be <code>null</code>
	 * @param defaultValue the default value
	 * @return the float represented by the string, or defaultValue if conversion fails
	 */
	public static Float toFloat(String str, Float defaultValue) {
		if (str == null) {
			return defaultValue;
		}
		try {
			return Float.parseFloat(str);
		}
		catch (NumberFormatException nfe) {
			return defaultValue;
		}
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>double</code>, returning <code>null</code> if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, <code>null</code> is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toDouble(null)   = null
	 *   NumberUtils.toDouble("")     = null
	 *   NumberUtils.toDouble("1.5")  = 1.5d
	 * </pre>
	 * 
	 * @param str the string to convert, may be <code>null</code>
	 * @return the double represented by the string, or <code>null</code> if conversion fails
	 */
	public static Double toDouble(String str) {
		return toDouble(str, null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>double</code>, returning a default value if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string <code>str</code> is <code>null</code>, the default value is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toDouble(null, 1.1d)   = 1.1d
	 *   NumberUtils.toDouble("", 1.1d)     = 1.1d
	 *   NumberUtils.toDouble("1.5", 0.0d)  = 1.5d
	 * </pre>
	 * 
	 * @param str the string to convert, may be <code>null</code>
	 * @param defaultValue the default value
	 * @return the double represented by the string, or defaultValue if conversion fails
	 */
	public static Double toDouble(String str, Double defaultValue) {
		if (str == null) {
			return defaultValue;
		}
		try {
			return Double.parseDouble(str);
		}
		catch (NumberFormatException nfe) {
			return defaultValue;
		}
	}


	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>BigDecimal</code>, returning <code>null</code> if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, <code>null</code> is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toBigDecimal(null)   = null
	 *   NumberUtils.toBigDecimal("")     = null
	 *   NumberUtils.toBigDecimal("1.5")  = 1.5d
	 * </pre>
	 * 
	 * @param str the string to convert, may be <code>null</code>
	 * @return the double represented by the string, or <code>null</code> if conversion fails
	 */
	public static BigDecimal toBigDecimal(String str) {
		return toBigDecimal(str, null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>BigDecimal</code>, returning a default value if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string <code>str</code> is <code>null</code>, the default value is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toBigDecimal(null, 1.1d)   = 1.1d
	 *   NumberUtils.toBigDecimal("", 1.1d)     = 1.1d
	 *   NumberUtils.toBigDecimal("1.5", 0.0d)  = 1.5d
	 * </pre>
	 * 
	 * @param str the string to convert, may be <code>null</code>
	 * @param defaultValue the default value
	 * @return the double represented by the string, or defaultValue if conversion fails
	 */
	public static BigDecimal toBigDecimal(String str, BigDecimal defaultValue) {
		if (str == null) {
			return defaultValue;
		}
		try {
			return new BigDecimal(str);
		}
		catch (NumberFormatException nfe) {
			return defaultValue;
		}
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>BigDecimal</code>, returning <code>null</code> if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, <code>null</code> is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toBigDecimal(null)   = null
	 *   NumberUtils.toBigDecimal("")     = null
	 *   NumberUtils.toBigDecimal("1.5")  = 1.5d
	 * </pre>
	 * 
	 * @param num the double number to convert, may be <code>null</code>
	 * @return the double represented by the string, or <code>null</code> if conversion fails
	 */
	public static BigDecimal toBigDecimal(double num) {
		return toBigDecimal(num, null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>BigDecimal</code>, returning a default value if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string <code>str</code> is <code>null</code>, the default value is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toBigDecimal(null, 1.1d)   = 1.1d
	 *   NumberUtils.toBigDecimal("", 1.1d)     = 1.1d
	 *   NumberUtils.toBigDecimal("1.5", 0.0d)  = 1.5d
	 * </pre>
	 * 
	 * @param num the double number to convert, may be <code>null</code>
	 * @param defaultValue the default value
	 * @return the double represented by the string, or defaultValue if conversion fails
	 */
	public static BigDecimal toBigDecimal(double num, BigDecimal defaultValue) {
		if (num == Double.NaN) {
			return defaultValue;
		}
		try {
			return BigDecimal.valueOf(num);
		}
		catch (NumberFormatException nfe) {
			return defaultValue;
		}
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>BigInteger</code>, returning <code>null</code> if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, <code>null</code> is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toBigInteger(null)   = null
	 *   NumberUtils.toBigInteger("")     = null
	 *   NumberUtils.toBigInteger("15")  = 15L
	 * </pre>
	 * 
	 * @param str the string to convert, may be <code>null</code>
	 * @return the double represented by the string, or <code>null</code> if conversion fails
	 */
	public static BigInteger toBigInteger(String str) {
		return toBigInteger(str, null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>BigInteger</code>, returning a default value if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string <code>str</code> is <code>null</code>, the default value is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toBigInteger(null, 1.1d)   = 1.1d
	 *   NumberUtils.toBigInteger("", 1.1d)     = 1.1d
	 *   NumberUtils.toBigInteger("1.5", 0.0d)  = 1.5d
	 * </pre>
	 * 
	 * @param str the string to convert, may be <code>null</code>
	 * @param defaultValue the default value
	 * @return the double represented by the string, or defaultValue if conversion fails
	 */
	public static BigInteger toBigInteger(String str, BigInteger defaultValue) {
		if (str == null) {
			return defaultValue;
		}
		try {
			return new BigInteger(str);
		}
		catch (NumberFormatException nfe) {
			return defaultValue;
		}
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>BigInteger</code>, returning <code>null</code> if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, <code>null</code> is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toBigInteger(null)   = null
	 *   NumberUtils.toBigInteger("")     = null
	 *   NumberUtils.toBigInteger("15")  = 15L
	 * </pre>
	 * 
	 * @param num the double number to convert, may be <code>null</code>
	 * @return the double represented by the string, or <code>null</code> if conversion fails
	 */
	public static BigInteger toBigInteger(long num) {
		return toBigInteger(num, null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>BigInteger</code>, returning a default value if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string <code>str</code> is <code>null</code>, the default value is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toBigInteger(null, 11L)   = 11L
	 *   NumberUtils.toBigInteger("", 11L)     = 11L
	 *   NumberUtils.toBigInteger("15", 0L)  = 15L
	 * </pre>
	 * 
	 * @param num the double number to convert, may be <code>null</code>
	 * @param defaultValue the default value
	 * @return the double represented by the string, or defaultValue if conversion fails
	 */
	public static BigInteger toBigInteger(long num, BigInteger defaultValue) {
		if (num == Double.NaN) {
			return defaultValue;
		}
		try {
			return BigInteger.valueOf(num);
		}
		catch (NumberFormatException nfe) {
			return defaultValue;
		}
	}

	// -----------------------------------------------------------------------
	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>byte</code>, returning <code>null</code> if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, <code>null</code> is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toByte(null) = null
	 *   NumberUtils.toByte("")   = null
	 *   NumberUtils.toByte("1")  = 1
	 * </pre>
	 * 
	 * @param str the string to convert, may be null
	 * @return the byte represented by the string, or <code>null</code> if conversion fails
	 */
	public static Byte toByte(String str) {
		return toByte(str, null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>byte</code>, returning a default value if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, the default value is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toByte(null, 1) = 1
	 *   NumberUtils.toByte("", 1)   = 1
	 *   NumberUtils.toByte("1", 0)  = 1
	 * </pre>
	 * 
	 * @param str the string to convert, may be null
	 * @param defaultValue the default value
	 * @return the byte represented by the string, or the default if conversion fails
	 * @since 2.5
	 */
	public static Byte toByte(String str, Byte defaultValue) {
		if (str == null) {
			return defaultValue;
		}
		try {
			return Byte.parseByte(str);
		}
		catch (NumberFormatException nfe) {
			return defaultValue;
		}
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>short</code>, returning <code>null</code> if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, <code>null</code> is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toShort(null) = null
	 *   NumberUtils.toShort("")   = null
	 *   NumberUtils.toShort("1")  = 1
	 * </pre>
	 * 
	 * @param str the string to convert, may be null
	 * @return the short represented by the string, or <code>null</code> if conversion fails
	 * @since 2.5
	 */
	public static Short toShort(String str) {
		return toShort(str, null);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to an <code>short</code>, returning a default value if the
	 * conversion fails.
	 * </p>
	 * <p>
	 * If the string is <code>null</code>, the default value is returned.
	 * </p>
	 * 
	 * <pre>
	 *   NumberUtils.toShort(null, 1) = 1
	 *   NumberUtils.toShort("", 1)   = 1
	 *   NumberUtils.toShort("1", 0)  = 1
	 * </pre>
	 * 
	 * @param str the string to convert, may be null
	 * @param defaultValue the default value
	 * @return the short represented by the string, or the default if conversion fails
	 * @since 2.5
	 */
	public static Short toShort(String str, Short defaultValue) {
		if (str == null) {
			return defaultValue;
		}
		try {
			return Short.parseShort(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)
	// Float.valueOf(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)
	// Integer.valueOf(String)
	// Double.valueOf(String)
	// new Byte(String)
	// Long.valueOf(String)
	// Long.getLong(String)
	// Long.getLong(String,int)
	// Long.getLong(String,Integer)
	// Long.valueOf(String,int)
	// Long.valueOf(String)
	// Short.valueOf(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.

	/**
	 * <p>
	 * Turns a string value into a java.lang.Number.
	 * </p>
	 * 
	 * <p>
	 * First, the value is examined for a type qualifier on the end (
	 * <code>'f','F','d','D','l','L'</code>). If it is found, it starts trying
	 * to create successively larger types from the type specified until one is
	 * found that can represent the value.
	 * </p>
	 * 
	 * <p>
	 * If a type specifier is not found, it will check for a decimal point and
	 * then try successively larger types from <code>Integer</code> to
	 * <code>BigInteger</code> and from <code>Float</code> to
	 * <code>BigDecimal</code>.
	 * </p>
	 * 
	 * <p>
	 * If the string starts with <code>0x</code> or <code>-0x</code> (lower or
	 * upper case), it will be interpreted as a hexadecimal integer. Values with
	 * leading <code>0</code>'s will not be interpreted as octal.
	 * </p>
	 * 
	 * <p>
	 * Returns <code>null</code> if the string is <code>null</code>.
	 * </p>
	 * 
	 * <p>
	 * This method does not trim the input string, i.e., strings with leading or
	 * trailing spaces will generate NumberFormatExceptions.
	 * </p>
	 * 
	 * @param str
	 *            String containing a number, may be null
	 * @return Number created from the string (or null if the input is null)
	 * @throws NumberFormatException
	 *             if the value cannot be converted
	 */
	public static Number createNumber(String str) throws NumberFormatException {
		if (str == null) {
			return null;
		}
		if (Strings.isBlank(str)) {
			throw new NumberFormatException(
					"A blank string is not a valid number");
		}
		if (str.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 (str.startsWith("0x") || str.startsWith("-0x")
				|| str.startsWith("0X") || str.startsWith("-0X")) {
			return createInteger(str);
		}
		char lastChar = str.charAt(str.length() - 1);
		String mant;
		String dec;
		String exp;
		int decPos = str.indexOf('.');
		int expPos = str.indexOf('e') + str.indexOf('E') + 1;

		if (decPos > -1) {

			if (expPos > -1) {
				if (expPos < decPos || expPos > str.length()) {
					throw new NumberFormatException(str
							+ " is not a valid number.");
				}
				dec = str.substring(decPos + 1, expPos);
			} else {
				dec = str.substring(decPos + 1);
			}
			mant = str.substring(0, decPos);
		} else {
			if (expPos > -1) {
				if (expPos > str.length()) {
					throw new NumberFormatException(str
							+ " is not a valid number.");
				}
				mant = str.substring(0, expPos);
			} else {
				mant = str;
			}
			dec = null;
		}
		if (!Character.isDigit(lastChar) && lastChar != '.') {
			if (expPos > -1 && expPos < str.length() - 1) {
				exp = str.substring(expPos + 1, str.length() - 1);
			} else {
				exp = null;
			}
			// Requesting a specific type..
			String numeric = str.substring(0, str.length() - 1);
			boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
			switch (lastChar) {
			case 'l':
			case 'L':
				if (dec == null
						&& exp == null
						&& (numeric.charAt(0) == '-'
								&& isDigits(numeric.substring(1)) || isDigits(numeric))) {
					try {
						return createLong(numeric);
					} catch (NumberFormatException nfe) { // NOPMD
						// Too big for a long
					}
					return createBigInteger(numeric);

				}
				throw new NumberFormatException(str + " is not a valid number.");
			case 'f':
			case 'F':
				try {
					Float f = Numbers.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 does not have the
						// precision we want
						return f;
					}

				} catch (NumberFormatException nfe) { // NOPMD
					// ignore the bad number
				}
				//$FALL-THROUGH$
			case 'd':
			case 'D':
				try {
					Double d = Numbers.createDouble(numeric);
					if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) {
						return d;
					}
				} catch (NumberFormatException nfe) { // NOPMD
					// ignore the bad number
				}
				try {
					return createBigDecimal(numeric);
				} catch (NumberFormatException e) { // NOPMD
					// ignore the bad number
				}
				//$FALL-THROUGH$
			default:
				throw new NumberFormatException(str + " 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 < str.length() - 1) {
				exp = str.substring(expPos + 1, str.length());
			} else {
				exp = null;
			}
			if (dec == null && exp == null) {
				// Must be an int,long,bigint
				try {
					return createInteger(str);
				} catch (NumberFormatException nfe) { // NOPMD
					// ignore the bad number
				}
				try {
					return createLong(str);
				} catch (NumberFormatException nfe) { // NOPMD
					// ignore the bad number
				}
				return createBigInteger(str);

			} else {
				// Must be a float,double,BigDec
				boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
				try {
					Float f = createFloat(str);
					if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
						return f;
					}
				} catch (NumberFormatException nfe) { // NOPMD
					// ignore the bad number
				}
				try {
					Double d = createDouble(str);
					if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
						return d;
					}
				} catch (NumberFormatException nfe) { // NOPMD
					// ignore the bad number
				}

				return createBigDecimal(str);

			}
		}
	}

	/**
	 * <p>
	 * Returns <code>true</code> if s is <code>null</code>.
	 * </p>
	 * 
	 * @param str the String to check
	 * @return if it is all zeros or <code>null</code>
	 */
	public static boolean isAllZeros(String str) {
		if (str == null) {
			return true;
		}
		for (int i = str.length() - 1; i >= 0; i--) {
			if (str.charAt(i) != '0') {
				return false;
			}
		}
		return str.length() > 0;
	}

	// -----------------------------------------------------------------------
	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>Float</code>.
	 * </p>
	 * 
	 * <p>
	 * Returns <code>null</code> if the string is <code>null</code>.
	 * </p>
	 * 
	 * @param str
	 *            a <code>String</code> to convert, may be null
	 * @return converted <code>Float</code>
	 * @throws NumberFormatException
	 *             if the value cannot be converted
	 */
	public static Float createFloat(String str) {
		if (str == null) {
			return null;
		}
		return Float.valueOf(str);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>Double</code>.
	 * </p>
	 * 
	 * <p>
	 * Returns <code>null</code> if the string is <code>null</code>.
	 * </p>
	 * 
	 * @param str
	 *            a <code>String</code> to convert, may be null
	 * @return converted <code>Double</code>
	 * @throws NumberFormatException
	 *             if the value cannot be converted
	 */
	public static Double createDouble(String str) {
		if (str == null) {
			return null;
		}
		return Double.valueOf(str);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>Integer</code>, handling hex and
	 * octal notations.
	 * </p>
	 * 
	 * <p>
	 * Returns <code>null</code> if the string is <code>null</code>.
	 * </p>
	 * 
	 * @param str
	 *            a <code>String</code> to convert, may be null
	 * @return converted <code>Integer</code>
	 * @throws NumberFormatException
	 *             if the value cannot be converted
	 */
	public static Integer createInteger(String str) {
		if (str == null) {
			return null;
		}
		// decode() handles 0xAABD and 0777 (hex and octal) as well.
		return Integer.decode(str);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>Long</code>; since 3.1 it
	 * handles hex and octal notations.
	 * </p>
	 * 
	 * <p>
	 * Returns <code>null</code> if the string is <code>null</code>.
	 * </p>
	 * 
	 * @param str
	 *            a <code>String</code> to convert, may be null
	 * @return converted <code>Long</code>
	 * @throws NumberFormatException
	 *             if the value cannot be converted
	 */
	public static Long createLong(String str) {
		if (str == null) {
			return null;
		}
		return Long.decode(str);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>BigInteger</code>.
	 * </p>
	 * 
	 * <p>
	 * Returns <code>null</code> if the string is <code>null</code>.
	 * </p>
	 * 
	 * @param str
	 *            a <code>String</code> to convert, may be null
	 * @return converted <code>BigInteger</code>
	 * @throws NumberFormatException
	 *             if the value cannot be converted
	 */
	public static BigInteger createBigInteger(String str) {
		if (str == null) {
			return null;
		}
		return new BigInteger(str);
	}

	/**
	 * <p>
	 * Convert a <code>String</code> to a <code>BigDecimal</code>.
	 * </p>
	 * 
	 * <p>
	 * Returns <code>null</code> if the string is <code>null</code>.
	 * </p>
	 * 
	 * @param str
	 *            a <code>String</code> to convert, may be null
	 * @return converted <code>BigDecimal</code>
	 * @throws NumberFormatException
	 *             if the value cannot be converted
	 */
	public static BigDecimal createBigDecimal(String str) {
		if (str == null) {
			return null;
		}
		// handle JDK1.3.1 bug where "" throws IndexOutOfBoundsException
		if (Strings.isBlank(str)) {
			throw new NumberFormatException(
					"A blank string is not a valid number");
		}
		return new BigDecimal(str);
	}

    // Min in array
	// --------------------------------------------------------------------
	/**
	 * <p>
	 * Returns the minimum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static long min(long[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns min
		long min = array[0];
		for (int i = 1; i < array.length; i++) {
			if (array[i] < min) {
				min = array[i];
			}
		}

		return min;
	}

	/**
	 * <p>
	 * Returns the minimum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static int min(int[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns min
		int min = array[0];
		for (int j = 1; j < array.length; j++) {
			if (array[j] < min) {
				min = array[j];
			}
		}

		return min;
	}

	/**
	 * <p>
	 * Returns the minimum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static short min(short[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns min
		short min = array[0];
		for (int i = 1; i < array.length; i++) {
			if (array[i] < min) {
				min = array[i];
			}
		}

		return min;
	}

	/**
	 * <p>
	 * Returns the minimum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static byte min(byte[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns min
		byte min = array[0];
		for (int i = 1; i < array.length; i++) {
			if (array[i] < min) {
				min = array[i];
			}
		}

		return min;
	}

	/**
	 * <p>
	 * Returns the minimum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static double min(double[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns min
		double min = array[0];
		for (int i = 1; i < array.length; i++) {
			if (Double.isNaN(array[i])) {
				return Double.NaN;
			}
			if (array[i] < min) {
				min = array[i];
			}
		}

		return min;
	}

	/**
	 * <p>
	 * Returns the minimum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static float min(float[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns min
		float min = array[0];
		for (int i = 1; i < array.length; i++) {
			if (Float.isNaN(array[i])) {
				return Float.NaN;
			}
			if (array[i] < min) {
				min = array[i];
			}
		}

		return min;
	}

	// Max in array
	// --------------------------------------------------------------------
	/**
	 * <p>
	 * Returns the maximum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static long max(long[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns max
		long max = array[0];
		for (int j = 1; j < array.length; j++) {
			if (array[j] > max) {
				max = array[j];
			}
		}

		return max;
	}

	/**
	 * <p>
	 * Returns the maximum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static int max(int[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns max
		int max = array[0];
		for (int j = 1; j < array.length; j++) {
			if (array[j] > max) {
				max = array[j];
			}
		}

		return max;
	}

	/**
	 * <p>
	 * Returns the maximum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static short max(short[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns max
		short max = array[0];
		for (int i = 1; i < array.length; i++) {
			if (array[i] > max) {
				max = array[i];
			}
		}

		return max;
	}

	/**
	 * <p>
	 * Returns the maximum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static byte max(byte[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns max
		byte max = array[0];
		for (int i = 1; i < array.length; i++) {
			if (array[i] > max) {
				max = array[i];
			}
		}

		return max;
	}

	/**
	 * <p>
	 * Returns the maximum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static double max(double[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns max
		double max = array[0];
		for (int j = 1; j < array.length; j++) {
			if (Double.isNaN(array[j])) {
				return Double.NaN;
			}
			if (array[j] > max) {
				max = array[j];
			}
		}

		return max;
	}

	/**
	 * <p>
	 * Returns the maximum value in an array.
	 * </p>
	 * 
	 * @param array an array, must not be null or empty
	 * @return the minimum value in the array
	 * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
	 * @throws IllegalArgumentException if <code>array</code> is empty
	 */
	public static float max(float[] array) {
		// Validates input
		if (array == null) {
			throw new IllegalArgumentException("The Array must not be null");
		}
		else if (array.length == 0) {
			throw new IllegalArgumentException("Array cannot be empty.");
		}

		// Finds and returns max
		float max = array[0];
		for (int j = 1; j < array.length; j++) {
			if (Float.isNaN(array[j])) {
				return Float.NaN;
			}
			if (array[j] > max) {
				max = array[j];
			}
		}

		return max;
	}

	// 3 param min
	// -----------------------------------------------------------------------
	/**
	 * <p>
	 * Gets the minimum of three <code>long</code> values.
	 * </p>
	 * 
	 * @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;
	}

	/**
	 * <p>
	 * Gets the minimum of three <code>int</code> values.
	 * </p>
	 * 
	 * @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;
	}

	/**
	 * <p>
	 * Gets the minimum of three <code>short</code> values.
	 * </p>
	 * 
	 * @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;
	}

	/**
	 * <p>
	 * Gets the minimum of three <code>byte</code> values.
	 * </p>
	 * 
	 * @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;
	}

	/**
	 * <p>
	 * Gets the minimum of three <code>double</code> values.
	 * </p>
	 * <p>
	 * If any value is <code>NaN</code>, <code>NaN</code> is returned. Infinity is handled.
	 * </p>
	 * 
	 * @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);
	}

	/**
	 * <p>
	 * Gets the minimum of three <code>float</code> values.
	 * </p>
	 * <p>
	 * If any value is <code>NaN</code>, <code>NaN</code> is returned. Infinity is handled.
	 * </p>
	 * 
	 * @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);
	}

	// 3 param max
	// -----------------------------------------------------------------------
	/**
	 * <p>
	 * Gets the maximum of three <code>long</code> values.
	 * </p>
	 * 
	 * @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;
	}

	/**
	 * <p>
	 * Gets the maximum of three <code>int</code> values.
	 * </p>
	 * 
	 * @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;
	}

	/**
	 * <p>
	 * Gets the maximum of three <code>short</code> values.
	 * </p>
	 * 
	 * @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;
	}

	/**
	 * <p>
	 * Gets the maximum of three <code>byte</code> values.
	 * </p>
	 * 
	 * @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;
	}

	/**
	 * <p>
	 * Gets the maximum of three <code>double</code> values.
	 * </p>
	 * <p>
	 * If any value is <code>NaN</code>, <code>NaN</code> is returned. Infinity is handled.
	 * </p>
	 * 
	 * @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);
	}

	/**
	 * <p>
	 * Gets the maximum of three <code>float</code> values.
	 * </p>
	 * <p>
	 * If any value is <code>NaN</code>, <code>NaN</code> is returned. Infinity is handled.
	 * </p>
	 * 
	 * @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);
	}

	// -----------------------------------------------------------------------
	/**
	 * <p>
	 * Compares two <code>doubles</code> for order.
	 * </p>
	 * <p>
	 * This method is more comprehensive than the standard Java greater than, less than and equals
	 * operators.
	 * </p>
	 * <ul>
	 * <li>It returns <code>-1</code> if the first value is less than the second.</li>
	 * <li>It returns <code>+1</code> if the first value is greater than the second.</li>
	 * <li>It returns <code>0</code> if the values are equal.</li>
	 * </ul>
	 * <p>
	 * The ordering is as follows, largest to smallest:
	 * <ul>
	 * <li>NaN
	 * <li>Positive infinity
	 * <li>Maximum double
	 * <li>Normal positive numbers
	 * <li>+0.0
	 * <li>-0.0
	 * <li>Normal negative numbers
	 * <li>Minimum double (<code>-Double.MAX_VALUE</code>)
	 * <li>Negative infinity
	 * </ul>
	 * </p>
	 * <p>
	 * Comparing <code>NaN</code> with <code>NaN</code> will return <code>0</code>.
	 * </p>
	 * 
	 * @param lhs the first <code>double</code>
	 * @param rhs the second <code>double</code>
	 * @return <code>-1</code> if lhs is less, <code>+1</code> if greater, <code>0</code> 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;
		}
	}

	/**
	 * <p>
	 * Compares two floats for order.
	 * </p>
	 * <p>
	 * This method is more comprehensive than the standard Java greater than, less than and equals
	 * operators.
	 * </p>
	 * <ul>
	 * <li>It returns <code>-1</code> if the first value is less than the second.
	 * <li>It returns <code>+1</code> if the first value is greater than the second.
	 * <li>It returns <code>0</code> if the values are equal.
	 * </ul>
	 * <p>
	 * The ordering is as follows, largest to smallest:
	 * <ul>
	 * <li>NaN
	 * <li>Positive infinity
	 * <li>Maximum float
	 * <li>Normal positive numbers
	 * <li>+0.0
	 * <li>-0.0
	 * <li>Normal negative numbers
	 * <li>Minimum float (<code>-Float.MAX_VALUE</code>)
	 * <li>Negative infinity
	 * </ul>
	 * <p>
	 * Comparing <code>NaN</code> with <code>NaN</code> will return <code>0</code>.
	 * </p>
	 * 
	 * @param lhs the first <code>float</code>
	 * @param rhs the second <code>float</code>
	 * @return <code>-1</code> if lhs is less, <code>+1</code> if greater, <code>0</code> 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;
		}
	}

	// -----------------------------------------------------------------------
	/**
	 * <p>
	 * Checks whether the <code>String</code> contains only digit characters.
	 * </p>
	 * <p>
	 * <code>Null</code> and empty String will return <code>false</code>.
	 * </p>
	 * 
	 * @param str the <code>String</code> to check
	 * @return <code>true</code> if str contains only unicode numeric
	 */
	public static boolean isDigits(String str) {
		if (Strings.isEmpty(str)) {
			return false;
		}
		for (int i = 0; i < str.length(); i++) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * <p>
	 * Checks whether the String a valid Java number.
	 * </p>
	 * <p>
	 * Valid numbers include hexadecimal marked with the <code>0x</code> qualifier, scientific
	 * notation and numbers marked with a type qualifier (e.g. 123L).
	 * </p>
	 * <p>
	 * <code>Null</code> and empty String will return <code>false</code>.
	 * </p>
	 * 
	 * @param str the <code>String</code> to check
	 * @return <code>true</code> if the string is a correctly formatted number
	 */
	public static boolean isNumber(String str) {
		if (Strings.isEmpty(str)) {
			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 (chars[i] == '.') {
				if (hasDecPoint || hasExp) {
					// two decimal points or dec in exponent
					return false;
				}
				// single trailing decimal point after non-exponent is ok
				return foundDigit;
			}
			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 exponent
				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;
	}
}
