IntervalMap implements TimeMap<Interval, T> { @Override public boolean put(Interval interval, T value) { if (value == null) { throw new NullPointerException(); } Object values = getValuesArray(); int valuesLength = Array.getLength(values); final int index = putInner(interval.getLow(), interval.getHigh()); if (index < 0) { int insertIndex = -index - 1; if (size - 1 < valuesLength) { if (insertIndex < size - 1) { System.arraycopy(values, insertIndex, values, insertIndex + 1, size - insertIndex - 1); } Array.set(values, insertIndex, value); } else { Object newArray = Array.newInstance(values.getClass().getComponentType(), valuesLength + 1); System.arraycopy(values, 0, newArray, 0, insertIndex); System.arraycopy(values, insertIndex, newArray, insertIndex + 1, valuesLength - insertIndex); Array.set(newArray, insertIndex, value); setValuesArray(newArray); } return true; } else { Array.set(values, index, value); } return false; } 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 testPutOne() { for (IntervalMap set : getAllInstances()) { Object[] defaultValues = getTestValues(set); Assert.assertTrue(set.put(new Interval(1.0, 2.0), defaultValues[0])); testValues(set, new Interval[] { new Interval(1.0, 2.0) }, new Object[] { defaultValues[0] }); } }
@Test public void testPutTwice() { for (IntervalMap set : getAllInstances()) { Object[] defaultValues = getTestValues(set); Assert.assertTrue(set.put(new Interval(1.0, 2.0), defaultValues[0])); Assert.assertFalse(set.put(new Interval(1.0, 2.0), defaultValues[1])); testValues(set, new Interval[] { new Interval(1.0, 2.0) }, new Object[] { defaultValues[1] }); } }
@Test public void testMultiplePut() { for (IntervalMap set : getAllInstances()) { Object[] defaultValues = getTestValues(set); Assert.assertTrue(set.put(new Interval(1.0, 2.0), defaultValues[0])); Assert.assertTrue(set.put(new Interval(6.0, 8.0), defaultValues[1])); testValues(set, new Interval[] { new Interval(1.0, 2.0), new Interval(6.0, 8.0) }, defaultValues); } }
@Test public void testMultiplePutWithCapacity() { for (IntervalMap set : getAllInstances(10)) { Object[] defaultValues = getTestValues(set); Assert.assertTrue(set.put(new Interval(1.0, 2.0), defaultValues[0])); Assert.assertTrue(set.put(new Interval(6.0, 8.0), defaultValues[1])); testValues(set, new Interval[] { new Interval(1.0, 2.0), new Interval(6.0, 8.0) }, defaultValues); } }
@Test public void testMultiplePutWithOverlap() { for (IntervalMap set : getAllInstances()) { Object[] defaultValues = getTestValues(set); Assert.assertTrue(set.put(new Interval(1.0, 2.0), defaultValues[0])); Assert.assertTrue(set.put(new Interval(3.0, 4.0), defaultValues[1])); Assert.assertTrue(set.put(new Interval(2.0, 2.0), defaultValues[0])); Assert.assertTrue(set.put(new Interval(2.0, 3.0), defaultValues[1])); defaultValues = new Object[] { defaultValues[0], defaultValues[0], defaultValues[1], defaultValues[1] }; testValues(set, new Interval[] { new Interval(1.0, 2.0), new Interval(2.0, 2.0), new Interval(2.0, 3.0), new Interval( 3.0, 4.0) }, defaultValues); } }
@Test public void testPutNull() { for (IntervalMap set : getAllInstances()) { boolean thrown = false; try { set.put(new Interval(1.0, 2.0), null); } catch (NullPointerException e) { thrown = true; } if (!thrown) { Assert.fail("Didn't throw an exception for " + set.getClass()); } } }