public Map<ReservationInterval, Resource> toIntervalMap() {

    readLock.lock();
    try {
      Map<ReservationInterval, Resource> allocations =
          new TreeMap<ReservationInterval, Resource>();

      // Empty
      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();
    }
  }
@Test
  public void testToIntervalMap() {
    ResourceCalculator resCalc = new DefaultResourceCalculator();
    RLESparseResourceAllocation rleSparseVector =
        new RLESparseResourceAllocation(resCalc);
    Map<ReservationInterval, Resource> mapAllocations;

    // Check empty
    mapAllocations = rleSparseVector.toIntervalMap();
    Assert.assertTrue(mapAllocations.isEmpty());

    // Check full
    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();
      }
    }
  }