net.lucidviews.util
Class DateComparator

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

public class DateComparator
extends Object
implements Comparator

A Comparator that compares dates.

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

Field Summary
protected  NumberComparator _timestampComparator
          The number comparator that will be used to compare UTC time.
static DateComparator CHRONOLOGICAL
          A DateComparator that puts dates into chronological order.
static DateComparator REVERSE_CHRONOLOGICAL
          A DateComparator that puts dates into reverse chronological order.
 
Constructor Summary
protected DateComparator()
          The default constructor.
protected DateComparator(boolean reverse)
          Create a comparator that will sort dates.
 
Method Summary
 int compare(Date date1, Date date2)
          Compares two Dates for order.
 int compare(long timestamp1, long timestamp2)
          Compares two timestamps 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

CHRONOLOGICAL

public static final DateComparator CHRONOLOGICAL
A DateComparator that puts dates into chronological order.


REVERSE_CHRONOLOGICAL

public static final DateComparator REVERSE_CHRONOLOGICAL
A DateComparator that puts dates into reverse chronological order.


_timestampComparator

protected NumberComparator _timestampComparator
The number comparator that will be used to compare UTC time.

Constructor Detail

DateComparator

protected DateComparator()
The default constructor.
Creates a comparator that will sort dates into chronological order.


DateComparator

protected DateComparator(boolean reverse)
Create a comparator that will sort dates.

Parameters:
reverse - if set to false dates will be put in chronological order; if set to true dates will be put in reverse chronological order
Method Detail

compare

public int compare(Date date1,
                   Date date2)
Compares two Dates for order. Returns a negative integer, zero, or a positive integer as the first date 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:
date1 - the first date to be compared
date2 - the second date to be compared
Returns:
a negative integer, zero, or a positive integer as the first date is less than, equal to, or greater than the second

compare

public int compare(long timestamp1,
                   long timestamp2)
Compares two timestamps 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:
timestamp1 - the first timestamp to be compared
timestamp2 - the second timestamp to be compared
Returns:
a negative integer, zero, or a positive integer as the first timestamp 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