net.lucidviews.util
Class NumberComparator

java.lang.Object
  extended by net.lucidviews.util.NumberComparator
All Implemented Interfaces:
Comparator

public class NumberComparator
extends Object
implements Comparator

A Comparator that compares numbers.

This class provides various compare methods, a series which cascades down from generic type to the specific number type
Object ==> Number ==> long or double

Since:
1.0
Version:
$Revision: 1.1 $
Author:
Stephen Battey
See Also:
Comparator

Field Summary
protected  boolean _descending
          Indicates if the ordering of numbers should be reversed, thus sorting numbers into descending order.
static NumberComparator ASCENDING
          A NumberComparator to sort numbers lowest to highest.
static NumberComparator DESCENDING
          A NumberComparator to sort numbers highest to lowest.
 
Constructor Summary
protected NumberComparator()
          The default constructor.
protected NumberComparator(boolean descending)
          Create a comparator that will sort numbers.
 
Method Summary
 int compare(double number1, double number2)
          Compares two floating-point numbers for order.
 int compare(long number1, long number2)
          Compares two fixed-point numbers for order.
 int compare(Number number1, Number number2)
          Compares two Numbers for order.
 int compare(Object o1, Object o2)
           
 boolean equals(Object obj)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ASCENDING

public static final NumberComparator ASCENDING
A NumberComparator to sort numbers lowest to highest.


DESCENDING

public static final NumberComparator DESCENDING
A NumberComparator to sort numbers highest to lowest.


_descending

protected boolean _descending
Indicates if the ordering of numbers should be reversed, thus sorting numbers into descending order. Descending is considered to be 'reverse' since when this flag is set the values returned by the compare method will be flipped - a positive value (which usually indicates a bigger number) will instead indicate a smaller number.

Constructor Detail

NumberComparator

protected NumberComparator()
The default constructor.
Creates a comparator that will sort numbers in ascending order.


NumberComparator

protected NumberComparator(boolean descending)
Create a comparator that will sort numbers.

Parameters:
descending - if set to false numbers will be in ascending order; if set to true files will be sorted in descending order
Method Detail

compare

public int compare(Number number1,
                   Number number2)
Compares two Numbers for order. Returns a negative integer, zero, or a positive integer as the first number is less than, equal to, or greater than the second.

This implementation ensures that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

This implementation also ensures that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

This implementation also ensures that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.

Finally, this implementation also ensures that (compare(x, y)==0) == (x.equals(y)).

Parameters:
number1 - the first number to be compared
number2 - the second number to be compared
Returns:
a negative integer, zero, or a positive integer as the first number is less than, equal to, or greater than the second

compare

public int compare(double number1,
                   double number2)
Compares two floating-point numbers for order. Returns a negative integer, zero, or a positive integer as the first number is less than, equal to, or greater than the second.

This implementation ensures that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

This implementation also ensures that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

This implementation also ensures that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.

Finally, this implementation also ensures that (compare(x, y)==0) == (x.equals(y)).

Parameters:
number1 - the first number to be compared
number2 - the second number to be compared
Returns:
a negative integer, zero, or a positive integer as the first number is less than, equal to, or greater than the second

compare

public int compare(long number1,
                   long number2)
Compares two fixed-point numbers for order. Returns a negative integer, zero, or a positive integer as the first number is less than, equal to, or greater than the second.

This implementation ensures that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

This implementation also ensures that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

This implementation also ensures that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.

Finally, this implementation also ensures that (compare(x, y)==0) == (x.equals(y)).

Parameters:
number1 - the first number to be compared
number2 - the second number to be compared
Returns:
a negative integer, zero, or a positive integer as the first number is less than, equal to, or greater than the second

compare

public int compare(Object o1,
                   Object o2)
Specified by:
compare in interface Comparator

equals

public boolean equals(Object obj)
Specified by:
equals in interface Comparator
Overrides:
equals in class Object