Example usage for com.google.common.collect RangeSet asDescendingSetOfRanges

List of usage examples for com.google.common.collect RangeSet asDescendingSetOfRanges

Introduction

In this page you can find the example usage for com.google.common.collect RangeSet asDescendingSetOfRanges.

Prototype

Set<Range<C>> asDescendingSetOfRanges();

Source Link

Document

Returns a descending view of the Range#isConnected disconnected ranges that make up this range set.

Usage

From source file:org.openehealth.ipf.commons.ihe.fhir.LazyBundleProvider.java

@Override
public List<IBaseResource> getResources(int fromIndex, int toIndex) {
    if (!cacheResults) {
        return getPartialResult(fromIndex, toIndex);
    }/* w w w  .  jav  a  2 s .  c o  m*/
    LOG.debug("Cached results contain the following ranges: {}. Requesting resources from index {} to {}",
            resultRanges, fromIndex, toIndex);
    Range<Integer> wanted = Range.closedOpen(fromIndex, toIndex);
    RangeSet<Integer> needed = resultRanges.required(wanted);
    LOG.debug("Requiring the following ranges {}", needed);
    for (Range<Integer> requiredRange : needed.asDescendingSetOfRanges()) {
        LOG.debug("Now requesting the following range {}", requiredRange);
        List<IBaseResource> results = getPartialResult(requiredRange.lowerEndpoint(),
                requiredRange.upperEndpoint());
        LOG.debug("Got back a list of size {}", results.size());
        if (!results.isEmpty()) {
            cacheAll(requiredRange.lowerEndpoint(), results);
            // Take care, potentially less elements than requested have been retrieved
            resultRanges.add(Range.closedOpen(requiredRange.lowerEndpoint(),
                    requiredRange.lowerEndpoint() + results.size()));
        }
    }
    LOG.debug("Cached results now contain the following ranges: {}", resultRanges);

    // Everything went OK, return whatever we got
    return cachedResults.subList(fromIndex,
            Math.min(cachedResults.size(), Math.min(cachedResults.size(), toIndex)));
}