package sort; import java.util.Comparator; /** * Allows to get a comparator on a cartesian product of two sets T1xT2 * using the lexicographic order obtained from their respective comparators. * The result is applied on a set T using two projections on T1 and T2. */ /** * @author casteran */ public abstract class Lexicographic implements Comparator { protected abstract Comparator getComparator1(); protected abstract Comparator getComparator2(); protected abstract T1 projection1(T t); protected abstract T2 projection2(T t); /* * (non-Javadoc) * * @see java.util.Comparator#compare(T, T) */ public final int compare(T t1, T t2) { int c = getComparator1().compare(projection1(t1), projection1(t2)); return c != 0 ? c : getComparator2().compare(projection2(t1), projection2(t2)); } }