org.jaitools.numeric
Class NumberOperations

java.lang.Object
  extended by org.jaitools.numeric.NumberOperations

public class NumberOperations
extends Object

Provides static methods to work directly with Number objects without having to cast them to narrower types.

Since:
1.1
Version:
$Id$
Author:
Michael Bedward

Nested Class Summary
static class NumberOperations.ClassInfo
          Information about the Number classes supported including their rank in terms of numeric precision.
 
Field Summary
static double DEFAULT_DOUBLE_TOL
          Default tolerance for double comparisons
static float DEFAULT_FLOAT_TOL
          Default tolerance for float comparisons.
 
Constructor Summary
NumberOperations()
           
 
Method Summary
static Number add(Number n1, Number n2)
          Adds two Number objects.
static Number castNumber(Number number, Class<? extends Number> clazz)
          Cast the value from number to the class clazz
static int compare(Number n1, Number n2)
          Compares value n1 to value n2.
static Number copy(Number number)
          Copies the given Number object.
static Number divide(Number n1, Number n2)
          Calculates n2 / n1.
static double doubleValue(Number number)
          Safely convert the argument to a double value.
static float floatValue(Number number)
          Safely convert the argument to a float value.
static double getDoubleTolerance()
          Gets the current tolerance used for Double comparisons.
static float getFloatTolerance()
          Gets the current tolerance used for Float comparisons.
static Class<? extends Number> highestClass(Number... numbers)
          Determine the highest ranking class (in terms of numeric precision) among the arguments
static int intValue(Number number)
          Safely convert the argument to an int value.
static long longValue(Number number)
          Safely convert the argument to a long value.
static Class<? extends Number> lowestClass(Number... numbers)
          Determine the lowest ranking class (in terms of numeric precision) among the arguments
static Number multiply(Number n1, Number n2)
          Calculates n2 * n1.
static Number newInstance(Number number, Class<? extends Number> clazz)
          Return a new instance of class clazz taking its value from number
static void setFloatTolerance(double tol)
          Sets the tolerance used for Double comparisons.
static void setFloatTolerance(float tol)
          Sets the tolerance used for Float comparisons.
static Number subtract(Number n1, Number n2)
          Calculates n2 - n1.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_FLOAT_TOL

public static final float DEFAULT_FLOAT_TOL
Default tolerance for float comparisons.

See Also:
Constant Field Values

DEFAULT_DOUBLE_TOL

public static final double DEFAULT_DOUBLE_TOL
Default tolerance for double comparisons

See Also:
Constant Field Values
Constructor Detail

NumberOperations

public NumberOperations()
Method Detail

add

public static Number add(Number n1,
                         Number n2)
Adds two Number objects. The return value will be an instance of the highest ranking class of the two arguments (e.g. Byte + Integer will give an Integer result)

Parameters:
n1 - first value
n2 - second value
Returns:
the result as a new instance of the highest ranking argument class

subtract

public static Number subtract(Number n1,
                              Number n2)
Calculates n2 - n1. The return value will be an instance of the highest ranking class of the two arguments (e.g. Byte - Integer will give an Integer result)

Parameters:
n1 - first value
n2 - second value
Returns:
the result as a new instance of the highest ranking argument class

multiply

public static Number multiply(Number n1,
                              Number n2)
Calculates n2 * n1. The return value will be an instance of the highest ranking class of the two arguments (e.g. Byte * Integer will give an Integer result)

Parameters:
n1 - first value
n2 - second value
Returns:
the result as a new instance of the highest ranking argument class

divide

public static Number divide(Number n1,
                            Number n2)
Calculates n2 / n1. The return value will be an instance of the highest ranking class of the two arguments (e.g. Byte / Integer will give an Integer result)

Parameters:
n1 - first value
n2 - second value
Returns:
the result as a new instance of the highest ranking argument class

compare

public static int compare(Number n1,
                          Number n2)
Compares value n1 to value n2. If one or both of the values are Float or Double the comparison is done within the currently set float or double tolerance.

Parameters:
n1 - the first value
n2 - the second value
Returns:
-1 if the first value is less than the second; 1 if the first value is greater than the second; 0 if the two values are equal.
See Also:
getFloatTolerance(), setFloatTolerance(float)

getFloatTolerance

public static float getFloatTolerance()
Gets the current tolerance used for Float comparisons.

Returns:
the current tolerance

setFloatTolerance

public static void setFloatTolerance(float tol)
Sets the tolerance used for Float comparisons.

Parameters:
tol - a small positive value

getDoubleTolerance

public static double getDoubleTolerance()
Gets the current tolerance used for Double comparisons.

Returns:
the current tolerance

setFloatTolerance

public static void setFloatTolerance(double tol)
Sets the tolerance used for Double comparisons.

Parameters:
tol - a small positive value

intValue

public static int intValue(Number number)
Safely convert the argument to an int value. The following conventions apply:

Parameters:
number - the object to convert
Returns:
the object value as an int

longValue

public static long longValue(Number number)
Safely convert the argument to a long value. The following conventions apply:

Parameters:
number - the object to convert
Returns:
the object value as a long

floatValue

public static float floatValue(Number number)
Safely convert the argument to a float value. The following conventions apply:

Parameters:
number - the object to convert
Returns:
the object value as a float

doubleValue

public static double doubleValue(Number number)
Safely convert the argument to a double value. The only difference between this method and Number.doubleValue() is that Byte values are handled as unsigned (e.g. range 0 - 255).

Parameters:
number - the object to convert
Returns:
the object value as a double

copy

public static Number copy(Number number)
Copies the given Number object. This is a short-cut for NumberOperations.newInstance(number, number.getClass()). Returns null if the input is null.

Parameters:
number - the number to copy
Returns:
a new instance with the same class and value as number; or null

newInstance

public static Number newInstance(Number number,
                                 Class<? extends Number> clazz)
Return a new instance of class clazz taking its value from number

Parameters:
number - the number object whose value will be copied
clazz - the class of the new instance
Returns:
a new instance of the request class with value number

castNumber

public static Number castNumber(Number number,
                                Class<? extends Number> clazz)
Cast the value from number to the class clazz

Parameters:
number - the number object whose value will be copied
clazz - the class of the new instance
Returns:
a new instance of the request class with value number

highestClass

public static Class<? extends Number> highestClass(Number... numbers)
Determine the highest ranking class (in terms of numeric precision) among the arguments

Parameters:
numbers - Number objects to compare
Returns:
the highest ranking class, or null if no arguments were supplied

lowestClass

public static Class<? extends Number> lowestClass(Number... numbers)
Determine the lowest ranking class (in terms of numeric precision) among the arguments

Parameters:
numbers - Number objects to compare
Returns:
the lowest ranking class, or null if no arguments were supplied


Copyright © 2009-2013. All Rights Reserved.