Offset implements Comparable<Offset> { @Override public int compareTo(Offset o) { int start = Integer.compare(begin, o.begin); if (start != 0) { return start; } else { return Integer.compare(end, o.end); } } Offset(int begin, int end); int getBegin(); int getEnd(); @Override int hashCode(); @Override boolean equals(Object obj); @Override int compareTo(Offset o);  }
@Test public void testOffsetOrdering() { Offset o1 = new Offset(0, 9); Offset o2 = new Offset(10, 19); Offset o3 = new Offset(20, 29); Offset o4 = new Offset(10, 22); assertEquals(0, o1.compareTo(o1)); assertEquals(-1, o1.compareTo(o2)); assertEquals(-1, o1.compareTo(o3)); assertEquals(-1, o2.compareTo(o3)); assertEquals(1, o3.compareTo(o1)); assertEquals(-1, o1.compareTo(o4)); assertEquals(-1, o2.compareTo(o4)); assertEquals(1, o4.compareTo(o2)); }