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

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

Introduction

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

Prototype

public boolean isEmpty() 

Source Link

Document

Returns true if this range is of the form [v..v) or (v..v] .

Usage

From source file:com.tinspx.util.io.callbacks.FileChannelCallback.java

/**
 * Normalizes lower bound to a closed bound type (if exists) and the
 * upper bound to an open bound type (if exists). Missing bounds remain
 * missing. Lower bound must be non-negative and upper bound must be
 * positive. The range cannot be empty./* w  w  w  . j  a  v a2  s  .com*/
 */
static Range<Long> checkAndNormalize(Range<Long> range) {
    checkArgument(!range.isEmpty(), "range %s is empty", range);
    boolean make = false;
    long lower = -1;
    long upper = -1;

    if (range.hasLowerBound()) {
        lower = range.lowerEndpoint();
        if (range.lowerBoundType() == BoundType.OPEN) {
            make = true;
            lower++;
        }
        checkArgument(lower >= 0, "closed lower bound (%s) may not be negative", lower);
    }
    if (range.hasUpperBound()) {
        upper = range.upperEndpoint();
        if (range.upperBoundType() == BoundType.CLOSED) {
            make = true;
            upper++;
        }
        checkArgument(upper > 0, "open upper bound (%s) must be positive", upper);
    }
    if (make) {
        if (lower >= 0) {
            if (upper > 0) {
                range = Range.closedOpen(lower, upper);
            } else {
                range = Range.atLeast(lower);
            }
        } else {
            assert upper > 0 : upper;
            range = Range.lessThan(upper);
        }
        checkArgument(!range.isEmpty(), "normalized range %s is empty", range);
    }
    return range;
}

From source file:org.apache.kylin.storage.cache.TsConditionExtractor.java

private static Range<Long> extractTsConditionInternal(TupleFilter filter, TblColRef colRef) {
    if (filter == null) {
        return Range.all();
    }/*  w  w  w  .  j a  v  a 2  s  .  c  o m*/

    if (filter instanceof LogicalTupleFilter) {
        if (filter.getOperator() == TupleFilter.FilterOperatorEnum.AND) {
            Range<Long> ret = Range.all();
            for (TupleFilter child : filter.getChildren()) {
                Range childRange = extractTsConditionInternal(child, colRef);
                if (childRange != null) {
                    if (ret.isConnected(childRange) && !ret.intersection(childRange).isEmpty()) {
                        ret = ret.intersection(childRange);
                    } else {
                        return null;
                    }
                } else {
                    return null;
                }
            }
            return ret.isEmpty() ? null : ret;
        } else {
            //for conditions like date > DATE'2000-11-11' OR date < DATE '1999-01-01'
            //we will use Ranges.all() rather than two ranges to represent them
            return Range.all();
        }
    }

    if (filter instanceof CompareTupleFilter) {
        CompareTupleFilter compareTupleFilter = (CompareTupleFilter) filter;
        if (compareTupleFilter.getColumn() == null)// column will be null at filters like " 1<>1"
            return Range.all();

        if (compareTupleFilter.getColumn().equals(colRef)) {
            Object firstValue = compareTupleFilter.getFirstValue();
            long t;
            switch (compareTupleFilter.getOperator()) {
            case EQ:
                t = DateFormat.stringToMillis((String) firstValue);
                return Range.closed(t, t);
            case LT:
                t = DateFormat.stringToMillis((String) firstValue);
                return Range.lessThan(t);
            case LTE:
                t = DateFormat.stringToMillis((String) firstValue);
                return Range.atMost(t);
            case GT:
                t = DateFormat.stringToMillis((String) firstValue);
                return Range.greaterThan(t);
            case GTE:
                t = DateFormat.stringToMillis((String) firstValue);
                return Range.atLeast(t);
            case NEQ:
            case IN://not handled for now
                break;
            default:
            }
        }
    }
    return Range.all();
}

From source file:edu.mit.streamjit.impl.compiler2.Core.java

public void allocate(ActorGroup group, Range<Integer> iterations) {
    if (!iterations.isEmpty())
        allocations.add(Pair.make(group, iterations));
}

From source file:org.apache.kylin.storage.cache.StreamSQLResult.java

public Range<Long> getReusableResults(Range<Long> tsRange) {

    if (tsRange.equals(timeCovered))
        return timeCovered;

    if (!timeCovered.isConnected(tsRange)) {
        //share nothing in common
        return null;
    }//  www.j  a  v a  2s  . c om

    Range<Long> ret = timeCovered.intersection(tsRange);
    return ret.isEmpty() ? null : ret;
}

From source file:org.obm.push.minig.imap.impl.ImapMessageSet.java

public String asString() {
    List<String> rangesAsStrings = Lists.newArrayList();
    for (Range<Long> range : set.getRanges()) {
        if (!range.isEmpty()) {
            rangesAsStrings.add(rangeAsString(range));
        }//  w  w w. j  av a  2 s  .co  m
    }
    return Joiner.on(',').join(rangesAsStrings);
}

From source file:net.joala.data.random.AbstractRandomNumberProvider.java

/**
 * Provide a random number in the given range.
 * @param range (closed) range in which to choose a number from
 * @return random value//from   w ww . j ava 2 s  . c  o m
 * @throws IllegalStateException if range is empty
 */
private T nextRandom(final Range<T> range) {
    checkState(!range.isEmpty(), "Range must not be empty.");
    final T lowerEndpoint = range.lowerEndpoint();
    final T upperEndpoint = range.upperEndpoint();
    final double random = nextRandomRatio();
    return numberType.sum(numberType.percentOf(random, upperEndpoint),
            numberType.percentOf((1d - random), lowerEndpoint));
}

From source file:io.blobkeeper.common.util.MerkleTree.java

public MerkleTree(Range<Long> fullRange, int maxDepth) {
    checkArgument(!fullRange.isEmpty(), "Range is empty!");

    this.size = 0;
    this.fullRange = fullRange;
    this.maxDepth = maxDepth;
    init();/*from w  w  w  . j av  a  2 s .co  m*/
}

From source file:org.eclipse.fx.ui.controls.styledtext.internal.LineHelper.java

public Range<Integer> getSelection(int index) {
    Range<Integer> selection = getSelection();
    if (selection.isEmpty()) {
        return null;
    }//from w w  w  .  j a  v  a  2 s.c o m

    Range<Integer> range = getRange(index);

    Range<Integer> localSelection = null;

    if (range.isConnected(selection)) {
        Range<Integer> intersection = selection.intersection(range);
        localSelection = mapToLocal(index, intersection);
    }

    return localSelection;
}

From source file:edu.cmu.lti.oaqa.framework.eval.passage.PassageMAPEvalAggregator.java

private float getAvgPsgMAP(List<Passage> docs, List<Passage> gs) {
    if (gs.size() == 0) {
        return 0;
    }//from w ww. j  a v  a2s  .  c o m
    int totalChars = 0;
    int overlapLength = 0;
    float sumPrecision = 0;
    int count = 0;
    Set<Passage> foundGoldTriplets = Sets.newHashSet();
    for (Passage doc : docs) {
        Range<Integer> docRange = Range.closedOpen(doc.getBegin(), doc.getEnd());
        totalChars += docRange.upperEndpoint() - docRange.lowerEndpoint();
        for (Passage g : gs) {
            if (!g.getUri().equals(doc.getUri()))
                continue;
            Range<Integer> gRange = Range.closedOpen(g.getBegin(), g.getEnd());
            if (!docRange.isConnected(gRange)) {
                continue;
            }
            Range<Integer> overlap = docRange.intersection(gRange);
            if (overlap.isEmpty()) {
                continue;
            }
            overlapLength += overlap.upperEndpoint() - overlap.lowerEndpoint();
            sumPrecision += (float) overlapLength / (float) totalChars;
            count++;
            foundGoldTriplets.add(g);
            break;
        }
    }
    int numZeros = Sets.difference(Sets.newHashSet(gs), foundGoldTriplets).size();
    return (float) sumPrecision / (float) (count + numZeros);
}

From source file:edu.mit.streamjit.impl.compiler2.FullDataParallelAllocationStrategy.java

@Override
public void allocateGroup(ActorGroup group, Range<Integer> iterations, List<Core> cores, Configuration config) {
    int coresSize = Math.min(cores.size(), maxNumCores);
    int perCore = IntMath.divide(iterations.upperEndpoint() - iterations.lowerEndpoint(), coresSize,
            RoundingMode.CEILING);
    for (int i = 0; i < coresSize && !iterations.isEmpty(); ++i) {
        int min = iterations.lowerEndpoint();
        Range<Integer> allocation = group.isStateful() ? iterations
                : iterations.intersection(Range.closedOpen(min, min + perCore));
        cores.get(i).allocate(group, allocation);
        iterations = Range.closedOpen(allocation.upperEndpoint(), iterations.upperEndpoint());
    }/*from  w  ww  .  j  av a2  s.c o m*/
    assert iterations.isEmpty();
}