Interval implements Comparable<Interval> { public Interval(long x) { this(x, x); } Interval(long x);  Interval(long min, long max); String toString(); int compareTo(Interval that); long min(); long max();  }
@Test public void testComparisons() { check(interval(4, 4), interval(4, 4), 0); check(interval(4, 4), interval(3, 4), 0); check(interval(4, 4), interval(4, 5), 0); check(interval(3, 5), interval(4, 4), 0); check(interval(3, 4), interval(4, 4), 0); check(interval(4, 5), interval(4, 4), 0); check(interval(3, 5), interval(4, 4), 0); check(interval(3, 4), interval(4, 5), -1); check(interval(4, 5), interval(3, 4), 1); checkNotComparable(interval(3, 5), interval(4, 6)); checkNotComparable(interval(4, 6), interval(3, 5)); }
@Test public void testMapOfDisjointIntervals() { for (int intervalSize = 1; intervalSize <= 5; intervalSize++) { List<Interval> control = new ArrayList<Interval>(); SortedMap<Interval, Interval> map = new TreeMap<Interval, Interval>(); for (int i = 0; i < 100; i++) { Interval interval = interval(i * intervalSize, (i + 1) * intervalSize - 1); control.add(interval); map.put(interval, interval); } Assert.assertEquals(control.size(), map.size()); Iterator<Interval> controlScan = control.iterator(); Iterator<Interval> mapScan = map.keySet().iterator(); while (controlScan.hasNext() && mapScan.hasNext()) { Interval controlInterval = controlScan.next(); Interval mapInterval = mapScan.next(); Assert.assertEquals(controlInterval, mapInterval); mapInterval = map.get(controlInterval); Assert.assertEquals(controlInterval, mapInterval); } Assert.assertTrue(!controlScan.hasNext()); Assert.assertTrue(!mapScan.hasNext()); } }