Distance2DComparator implements Comparator<ILocated2D> { @Override public int compare(ILocated2D o1, ILocated2D o2) { if (reference == null || (o1 == null && o2 == null)) { return 0; } else if (o1 == null) { return 1; } else if (o2 == null) { return -1; } return (int) Math.signum(o1.distance(reference) - o2.distance(reference)); } Distance2DComparator(ILocated2D reference); @Override /** * compares two distances, each between the reference and a given point, * * @param o1 * object in 2d space * @param o2 * another object in 2d space * @return comparison between (distance between (o1, reference)) and (distance between (o2, reference)) * 1 : o1 is farther, * 0 : they are equally far from reference, * -1 : o2 is farther. */ int compare(ILocated2D o1, ILocated2D o2);  }
@Test public void testCompare() { Distance2DComparator vsBottomLeft = new Distance2DComparator(bottomLeft); assertEquals(0, vsBottomLeft.compare(topLeft, bottomRight)); assertEquals(1, vsBottomLeft.compare(topRight, bottomRight)); assertEquals(-1, vsBottomLeft.compare(topLeft, topRight)); assertEquals(1, vsBottomLeft.compare(topCenter, bottomRight)); assertEquals(1, vsBottomLeft.compare(topCenter, bottomLeft)); assertEquals(-1, vsBottomLeft.compare(topCenter, topRight)); Distance2DComparator vsFar = new Distance2DComparator(farBottomLeft); assertEquals(1, vsFar.compare(topCenter, bottomRight)); assertEquals(-1, vsFar.compare(bottomLeft, bottomRight)); assertEquals(0, vsFar.compare(topLeft, bottomRight)); }