import java.util.*;

/**
 * A class that implements the java.util.Comparator interface to compare two transitions regarding their overlapping.
 * You can set two parameters RMax and RMin representing the minimal pourcentages of overlapping of respectively the
 * the shorter and the longer 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 OverlapComparator implements Comparator{

	/**
     * The unique instance of this class.
     *
     * @see #getInstance()
     */
    private static OverlapComparator myInstance = null;
	/**
     * The required overlapping pourcentage of the longer transition to consider the two transitions as equal.
     *
     * @see #setRMin()
     */
	private static double rmin = 0.333333;
	/**
     * The required overlapping pourcentage of the shorter transition to consider the two transitions as equal.
     *
     * @see #setRMax()
     */
	private static double rmax = 0.499999;
	
	private OverlapComparator(){}
	/**
     * Private constructor with two required parameters.
     *
     */
    private OverlapComparator(double rMin, double rMax){ 
    	rmin = rMin; 
    	rmax = rMax;
    }
	/**
     * Sets the RMax parameter.
     *
     */
	public static void setRMax(double rMax){ rmax = rMax;System.out.println("RMax=="+rmax); }
	/**
     * Sets the RMin parameter.
     *
     */
	public static void setRMin(double rMin){ rmin = rMin;System.out.println("RMin=="+rmin); }
	/**
     * Gets the RMax parameter.
     *
     */
	public static double getRMax(){ return rmax; }
	/**
     * Gets the RMin parameter.
     *
     */
	public static double getRMin(){ return rmin; }
	
	/**
     * Returns the unique instance of this class and set the parameters.
     *
     */
    public static OverlapComparator getInstance(double rmin, double rmax){
		if(myInstance == null)
	    	myInstance = new OverlapComparator(rmin, rmax);
		return myInstance;
    }
	/**
     * Returns the unique instance of this class with default parameter values.
     *
     */
    public static OverlapComparator getInstance(){
		if(myInstance == null)
	    	myInstance = new OverlapComparator();
		return myInstance;
    }

	/**
     * Compares its two arguments for ordering them. Uses the 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 intersection = longerTran.intersection(shorterTran);
			//Equality test according to this comparator
			if((intersection.length() >= (rmin*longerTran.length())) &&
			   (intersection.length() >= (rmax*shorterTran.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");
	}

}