public class Lexicographic implements Comparator {
int xa, ya, xb, yb;
// Comparator for points in the plane with the standard lexicographic order.
// Assumes a Point2D has getX() and getY() methods returning its coordinates.
private void getXY(Object a, Object b) {
if (a == null || b == null)
throw new InvalidElementException("Null Argument");
try{
xa = ((Point2D) a).getX();
ya = ((Point2D) a).getY();
xb = ((Point2D) b).getX();
yb = ((Point2D) b).getY();
}
catch (ClassCastException e)
{ throw new InvalidElementException("Argument not a Point2D"); }
}
public boolean isLessThan(Object a, Object b) {
getXY(a, b);
if (xa == xb)
return (ya < yb);
else
return (xa < xb);
}
public boolean isLessThanOrEqualTo(Object a, Object b) {
getXY(a, b);
if (xa == xb)
return (ya <= yb);
else
return (xa <= xb);
}
public boolean isEqualTo(Object a, Object b) {
getXY(a, b);
return (xa == xb) && (ya == yb);
}