public class NumberOperations extends Object
Number objects
without having to cast them to narrower types.| Modifier and Type | Class and Description |
|---|---|
static class |
NumberOperations.ClassInfo
Information about the
Number classes supported including
their rank in terms of numeric precision. |
| Modifier and Type | Field and Description |
|---|---|
static double |
DEFAULT_DOUBLE_TOL
Default tolerance for double comparisons
|
static float |
DEFAULT_FLOAT_TOL
Default tolerance for float comparisons.
|
| Constructor and Description |
|---|
NumberOperations() |
| Modifier and Type | Method and Description |
|---|---|
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. |
public static final float DEFAULT_FLOAT_TOL
public static final double DEFAULT_DOUBLE_TOL
public static Number add(Number n1, Number n2)
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)n1 - first valuen2 - second valuepublic static Number subtract(Number n1, Number n2)
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)n1 - first valuen2 - second valuepublic static Number multiply(Number n1, Number n2)
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)n1 - first valuen2 - second valuepublic static Number divide(Number n1, Number n2)
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)n1 - first valuen2 - second valuepublic static int compare(Number n1, Number n2)
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.n1 - the first valuen2 - the second valuegetFloatTolerance(),
setFloatTolerance(float)public static float getFloatTolerance()
public static void setFloatTolerance(float tol)
tol - a small positive valuepublic static double getDoubleTolerance()
public static void setFloatTolerance(double tol)
tol - a small positive valuepublic static int intValue(Number number)
int value. The following conventions apply:
number - the object to convertintpublic static long longValue(Number number)
long value. The following conventions apply:
number - the object to convertlongpublic static float floatValue(Number number)
float value. The following conventions apply:
number - the object to convertfloatpublic static double doubleValue(Number number)
double value. The only difference
between this method and Number.doubleValue() is that Byte values
are handled as unsigned (e.g. range 0 - 255).number - the object to convertdoublepublic static Number copy(Number number)
Number object. This is a short-cut for
NumberOperations.newInstance(number, number.getClass()).
Returns null if the input is null.number - the number to copynumber;
or nullpublic static Number newInstance(Number number, Class<? extends Number> clazz)
clazz taking its value from numbernumber - the number object whose value will be copiedclazz - the class of the new instancenumberpublic static Number castNumber(Number number, Class<? extends Number> clazz)
number to the class clazznumber - the number object whose value will be copiedclazz - the class of the new instancenumberpublic static Class<? extends Number> highestClass(Number... numbers)
numbers - Number objects to comparenull if no arguments were suppliedCopyright © 2009–2020. All rights reserved.