net.sf.molae.pipe.number
Class NumberComparator

java.lang.Object
  extended by net.sf.molae.pipe.number.NumberComparator
All Implemented Interfaces:
Comparator<Number>

public final class NumberComparator
extends Object
implements Comparator<Number>

This class provides a means to compare numbers of different types.


Field Summary
static Double NAN_DOUBLE
          Double.NaN as a Double.
static Float NAN_FLOAT
          Float.NaN as a Float.
static Double NEGATIVE_INFINITY_DOUBLE
          Double.NEGATIVE_INFINITY as a Double.
static Float NEGATIVE_INFINITY_FLOAT
          Float.NEGATIVE_INFINITY as a Float.
static Double POSITIVE_INFINITY_DOUBLE
          Double.POSITIVE_INFINITY as a Double.
static Float POSITIVE_INFINITY_FLOAT
          Float.POSITIVE_INFINITY as a Float.
 
Method Summary
 int compare(Class<? extends Number> type, Number n1, Number n2)
          Converts two numbers to the specified type and compares them.
 int compare(Number n1, Number n2)
          Compares two numbers regardless of their specific type.
static Number convertTo(Class<? extends Number> type, Number input)
          Converts the specified number into an object of the specified class.
static AtomicInteger convertToAtomicInteger(Number input)
          Converts the specified number to a AtomicInteger
static AtomicLong convertToAtomicLong(Number input)
          Converts the specified number to a AtomicLong
static BigDecimal convertToBigDecimal(Number input)
          Converts the specified number to a BigDecimal
static BigInteger convertToBigInteger(Number input)
          Converts the specified number to a BigInteger
static Byte convertToByte(Number input)
          Converts the specified number to a Byte
static Double convertToDouble(Number input)
          Converts the specified number to a Double
static Float convertToFloat(Number input)
          Converts the specified number to a Float
static Integer convertToInteger(Number input)
          Converts the specified number to a Integer
static Long convertToLong(Number input)
          Converts the specified number to a Long
static Short convertToShort(Number input)
          Converts the specified number to a Short
static NumberComparator getInstance()
          Returns the singleton instance of this comparator.
static boolean isInfinite(Number num)
          Checks if the specified number is infinite (positive or negative).
static boolean isMorePreciseThanOrEquals(Class<? extends Number> a, Class<? extends Number> b)
          Checks if the value set of the first parameter contains the value set of the second parameter.
static boolean isNegativeInfinity(Number num)
          Checks if the specified number is negative infinite.
static boolean isNotANumber(Number num)
          Checks if the specified number is a number.
static boolean isPositiveInfinity(Number num)
          Checks if the specified number is positive infinite.
static Class<? extends Number> leastCommonType(Class<? extends Number> c1, Class<? extends Number> c2)
          Returns the least precise class lct so that lct is more precise than each of the specified classes.
static byte max(byte a, byte b)
          Returns the greater of two byte values.
static Number max(Number n1, Number n2)
          Compares the two specified numbers and returns the greater one.
static short max(short a, short b)
          Returns the greater of two short values.
static byte min(byte a, byte b)
          Returns the smaller of two byte values.
static Number min(Number n1, Number n2)
          Compares the two specified numbers and returns the smaller one.
static short min(short a, short b)
          Returns the smaller of two short values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Field Detail

NEGATIVE_INFINITY_FLOAT

public static final Float NEGATIVE_INFINITY_FLOAT
Float.NEGATIVE_INFINITY as a Float.


NEGATIVE_INFINITY_DOUBLE

public static final Double NEGATIVE_INFINITY_DOUBLE
Double.NEGATIVE_INFINITY as a Double.


POSITIVE_INFINITY_FLOAT

public static final Float POSITIVE_INFINITY_FLOAT
Float.POSITIVE_INFINITY as a Float.


POSITIVE_INFINITY_DOUBLE

public static final Double POSITIVE_INFINITY_DOUBLE
Double.POSITIVE_INFINITY as a Double.


NAN_DOUBLE

public static final Double NAN_DOUBLE
Double.NaN as a Double.


NAN_FLOAT

public static final Float NAN_FLOAT
Float.NaN as a Float.

Method Detail

getInstance

public static NumberComparator getInstance()
Returns the singleton instance of this comparator.

Returns:
The singleton instance of this comparator

isMorePreciseThanOrEquals

public static boolean isMorePreciseThanOrEquals(Class<? extends Number> a,
                                                Class<? extends Number> b)
Checks if the value set of the first parameter contains the value set of the second parameter. The infinity and NaN constants of Float and Double are not regarded. Therefore Float and Double fit into BigDecimal. If one of the classes is an unkown Number subclass (not in java.lang or java.math), false is returned. The method follows the following rules:
 BigDecimal >                         Double > Float
     V                                  V        V
 BigInteger > (Atomic)Long > (Atomic)Integer > Short > Byte
 
It should not be used with other classes.

Parameters:
a - the first operand of the comparison
b - the second operand of the comparison
Returns:
true, iff all values of class b are also members of class a.
Throws:
IllegalClassException - if either parameter is not assignable to Number.

leastCommonType

public static Class<? extends Number> leastCommonType(Class<? extends Number> c1,
                                                      Class<? extends Number> c2)
Returns the least precise class lct so that lct is more precise than each of the specified classes. For definition of "more precise than" see isMorePreciseThanOrEquals(Class, Class). Note that if either class is unknown (not in the java.lang or java.math or java.util.concurrent.atomic package), BigDecimal is returned.

Parameters:
c1 - first class to compare
c2 - second class to compare
Returns:
The least precise class lct so that lct is more precise than each of the specified classes.
Throws:
IllegalClassException - if either parameter is not assignable to Number.

isNegativeInfinity

public static boolean isNegativeInfinity(Number num)
Checks if the specified number is negative infinite.

Parameters:
num - number to check
Returns:
true, iff the specified number is Float.NEGATIVE_INFINITY or Double.NEGATIVE_INFINITY.

isPositiveInfinity

public static boolean isPositiveInfinity(Number num)
Checks if the specified number is positive infinite.

Parameters:
num - number to check
Returns:
true, iff the specified number is Float.POSITIVE_INFINITY or Double.POSITIVE_INFINITY.

isInfinite

public static boolean isInfinite(Number num)
Checks if the specified number is infinite (positive or negative).

Parameters:
num - number to check
Returns:
true, iff the specified number is negative infinite or positive infinite.

isNotANumber

public static boolean isNotANumber(Number num)
Checks if the specified number is a number.

Parameters:
num - number to check
Returns:
true, iff the specified number is Float.NaN or Double.NaN.

convertToByte

public static Byte convertToByte(Number input)
Converts the specified number to a Byte

Parameters:
input - the value the return value should have
Returns:
the specified number as a Byte
Since:
2.0

convertToShort

public static Short convertToShort(Number input)
Converts the specified number to a Short

Parameters:
input - the value the return value should have
Returns:
the specified number as a Short
Since:
2.0

convertToInteger

public static Integer convertToInteger(Number input)
Converts the specified number to a Integer

Parameters:
input - the value the return value should have
Returns:
the specified number as a Integer
Since:
2.0

convertToLong

public static Long convertToLong(Number input)
Converts the specified number to a Long

Parameters:
input - the value the return value should have
Returns:
the specified number as a Long
Since:
2.0

convertToFloat

public static Float convertToFloat(Number input)
Converts the specified number to a Float

Parameters:
input - the value the return value should have
Returns:
the specified number as a Float
Since:
2.0

convertToDouble

public static Double convertToDouble(Number input)
Converts the specified number to a Double

Parameters:
input - the value the return value should have
Returns:
the specified number as a Double
Since:
2.0

convertToBigInteger

public static BigInteger convertToBigInteger(Number input)
Converts the specified number to a BigInteger

Parameters:
input - the value the return value should have
Returns:
the specified number as a BigInteger
Since:
2.0

convertToBigDecimal

public static BigDecimal convertToBigDecimal(Number input)
Converts the specified number to a BigDecimal

Parameters:
input - the value the return value should have
Returns:
the specified number as a BigDecimal
Since:
2.0

convertToAtomicInteger

public static AtomicInteger convertToAtomicInteger(Number input)
Converts the specified number to a AtomicInteger

Parameters:
input - the value the return value should have
Returns:
the specified number as a AtomicInteger
Since:
2.0

convertToAtomicLong

public static AtomicLong convertToAtomicLong(Number input)
Converts the specified number to a AtomicLong

Parameters:
input - the value the return value should have
Returns:
the specified number as a AtomicLong
Since:
2.0

convertTo

public static Number convertTo(Class<? extends Number> type,
                               Number input)
Converts the specified number into an object of the specified class.

Parameters:
type - the desired class of the return value
input - the value the return value should have
Returns:
a number of the specified class with the value of the specified number
Throws:
IllegalClassException - if the specified class is null, not a number or not known.

compare

public int compare(Class<? extends Number> type,
                   Number n1,
                   Number n2)
Converts two numbers to the specified type and compares them.

Parameters:
n1 - the first operand of the comparison
n2 - the second operand of the comparison
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Throws:
IllegalClassException - if the specified class does not implement the Comparable interface or is not implemented.
Since:
2.0

compare

public int compare(Number n1,
                   Number n2)
Compares two numbers regardless of their specific type.

Specified by:
compare in interface Comparator<Number>
Parameters:
n1 - the first operand of the comparison
n2 - the second operand of the comparison
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Throws:
ClassCastException - if the first number does not implement the Comparable interface.

max

public static short max(short a,
                        short b)
Returns the greater of two short values. That is, the result is the argument closer to the value of Short.MAX_VALUE. If the arguments have the same value, the result is that same value.

Parameters:
a - a short value.
b - a short value.
Returns:
the larger of a and b.

max

public static byte max(byte a,
                       byte b)
Returns the greater of two byte values. That is, the result is the argument closer to the value of Byte.MAX_VALUE. If the arguments have the same value, the result is that same value.

Parameters:
a - a byte value.
b - a byte value.
Returns:
the larger of a and b.

min

public static short min(short a,
                        short b)
Returns the smaller of two short values. That is, the result is the argument closer to the value of Short.MIN_VALUE. If the arguments have the same value, the result is that same value.

Parameters:
a - a short value.
b - a short value.
Returns:
the smaller of a and b.

min

public static byte min(byte a,
                       byte b)
Returns the smaller of two byte values. That is, the result is the argument closer to the value of Byte.MIN_VALUE. If the arguments have the same value, the result is that same value.

Parameters:
a - a byte value.
b - a byte value.
Returns:
the smaller of a and b.

max

public static Number max(Number n1,
                         Number n2)
Compares the two specified numbers and returns the greater one.

Parameters:
n1 - first number to compare
n2 - second number to compare
Returns:
The maximum of n1 and n2. If both are equal, n1 is returned.
Throws:
ClassCastException - if the first number does not implement the Comparable interface.

min

public static Number min(Number n1,
                         Number n2)
Compares the two specified numbers and returns the smaller one.

Parameters:
n1 - first number to compare
n2 - second number to compare
Returns:
The minimum of n1 and n2. If both are equal, n1 is returned.
Throws:
ClassCastException - if the first number does not implement the Comparable interface.