Example usage for com.google.common.collect Range lowerBoundType

List of usage examples for com.google.common.collect Range lowerBoundType

Introduction

In this page you can find the example usage for com.google.common.collect Range lowerBoundType.

Prototype

public BoundType lowerBoundType() 

Source Link

Document

Returns the type of this range's lower bound: BoundType#CLOSED if the range includes its lower endpoint, BoundType#OPEN if it does not.

Usage

From source file:org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.java

/**
 * Get the number of entries between a contiguous range of two positions
 *
 * @param range/*from  ww  w . j  a  va 2 s .  c o  m*/
 *            the position range
 * @return the count of entries
 */
long getNumberOfEntries(Range<PositionImpl> range) {
    PositionImpl fromPosition = range.lowerEndpoint();
    boolean fromIncluded = range.lowerBoundType() == BoundType.CLOSED;
    PositionImpl toPosition = range.upperEndpoint();
    boolean toIncluded = range.upperBoundType() == BoundType.CLOSED;

    if (fromPosition.getLedgerId() == toPosition.getLedgerId()) {
        // If the 2 positions are in the same ledger
        long count = toPosition.getEntryId() - fromPosition.getEntryId() - 1;
        count += fromIncluded ? 1 : 0;
        count += toIncluded ? 1 : 0;
        return count;
    } else {
        long count = 0;
        // If the from & to are pointing to different ledgers, then we need to :
        // 1. Add the entries in the ledger pointed by toPosition
        count += toPosition.getEntryId();
        count += toIncluded ? 1 : 0;

        // 2. Add the entries in the ledger pointed by fromPosition
        LedgerInfo li = ledgers.get(fromPosition.getLedgerId());
        if (li != null) {
            count += li.getEntries() - (fromPosition.getEntryId() + 1);
            count += fromIncluded ? 1 : 0;
        }

        // 3. Add the whole ledgers entries in between
        for (LedgerInfo ls : ledgers.subMap(fromPosition.getLedgerId(), false, toPosition.getLedgerId(), false)
                .values()) {
            count += ls.getEntries();
        }

        return count;
    }
}