import java.util.*;
/**
 * A class that implements the java.util.Comparator interface to compare Items
 * based on rank
 *
 * @author NIST-ITL-IAD-Retrieval Group
 * This software was produced by NIST, an agency of the
 * U.S. government, and by statute is not subject to copyright in the
 * United States. Recipients of this software assume all
 * responsibilities associated with its operation, modification and
 * maintenance.
 *
 */
public class RankComparator implements Comparator{

    /**
     * The unique instance of this class.
     *
     * @see #getInstance()
     */
    private static RankComparator myInstance = null;    
    /**
     * Private constructor.
     *
     */
    private RankComparator(){
    }
    /**
     * Returns the unique instance of this class.
     *
     */
    public static RankComparator getInstance(){
		if(myInstance == null)
	    	myInstance = new RankComparator();
		return myInstance;
    }    
    /**
     * Compares its two arguments for ordering them on rank.
     *
     * @return    a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
     * @exception ClassCastException Thrown if one of the arguments is not a transition instance.
     */
	public int compare(Object o1, Object o2) throws ClassCastException{
		if((o1 instanceof SystemItem) && (o2 instanceof SystemItem)){
			int toReturn = -1;
			SystemItem t1 = (SystemItem) o1;
			SystemItem t2 = (SystemItem) o2;
						
			if(t1.getSrc().equals(t2.getSrc()) &&
				(t1.getMilliStart() == t2.getMilliStart()) &&
				(t1.getMilliStop() == t2.getMilliStop()) ){
				toReturn = 0;
			}
			else{
				toReturn = Integer.valueOf(t1.getSeqNum()).compareTo(Integer.valueOf(t2.getSeqNum()));			
			}
			return toReturn;
		}
		else throw new ClassCastException("Instance Problem for RankComparator");
	}
}
