TileCodeComparator implements Comparator<TileCode> { @Override public int compare(TileCode o1, TileCode o2) { int x1 = o1.getX() - offsetX; int x2 = o2.getX() - offsetX; int y1 = o1.getY() - offsetY; int y2 = o2.getY() - offsetY; int o1d = Math.max(Math.abs(x1), Math.abs(y1)); int o2d = Math.max(Math.abs(x2), Math.abs(y2)); if (x1 == x2 && y1 == y2) { return 0; } else if (o1d != o2d) { return (o1d < o2d) ? -1 : 1; } else { if (y1 == o1d) { if (y1 == y2) { return (x1 < x2) ? -1 : 1; } else { return -1; } } else if (x1 == o1d) { if (y2 == o1d) { return 1; } else if (x1 == x2) { return (y1 > y2) ? -1 : 1; } else { return -1; } } else if (y1 == -1 * o1d) { if (y2 == o1d || x2 == o1d) { return 1; } else if (y1 == y2) { return (x1 > x2) ? -1 : 1; } else { return -1; } } else if (x1 == -1 * o1d) { if (y2 == o1d || x2 == o1d || y2 == -1 * o1d) { return 1; } else { return (y1 < y2) ? -1 : 1; } } return 0; } } TileCodeComparator(int offsetX, int offsetY); @Override int compare(TileCode o1, TileCode o2);  }
@Test public void testTileCodeComparatorSymmetry() { List<TileCode> list = createTileCodes(0, 0, 3, 3); TileCodeComparator comp = new TileCodeComparator(1, 1); for (TileCode code1 : list) { for (TileCode code2 : list) { int left = comp.compare(code1, code2); int right = - comp.compare(code2, code1); if (left != right) { System.out.println(code1 + " " + code2); } Assert.assertEquals(left, right); } } }