JTemporal Home
Tutorial

net.sf.jtemporal.spi
Class StartComparator

java.lang.Object
  extended by net.sf.jtemporal.spi.StartComparator
All Implemented Interfaces:
java.util.Comparator

public class StartComparator
extends java.lang.Object
implements java.util.Comparator

Compares Periods and Instants by using the Period start instant.
This is used by all the Storages based on a TreeMap, whose key is supposed to contain instances of Period only.
It can look surprising and somewhat hacky that the comparator simultaneously compares instances of Period and Instant, but this is required by the duality of the map key: Periods when we write, Instants when we read. Given that all the objects in the key of the map are of the same class, it does not break Comparator's contract.


Field Summary
static java.util.Comparator COMPARATOR
          The shared instance.
 
Constructor Summary
StartComparator()
          Some ORM frameworks require to instantiate new instances of the comparator, that's why this is public.
 
Method Summary
 int compare(java.lang.Object o1, java.lang.Object o2)
          If both objects are Periods, they are compared as periods otherwise they are compared as instants (taking the start instant if one is a Period).
 
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

COMPARATOR

public static final java.util.Comparator COMPARATOR
The shared instance.

Constructor Detail

StartComparator

public StartComparator()
Some ORM frameworks require to instantiate new instances of the comparator, that's why this is public.

Method Detail

compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
If both objects are Periods, they are compared as periods otherwise they are compared as instants (taking the start instant if one is a Period). It would be more OO to implement a common method in Period and in Instant, but I want to keep the Instant interface as simple as possible.

Specified by:
compare in interface java.util.Comparator

JTemporal Home
Tutorial