RLESparseResourceAllocation { public Map<ReservationInterval, Resource> toIntervalMap() { readLock.lock(); try { Map<ReservationInterval, Resource> allocations = new TreeMap<ReservationInterval, Resource>(); if (isEmpty()) { return allocations; } Map.Entry<Long, Resource> lastEntry = null; for (Map.Entry<Long, Resource> entry : cumulativeCapacity.entrySet()) { if (lastEntry != null && entry.getValue() != null) { ReservationInterval interval = new ReservationInterval(lastEntry.getKey(), entry.getKey()); Resource resource = lastEntry.getValue(); allocations.put(interval, resource); } lastEntry = entry; } return allocations; } finally { readLock.unlock(); } } RLESparseResourceAllocation(ResourceCalculator resourceCalculator);  RLESparseResourceAllocation(NavigableMap<Long, Resource> out,
      ResourceCalculator resourceCalculator); boolean addInterval(ReservationInterval reservationInterval,
      Resource totCap); boolean removeInterval(ReservationInterval reservationInterval,
      Resource totCap); Resource getCapacityAtTime(long tick); long getEarliestStartTime(); long getLatestNonNullTime(); boolean isEmpty(); @Override String toString(); String toMemJSONString(); Map<ReservationInterval, Resource> toIntervalMap(); NavigableMap<Long, Resource> getCumulative(); static RLESparseResourceAllocation merge(ResourceCalculator resCalc,
      Resource clusterResource, RLESparseResourceAllocation a,
      RLESparseResourceAllocation b, RLEOperator operator, long start, long end); RLESparseResourceAllocation getRangeOverlapping(long start, long end);  }
@Test public void testToIntervalMap() { ResourceCalculator resCalc = new DefaultResourceCalculator(); RLESparseResourceAllocation rleSparseVector = new RLESparseResourceAllocation(resCalc); Map<ReservationInterval, Resource> mapAllocations; mapAllocations = rleSparseVector.toIntervalMap(); Assert.assertTrue(mapAllocations.isEmpty()); int[] alloc = { 0, 5, 10, 10, 5, 0, 5, 0 }; int start = 100; Set<Entry<ReservationInterval, Resource>> inputs = generateAllocation(start, alloc, false).entrySet(); for (Entry<ReservationInterval, Resource> ip : inputs) { rleSparseVector.addInterval(ip.getKey(), ip.getValue()); } mapAllocations = rleSparseVector.toIntervalMap(); Assert.assertTrue(mapAllocations.size() == 5); for (Entry<ReservationInterval, Resource> entry : mapAllocations.entrySet()) { ReservationInterval interval = entry.getKey(); Resource resource = entry.getValue(); if (interval.getStartTime() == 101L) { Assert.assertTrue(interval.getEndTime() == 102L); Assert.assertEquals(resource, Resource.newInstance(5 * 1024, 5)); } else if (interval.getStartTime() == 102L) { Assert.assertTrue(interval.getEndTime() == 104L); Assert.assertEquals(resource, Resource.newInstance(10 * 1024, 10)); } else if (interval.getStartTime() == 104L) { Assert.assertTrue(interval.getEndTime() == 105L); Assert.assertEquals(resource, Resource.newInstance(5 * 1024, 5)); } else if (interval.getStartTime() == 105L) { Assert.assertTrue(interval.getEndTime() == 106L); Assert.assertEquals(resource, Resource.newInstance(0 * 1024, 0)); } else if (interval.getStartTime() == 106L) { Assert.assertTrue(interval.getEndTime() == 107L); Assert.assertEquals(resource, Resource.newInstance(5 * 1024, 5)); } else { Assert.fail(); } } }