// (c) MIT 2003.  All rights reserved.

// -*-Mode: JDE-*- 
// $Id: IntervalComparator.java,v 1.3 2003/03/06 00:40:49 moltmans Exp $
// IntervalComparator.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Tue Jun 13 19:13:27 2000> 
// Time-stamp: <2000-07-12 14:52:11 moltmans> 
package rec.annotate;

import java.util.*;
import rec.util.*;

public class IntervalComparator implements Comparator
{

  public int compare( Object o1, Object o2 )
  {
    return compare( (Interval)o1, (Interval)o2 );
  }

  /**
   * An interval comes first if it starts before the other one.  If
   * start times are equal, then they are equal if it ends first.  If
   * that is equal too, then by hashcode.  If still equal i1 is
   * arbitrarily picked as the smaller.
   **/
  public int compare( Interval i1, Interval i2 )
  {
    if( i1.getStartTime() < i2.getStartTime() ) {
      return -1;
    }
    if( i1.getStartTime() == i2.getStartTime() &&
	i1.getEndTime() == i2.getEndTime() )
    {
      if( i1.equals( i2 ) ) {
	return 0;
      }
      if( i1.hashCode() < i2.hashCode() ) {
	return -1;	  
      }
      if( i1.hashCode() > i2.hashCode() ) { 
	return 1;
      }
      else {
	// arbitrary hopefully not too frequent for things
	// to have the same start, end, and hash code
	Assert.note( "Trouble comparing intervals: " + i1 + " " + i2 );
	return -1; 
      }
    }
    if( i1.getEndTime() < i2.getEndTime() ) {
      return -1;
    }
    else {
      return 1;
    }
  }
}

