net.sf.morph.util
Class NumberUtils

java.lang.Object
  extended by net.sf.morph.util.NumberUtils

public abstract class NumberUtils
extends java.lang.Object

Various values and utility functions that are useful when working with numbers.

Since:
Dec 15, 2004
Author:
Matt Sgarlata

Field Summary
protected static java.lang.Class[] BASE_NUMBER_TYPES
          Used by isNumber(Class).
static java.math.BigDecimal MAX_BYTE
          The maximum value a byte can have.
static java.math.BigDecimal MAX_DOUBLE
          The maximum value a double can have.
static java.math.BigDecimal MAX_FLOAT
          The maximum value a float can have.
static java.math.BigDecimal MAX_INTEGER
          The maximum value an integer can have.
static java.math.BigDecimal MAX_LONG
          The maximum value a long can have.
static java.math.BigDecimal MAX_SHORT
          The maximum value a short can have.
static java.util.Map MAXIMUMS_FOR_TYPES
          A Map of BigDecimals keyed by Class that indicate the maximum value that the given (Number) Class may taken on.
static java.math.BigDecimal MIN_BYTE
          The minimum value a byte can have.
static java.math.BigDecimal MIN_DOUBLE
          The minimum value a double can have.
static java.math.BigDecimal MIN_FLOAT
          The minimum value a float can have.
static java.math.BigDecimal MIN_INTEGER
          The minimum value an integer can have.
static java.math.BigDecimal MIN_LONG
          The minimum value a long can have.
static java.math.BigDecimal MIN_SHORT
          The minimum value a short can have.
static java.util.Map MINIMUMS_FOR_TYPES
          A Map of BigDecimals keyed by Class that indicate the minimum value that the given (Number) Class may taken on.
static java.util.Comparator NARROWNESS_COMPARATOR
          Comparator of class/object type narrowness.
static java.math.BigDecimal ZERO
          A BigDecimal containing the value zero (0).
 
Constructor Summary
NumberUtils()
           
 
Method Summary
static java.math.BigDecimal getMaximumForType(java.lang.Class type)
          Returns the maximum allowed value for the given type, which must be a number.
static java.math.BigDecimal getMinimumForType(java.lang.Class type)
          Returns the minimum allowed value for the given type, which must be a number.
static java.lang.Number getNumber(java.lang.Class type, java.lang.String s)
          Get a number from a String.
static java.lang.Class getWrapperForPrimitiveType(java.lang.Class type)
          Deprecated. for ClassUtils.getPrimitiveWrapper(Class)
static boolean isDecimal(java.lang.Number number)
          Returns true if number is capable of containing a decimal (fractional) component.
static boolean isNumber(java.lang.Class type)
          Determines if the given type is a number type.
protected static boolean isOutOfBoundsForType(java.util.Map boundMap, java.lang.Number number, java.lang.Class type, int badCompareToResult)
          Implementation of isTooBigForType and isTooSmallForType
static boolean isOutOfBoundsForType(java.lang.Number number, java.lang.Class type)
          Returns true if number has too large an absolute value to be stored in an instance of the given type.
static boolean isTooBigForType(java.lang.Number number, java.lang.Class type)
          Returns true if number represents a value too large to be stored in an instance of the given type.
static boolean isTooSmallForType(java.lang.Number number, java.lang.Class type)
          Returns true if number represents a value too small (i.e.
static java.math.BigDecimal numberToBigDecimal(java.lang.Number number)
          Converts the given number to a BigDecimal.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXIMUMS_FOR_TYPES

public static final java.util.Map MAXIMUMS_FOR_TYPES
A Map of BigDecimals keyed by Class that indicate the maximum value that the given (Number) Class may taken on.


MINIMUMS_FOR_TYPES

public static final java.util.Map MINIMUMS_FOR_TYPES
A Map of BigDecimals keyed by Class that indicate the minimum value that the given (Number) Class may taken on.


NARROWNESS_COMPARATOR

public static final java.util.Comparator NARROWNESS_COMPARATOR
Comparator of class/object type narrowness.


BASE_NUMBER_TYPES

protected static final java.lang.Class[] BASE_NUMBER_TYPES
Used by isNumber(Class).


MAX_LONG

public static final java.math.BigDecimal MAX_LONG
The maximum value a long can have.

See Also:
Long.MAX_VALUE

MAX_INTEGER

public static final java.math.BigDecimal MAX_INTEGER
The maximum value an integer can have.

See Also:
Integer.MAX_VALUE

MAX_SHORT

public static final java.math.BigDecimal MAX_SHORT
The maximum value a short can have.

See Also:
Short.MAX_VALUE

MAX_BYTE

public static final java.math.BigDecimal MAX_BYTE
The maximum value a byte can have.

See Also:
Byte.MAX_VALUE

MAX_DOUBLE

public static final java.math.BigDecimal MAX_DOUBLE
The maximum value a double can have.

See Also:
Double.MAX_VALUE

MAX_FLOAT

public static final java.math.BigDecimal MAX_FLOAT
The maximum value a float can have.

See Also:
Float.MAX_VALUE

MIN_LONG

public static final java.math.BigDecimal MIN_LONG
The minimum value a long can have.

See Also:
Long.MIN_VALUE

MIN_INTEGER

public static final java.math.BigDecimal MIN_INTEGER
The minimum value an integer can have.

See Also:
Long.MIN_VALUE

MIN_SHORT

public static final java.math.BigDecimal MIN_SHORT
The minimum value a short can have.

See Also:
Long.MIN_VALUE

MIN_BYTE

public static final java.math.BigDecimal MIN_BYTE
The minimum value a byte can have.

See Also:
Long.MIN_VALUE

MIN_DOUBLE

public static final java.math.BigDecimal MIN_DOUBLE
The minimum value a double can have.

See Also:
Long.MIN_VALUE

MIN_FLOAT

public static final java.math.BigDecimal MIN_FLOAT
The minimum value a float can have.

See Also:
Long.MIN_VALUE

ZERO

public static final java.math.BigDecimal ZERO
A BigDecimal containing the value zero (0).

Constructor Detail

NumberUtils

public NumberUtils()
Method Detail

getMaximumForType

public static java.math.BigDecimal getMaximumForType(java.lang.Class type)
Returns the maximum allowed value for the given type, which must be a number.

Parameters:
type - the type
Returns:
the maximum allowed value for the given type, if type is a number or
null, otherwise

getMinimumForType

public static java.math.BigDecimal getMinimumForType(java.lang.Class type)
Returns the minimum allowed value for the given type, which must be a number.

Parameters:
type - the type
Returns:
the minimum allowed value for the given type, if type is a number or
null, otherwise

numberToBigDecimal

public static java.math.BigDecimal numberToBigDecimal(java.lang.Number number)
Converts the given number to a BigDecimal.

Parameters:
number - the number to convert
Returns:
null, if number is null or
the given number as a BigDecimal, otherwise

isNumber

public static boolean isNumber(java.lang.Class type)
Determines if the given type is a number type. A number type is any type that is a subclass of java.lang.Number or is a primitive number type.

Parameters:
type - the type to test
Returns:
true if the given type is a number type or false, otherwise

isDecimal

public static boolean isDecimal(java.lang.Number number)
Returns true if number is capable of containing a decimal (fractional) component. This method returns true for Floats, Doubles, and Longs.

Parameters:
number - the number to test
Returns:
false, if number is null or
true if number is capable of containing a decimal (fractional) component or
false, otherwise.

isOutOfBoundsForType

protected static boolean isOutOfBoundsForType(java.util.Map boundMap,
                                              java.lang.Number number,
                                              java.lang.Class type,
                                              int badCompareToResult)
Implementation of isTooBigForType and isTooSmallForType


isTooBigForType

public static boolean isTooBigForType(java.lang.Number number,
                                      java.lang.Class type)
Returns true if number represents a value too large to be stored in an instance of the given type.

Parameters:
number - the number to test
the - type to test
Returns:
true, if number represents a value too large to be stored in an instance of the given type or
false, otherwise
Throws:
java.lang.IllegalArgumentException - if the maximum value cannot be determined for the given type

isTooSmallForType

public static boolean isTooSmallForType(java.lang.Number number,
                                        java.lang.Class type)
Returns true if number represents a value too small (i.e. - with too large a negative absolute value) to be stored in an instance of the given type.

Parameters:
number - the number to test
the - type to test
Returns:
true, if number represents a value too small (i.e. - with too large a negative absolute value) to be stored in an instance of the given type or
false, otherwise
Throws:
java.lang.IllegalArgumentException - if the minimum value cannot be determined for the given type

isOutOfBoundsForType

public static boolean isOutOfBoundsForType(java.lang.Number number,
                                           java.lang.Class type)
Returns true if number has too large an absolute value to be stored in an instance of the given type.

Parameters:
number - the number to test
the - type to test
Returns:
true, if number has too large an absolute value to be stored in an instance of the given type or
false, otherwise

getWrapperForPrimitiveType

public static java.lang.Class getWrapperForPrimitiveType(java.lang.Class type)
Deprecated. for ClassUtils.getPrimitiveWrapper(Class)

Parameters:
type -
Returns:
Class

getNumber

public static java.lang.Number getNumber(java.lang.Class type,
                                         java.lang.String s)
                                  throws java.lang.Exception
Get a number from a String.

Parameters:
type -
s -
Returns:
Number
Throws:
java.lang.Exception


Copyright © 2004-2008.