IntervalMap implements TimeMap<Interval, T> { protected int[] getOverlappingIntervals(double intervalStart, double intervalEnd) { int realSize = size * 2; int index = Arrays.binarySearch(array, 0, realSize, intervalStart); if (index >= 0) { int startIndex = index % 2 == 0 ? index : index - 1; for (; startIndex - 2 >= 0;) { if (array[startIndex - 2] == intervalStart) { startIndex -= 2; } else { break; } } int[] res = new int[size - (startIndex / 2)]; int i = 0; for (; startIndex < realSize && array[startIndex] <= intervalEnd; startIndex += 2) { res[i++] = startIndex / 2; } if (res.length != i) { return Arrays.copyOf(res, i); } return res; } else { int startIndex = (-index - 1) % 2 == 0 ? (-index - 1) : -index - 2; if (startIndex < realSize && array[startIndex] <= intervalEnd) { int[] res = new int[size - (startIndex / 2)]; int i = 0; for (; startIndex < realSize && array[startIndex] <= intervalEnd; startIndex += 2) { res[i++] = startIndex / 2; } if (i != 0) { if (res.length != i) { return Arrays.copyOf(res, i); } return res; } } } return new int[0]; } IntervalMap();  IntervalMap(int capacity); protected  IntervalMap(double[] keys); @Override boolean put(Interval interval, T value); @Override boolean remove(Interval interval); @Override Object get(Interval interval, Estimator estimator); @Override T get(Interval interval, T defaultValue); @Override T[] toValuesArray(); @Override int size(); @Override boolean isEmpty(); boolean contains(double timestamp); @Override boolean contains(Interval interval); @Override Interval[] toKeysArray(); double[] getIntervals(); @Override void clear(); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(TimeFormat timeFormat, DateTimeZone timeZone); @Override String toString(TimeFormat timeFormat); @Override String toString();  }
@Test public void testGetOverlappingIntervals() { IntervalIntegerMap i = new IntervalIntegerMap(); i.put(new Interval(2001, 2004), 42); Assert.assertEquals(i.getOverlappingIntervals(2001, 2004), new int[] { 0 }); Assert.assertEquals(i.getOverlappingIntervals(2001, 2005), new int[] { 0 }); Assert.assertEquals(i.getOverlappingIntervals(2001, Double.POSITIVE_INFINITY), new int[] { 0 }); IntervalIntegerMap j = new IntervalIntegerMap(); j.put(new Interval(2000, 2002), 42); j.put(new Interval(2002, 2002), 42); j.put(new Interval(2002, 2004), 42); j.put(new Interval(2005, 2006), 42); Assert.assertEquals(j.getOverlappingIntervals(1998, 1999), new int[] {}); Assert.assertEquals(j.getOverlappingIntervals(1998, 2001), new int[] { 0 }); Assert.assertEquals(j.getOverlappingIntervals(1998, 2000), new int[] { 0 }); Assert.assertEquals(j.getOverlappingIntervals(1998, 2002), new int[] { 0, 1, 2 }); Assert.assertEquals(j.getOverlappingIntervals(2000, 2001), new int[] { 0 }); Assert.assertEquals(j.getOverlappingIntervals(2001, 2001), new int[] { 0 }); Assert.assertEquals(j.getOverlappingIntervals(2003, 2003), new int[] { 2 }); Assert.assertEquals(j.getOverlappingIntervals(2003, 2007), new int[] { 2, 3 }); Assert.assertEquals(j.getOverlappingIntervals(2009, 2010), new int[] {}); Assert.assertEquals(j.getOverlappingIntervals(2002, 2004.9), new int[] { 1, 2 }); }