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

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

Introduction

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

Prototype

void add(Range<C> range);

Source Link

Document

Adds the specified range to this RangeSet (optional operation).

Usage

From source file:org.mskcc.shenkers.data.interval.RangeTools.java

public static RangeSet<Integer> asClosed(RangeSet<Integer> s) {
    RangeSet<Integer> closedIntervalSet = TreeRangeSet.create();
    for (Range<Integer> r : s.asRanges()) {
        closedIntervalSet.add(asClosed(r));
    }//from   w  w w  .  j a  v a  2s  . co  m
    return closedIntervalSet;
}

From source file:io.druid.sql.calcite.filtration.RangeSets.java

public static RangeSet<Long> fromIntervals(final Iterable<Interval> intervals) {
    final RangeSet<Long> retVal = TreeRangeSet.create();
    for (Interval interval : intervals) {
        retVal.add(Range.closedOpen(interval.getStartMillis(), interval.getEndMillis()));
    }/*from w  ww.  j  a  v  a 2  s .  co  m*/
    return retVal;
}

From source file:org.apache.james.imap.api.message.UidRange.java

private static RangeSet<MessageUid> createSortedRangeSet(List<UidRange> ranges) {
    RangeSet<MessageUid> rangeSet = TreeRangeSet.create();
    for (UidRange range : ranges) {
        rangeSet.add(Range.closed(range.getLowVal(), range.getHighVal()));
    }// w  w  w.j  ava 2  s  . c o  m
    return rangeSet;
}

From source file:org.apache.lens.cube.parse.CandidateUtil.java

/**
 * Returns true is the Candidates cover the entire time range.
 * @param candidates//from   w w  w  .  j  a  v a2 s  .  c o  m
 * @param startTime
 * @param endTime
 * @return
 */
static boolean isTimeRangeCovered(Collection<Candidate> candidates, Date startTime, Date endTime) {
    RangeSet<Date> set = TreeRangeSet.create();
    for (Candidate candidate : candidates) {
        set.add(Range.range(candidate.getStartTime(), BoundType.CLOSED, candidate.getEndTime(),
                BoundType.OPEN));
    }
    return set.encloses(Range.range(startTime, BoundType.CLOSED, endTime, BoundType.OPEN));
}

From source file:io.druid.sql.calcite.filtration.RangeSets.java

/**
 * Intersects a set of ranges, or returns null if the set is empty.
 *///from  w w  w .  j  a v  a  2s  .  co m
public static <T extends Comparable<T>> RangeSet<T> intersectRanges(final Iterable<Range<T>> ranges) {
    RangeSet<T> rangeSet = null;
    for (final Range<T> range : ranges) {
        if (rangeSet == null) {
            rangeSet = TreeRangeSet.create();
            rangeSet.add(range);
        } else {
            rangeSet = TreeRangeSet.create(rangeSet.subRangeSet(range));
        }
    }
    return rangeSet;
}

From source file:io.druid.sql.calcite.filtration.RangeSets.java

/**
 * Unions a set of ranges, or returns null if the set is empty.
 *//*  w  ww . ja va 2  s.  c o m*/
public static <T extends Comparable<T>> RangeSet<T> unionRanges(final Iterable<Range<T>> ranges) {
    RangeSet<T> rangeSet = null;
    for (Range<T> range : ranges) {
        if (rangeSet == null) {
            rangeSet = TreeRangeSet.create();
        }
        rangeSet.add(range);
    }
    return rangeSet;
}

From source file:com.rockhoppertech.music.examples.Guava.java

public static void goofaround() {
    MIDITrack track = MIDITrackBuilder.create().noteString("C D E").sequential().build();

    // http://docs.guava-libraries.googlecode.com/git-history/release12/javadoc/com/google/common/collect/FluentIterable.html#filter(com.google.common.base.Predicate)

    ImmutableList<MIDINote> notes = FluentIterable.from(track).transform(new AddFunction(1)).limit(10).toList();

    for (MIDINote note : notes) {
        logger.debug("{}", note);
    }//from  www .  j ava 2 s. c  om

    RangeSet<Double> rangeSet = TreeRangeSet.create();
    rangeSet.add(Range.closed(1d, 10d));
    rangeSet.add(Range.closed(11d, 20d));
    for (Range<Double> r : rangeSet.asRanges()) {
        logger.debug("{}", r);
    }
    logger.debug("span {}", rangeSet.span());
    logger.debug("contains {}", rangeSet.contains(20d));
    logger.debug("contains {}", rangeSet.contains(20.1));

}

From source file:com.pingcap.tikv.predicates.RangeBuilder.java

/**
 * Turn CNF filters into range//from   ww w  .ja v a2  s  . c o m
 *
 * @param accessConditions filters in CNF list
 * @param type index column type
 * @return access ranges
 */
@SuppressWarnings("unchecked")
static List<Range> exprToRanges(List<TiExpr> accessConditions, DataType type) {
    if (accessConditions == null || accessConditions.size() == 0) {
        return ImmutableList.of();
    }
    RangeSet ranges = TreeRangeSet.create();
    ranges.add(Range.all());
    for (TiExpr ac : accessConditions) {
        NormalizedCondition cond = AccessConditionNormalizer.normalize(ac);
        TiConstant constVal = cond.constantVals.get(0);
        Comparable<?> comparableVal = Comparables.wrap(constVal.getValue());
        TiExpr expr = cond.condition;

        if (expr instanceof GreaterThan) {
            ranges = ranges.subRangeSet(Range.greaterThan(comparableVal));
        } else if (expr instanceof GreaterEqual) {
            ranges = ranges.subRangeSet(Range.atLeast(comparableVal));
        } else if (expr instanceof LessThan) {
            ranges = ranges.subRangeSet(Range.lessThan(comparableVal));
        } else if (expr instanceof LessEqual) {
            ranges = ranges.subRangeSet(Range.atMost(comparableVal));
        } else if (expr instanceof Equal) {
            ranges = ranges.subRangeSet(Range.singleton(comparableVal));
        } else if (expr instanceof NotEqual) {
            RangeSet left = ranges.subRangeSet(Range.lessThan(comparableVal));
            RangeSet right = ranges.subRangeSet(Range.greaterThan(comparableVal));
            ranges = TreeRangeSet.create(left);
            ranges.addAll(right);
        } else {
            throw new TiClientInternalException(
                    "Unsupported conversion to Range " + expr.getClass().getSimpleName());
        }
    }
    return ImmutableList.copyOf(ranges.asRanges());
}

From source file:net.hydromatic.foodbench.Main.java

private static RangeSet<Integer> parseInts(String idsProperty) {
    RangeSet<Integer> idSet = TreeRangeSet.create();
    if (idsProperty == null) {
        idsProperty = "";
    }/*from  w w w. j  a  v  a  2 s.  co  m*/
    if (idsProperty.isEmpty() || idsProperty.startsWith("-")) {
        idSet.add(Range.<Integer>all());
    }
    if (!idsProperty.isEmpty()) {
        for (String id : idsProperty.split(",")) {
            String[] split2 = id.split("-");
            if (split2.length != 2) {
                if (id.endsWith("-")) {
                    // 10- means "10 onwards"
                    idSet.add(Range.atLeast(Integer.parseInt(id.substring(0, id.length() - 1))));
                } else {
                    idSet.add(Range.singleton(Integer.parseInt(id)));
                }
            } else if (split2[0].equals("")) {
                // -10 means "not 10"
                idSet.remove(Range.singleton(Integer.parseInt(split2[1])));
            } else {
                int min = Integer.parseInt(split2[0]);
                int max = Integer.parseInt(split2[1]);
                idSet.add(Range.closed(min, max));
            }
        }
    }
    return idSet;
}

From source file:org.kiji.schema.impl.cassandra.CassandraKijiPartition.java

/**
 * Convert a set of (start-token, host) pairs into a set of (token-range, host) pairs.
 *
 * Package private for testing./*from   w w  w .  j av a 2 s .c  om*/
 *
 * @param startTokens The set of start tokens with hosts.
 * @return The token corresponding token ranges.
 */
static Map<Range<Long>, InetAddress> getTokenRanges(final SortedMap<Long, InetAddress> startTokens) {

    ImmutableMap.Builder<Range<Long>, InetAddress> tokenRangesBldr = ImmutableMap.builder();

    final PeekingIterator<Entry<Long, InetAddress>> startTokensItr = Iterators
            .peekingIterator(startTokens.entrySet().iterator());

    // Add a range for [-, firstStartToken) owned by the final key (the wrap-around range).
    // For more information on Casandra VNode token ranges:
    //    http://www.datastax.com/dev/blog/virtual-nodes-in-cassandra-1-2
    tokenRangesBldr.put(Range.lessThan(startTokens.firstKey()), startTokens.get(startTokens.lastKey()));

    while (startTokensItr.hasNext()) {
        Entry<Long, InetAddress> startToken = startTokensItr.next();
        if (!startTokensItr.hasNext()) {
            // The final start token
            // Add a range for [lastStartToken, )
            tokenRangesBldr.put(Range.atLeast(startToken.getKey()), startToken.getValue());
        } else {
            // Add a range for [thisStartToken, nextStartToken)
            tokenRangesBldr.put(Range.closedOpen(startToken.getKey(), startTokensItr.peek().getKey()),
                    startToken.getValue());
        }
    }

    final Map<Range<Long>, InetAddress> tokenRanges = tokenRangesBldr.build();

    // Check that the returned ranges are coherent; most importantly that all possible tokens fall
    // within the returned range set.

    if (startTokens.size() + 1 != tokenRanges.size()) {
        throw new InternalKijiError(
                String.format("Unexpected number of token ranges. start-tokens: %s, token-ranges: %s.",
                        startTokens.size(), tokenRanges.size()));
    }

    final RangeSet<Long> ranges = TreeRangeSet.create();
    for (Range<Long> tokenRange : tokenRanges.keySet()) {
        ranges.add(tokenRange);
    }

    if (!ranges.encloses(Range.closed(Long.MIN_VALUE, Long.MAX_VALUE))) {
        throw new InternalKijiError("Token range does not include all possible tokens.");
    }

    return tokenRanges;
}