import java.util.*;

/**
 * A class that implements the java.util.Comparator interface to compare two transitions regarding the overlaping of the longer.
 * Used to compare a CUT transition compared with a short gradual transition.
 *
 * @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 ApproximateComparator implements Comparator{
    
    /**
     * The unique instance of this class.
     *
     * @see #getInstance()
     */
    private static ApproximateComparator myInstance = null;
	/**
     * The required overlapping pourcentage of the longer transition to consider the two transitions as equal.
     *
     * @see #setRone()
     */
    private static double rone = 0.199999;
	/**
     * Private constructor.
     *
     */
    private ApproximateComparator(){}
    /**
     * Private constructor with overlapping parameter.
     *
     */
    private ApproximateComparator(double aRone){ rone = aRone; }
	/**
     * Sets the parameter.
     *
     */
	public static void setRone(double aRone){rone = aRone;System.out.println("Rone=="+rone);}
	/**
     * Gets the parameter.
     *
     */
	public static double getRone(){ return rone;}
    
    /**
     * Returns the unique instance of this class and set the parameter.
     *
     */
    public static ApproximateComparator getInstance(double rone){
		if(myInstance == null)
	    	myInstance = new ApproximateComparator(rone);
		return myInstance;
    }   
    /**
     * Returns the unique instance of this class with default parameter value.
     *
     */ 
    public static ApproximateComparator getInstance(){
		if(myInstance == null)
	    	myInstance = new ApproximateComparator();
		return myInstance;
    }    
	/**
     * Compares its two arguments for ordering them. Uses the longer overlapping parameter to check the equality.
     *
     * @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 Transition) && (o2 instanceof Transition)){

			Transition longerTran = (Transition)o1;
			Transition shorterTran = (Transition)o2;
			if(longerTran.length() < shorterTran.length()){
				longerTran = shorterTran;
				shorterTran = (Transition)o1;
			}
			Transition intersectionTran = longerTran.intersection(shorterTran);
			//Equality test according to this comparator
			if(intersectionTran.length() >= (rone*longerTran.length())){
				return 0;
			}
			else{
				if(shorterTran.equals(o1) && (shorterTran.pre <= longerTran.pre)) return -1;
				else return 1;
			}
		}
		else throw new ClassCastException("Instance Problem for OverlapComparator");
	}

}//End class