public class Point2D implements Comparable { public static final Comparator X_ORDER = new XOrder(); public static final Comparator Y_ORDER = new YOrder(); public static final Comparator R_ORDER = new ROrder(); public final Comparator POLAR_ORDER = new PolarOrder(); public final Comparator ATAN2_ORDER = new Atan2Order(); public final Comparator DISTANCE_TO_ORDER = new DistanceToOrder(); private final double x; private final double y; /*Many many methods omitted here*/ // compare by y-coordinate, breaking ties by x-coordinate public int compareTo(Point2D that) { if (this.y < that.y) return -1; if (this.y > that.y) return +1; if (this.x < that.x) return -1; if (this.x > that.x) return +1; return 0; } // compare points according to their x-coordinate private static class XOrder implements Comparator { public int compare(Point2D p, Point2D q) { if (p.x < q.x) return -1; if (p.x > q.x) return +1; return 0; } } // compare points according to their distance to this point private class DistanceToOrder implements Comparator { public int compare(Point2D p, Point2D q) { double dist1 = distanceSquaredTo(p); double dist2 = distanceSquaredTo(q); if (dist1 < dist2) return -1; else if (dist1 > dist2) return +1; else return 0; } } }