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

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

Introduction

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

Prototype

public static <C extends Comparable<?>> Range<C> closedOpen(C lower, C upper) 

Source Link

Document

Returns a range that contains all values greater than or equal to lower and strictly less than upper .

Usage

From source file:org.obm.opush.windowing.WindowingStepdefs.java

private WindowingChanges<Email> generateEmails(long start, long number) {
    return EmailChanges.builder()
            .additions(FluentIterable//from   w w  w .j av a 2  s . c om
                    .from(ContiguousSet.create(Range.closedOpen(start, start + number), DiscreteDomain.longs()))
                    .transform(new Function<Long, Email>() {
                        @Override
                        public Email apply(Long uid) {
                            return Email.builder().uid(uid).build();
                        }
                    }).toSet())
            .build();
}

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

/**
 * Returns the physical indices read from the given storage during the given
 * group iterations./*from ww  w.  j  a v  a2s  .  c o m*/
 * @param s the storage being read from
 * @param iterations the group iterations
 * @return the physical indices read
 */
public ImmutableSortedSet<Integer> reads(Storage s, Range<Integer> iterations) {
    iterations = iterations.canonical(DiscreteDomain.integers());
    ImmutableSortedSet.Builder<Integer> builder = ImmutableSortedSet.naturalOrder();
    for (Actor a : actors())
        builder.addAll(a.reads(s, Range.closedOpen(iterations.lowerEndpoint() * schedule.get(a),
                iterations.upperEndpoint() * schedule.get(a))));
    return builder.build();
}

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 ww  .ja v  a  2s  .  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;
}

From source file:org.dishevelled.bio.align.Alignments.java

/**
 * Return the gaps in the specified gapped symbol list as 0-based [closed, open) ranges.
 *
 * @param gappedSymbols gapped symbol list, must not be null
 * @return the gaps in the specified gapped symbol list as 0-based [closed, open) ranges
 *//*w  w  w  . ja v a  2s .co  m*/
public static List<Range<Long>> gaps(final GappedSymbolList gappedSymbols) {
    checkNotNull(gappedSymbols);
    List<Range<Long>> gaps = new ArrayList<Range<Long>>();
    int gapStart = -1;
    for (int i = 1, length = gappedSymbols.length() + 1; i < length; i++) {
        if (isGapSymbol(gappedSymbols.symbolAt(i))) {
            if (gapStart < 0) {
                gapStart = i;
            }
        } else {
            if (gapStart > 0) {
                // biojava coordinates are 1-based
                gaps.add(Range.closedOpen(Long.valueOf(gapStart - 1L), Long.valueOf(i - 1L)));
                gapStart = -1;
            }
        }
    }
    if (gapStart > 0) {
        gaps.add(Range.closedOpen(Long.valueOf(gapStart - 1L), Long.valueOf(gappedSymbols.length())));
    }
    return gaps;
}

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

private void triggerUpdate(RangeSet<Integer> range) {
    Range<Integer> all = Range.closedOpen(Integer.valueOf(0), Integer.valueOf(getNumberOfLines()));
    RangeSet<Integer> onlyExisting = range.subRangeSet(all);
    RangeSet<Integer> onlyVisible = onlyExisting.subRangeSet(getVisible());

    onlyVisible.asRanges().stream().flatMapToInt(VFlow::toIntStream).forEach(index -> updateNode(index));
}

From source file:com.google.testing.pogen.parser.template.TemplateParser.java

/**
 * Returns a {@link RangeSet} of indexes between the start and the end tags
 * for nested tags. If there're nested tags, returns the outermost ranges.
 * /*w ww  . j  a va2 s  . c o m*/
 * @param text
 *            the string to be parsed
 * @param startPattern
 *            the regular expression of start tags to find
 * @param endPattern
 *            the regular expression of end tags to find
 * @return the {@link RangeSet} of the indexes between the start and the end
 *         tags
 * @throws TemplateParseException
 *             if the specified template is in bad format where broken pairs
 *             of start and end tags appear
 */
protected static RangeSet<Integer> getIndexRangesOfNestedTags(String text, Pattern startPattern,
        Pattern endPattern) throws TemplateParseException {
    RangeSet<Integer> rangeSet = TreeRangeSet.create();

    List<Integer> startIndexes = getMatchedIndexes(text, startPattern);
    List<Integer> endIndexes = getMatchedIndexes(text, endPattern);

    int startIndex = 0, endIndex = 0;
    // Check whether the sizes of start tags and end tags are equal
    if (startIndexes.size() != endIndexes.size()) {
        throw new TemplateParseException(String.format(
                "The sizes of start tags and end tags are not equal (#start tags: %d, #end tags: %d).",
                startIndexes.size(), endIndexes.size()));
    }

    int depth = 0, lastStartIndex = 0;
    while (endIndex < endIndexes.size()) {
        boolean consumedStartIndexes = startIndex == startIndexes.size();
        // Process a previous index before a next index
        if (consumedStartIndexes || endIndexes.get(endIndex) < startIndexes.get(startIndex)) {
            if (--depth <= 0) {
                if (depth < 0) {
                    // Check whether all start tags and end tags are paired
                    // correctly
                    throw new TemplateParseException(
                            String.format("Broken pairs of start and end tags are found."));
                }
                rangeSet.add(Range.closedOpen(lastStartIndex, endIndexes.get(endIndex)));
            }
            endIndex++;
        } else {
            if (depth++ == 0) {
                lastStartIndex = startIndexes.get(startIndex);
            }
            startIndex++;
        }
    }
    return rangeSet;
}

From source file:org.cinchapi.concourse.server.model.Ranges.java

/**
 * Return the ranges that include the points that are in {@code a} or the
 * {@code b} one and not in their intersection. The return set will include
 * between 0 and 2 ranges that together include all the points that meet
 * this criteria./*w w  w  .  jav  a2s.  c  o m*/
 * <p>
 * <strong>NOTE:</strong> If the two ranges do not intersect, then a
 * collection containing both of them is returned (since they already form
 * their xor).
 * </p>
 * 
 * @param a
 * @param b
 * @return the set or ranges that make uValue the symmetric difference
 *         between this range and the {@code other} one
 */
public static Iterable<Range<Value>> xor(Range<Value> a, Range<Value> b) {
    List<Range<Value>> ranges = Lists.newArrayList();
    try {
        Range<Value> intersection = a.intersection(b);
        boolean aStart = compareToLower(a, b) < 0;
        boolean aEnd = compareToUpper(a, b) > 0;
        boolean lower = getLowerBoundType(aStart ? a : b) == BoundType.CLOSED;
        boolean upper = getUpperBoundType(aEnd ? a : b) == BoundType.CLOSED;
        boolean interLower = getLowerBoundType(intersection) == BoundType.OPEN;
        boolean interUpper = getUpperBoundType(intersection) == BoundType.OPEN;
        Range<Value> first;
        if (lower && interLower) {
            first = Range.closed(getLowerEndpoint(aStart ? a : b), getLowerEndpoint(intersection));
        } else if (!lower && interLower) {
            first = Range.openClosed(getLowerEndpoint(aStart ? a : b), getLowerEndpoint(intersection));
        } else if (lower && !interLower) {
            first = Range.closedOpen(getLowerEndpoint(aStart ? a : b), getLowerEndpoint(intersection));
        } else {
            first = Range.open(getLowerEndpoint(aStart ? a : b), getLowerEndpoint(intersection));
        }
        Range<Value> second;
        if (interUpper && upper) {
            second = Range.closed(getUpperEndpoint(intersection), getUpperEndpoint(aEnd ? a : b));
        } else if (!interUpper && upper) {
            second = Range.openClosed(getUpperEndpoint(intersection), getUpperEndpoint(aEnd ? a : b));
        } else if (interUpper && !interUpper) {
            second = Range.closedOpen(getUpperEndpoint(intersection), getUpperEndpoint(aEnd ? a : b));
        } else {
            second = Range.open(getUpperEndpoint(intersection), getUpperEndpoint(aEnd ? a : b));
        }
        if (!first.isEmpty()) {
            ranges.add(first);
        }
        if (!second.isEmpty()) {
            ranges.add(second);
        }
    } catch (IllegalArgumentException e) { // ranges dont intersect
        ranges.add(a);
        ranges.add(b);
    }
    return ranges;
}

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

/**
 * Returns the logical indices popped on the given input during the given
 * iteration.//from  www  . j a  v a  2  s  .  c  om
 * @param input the input index
 * @param iteration the iteration number
 * @return the logical indices popped on the given input during the given
 * iteration
 */
public ContiguousSet<Integer> pops(int input, int iteration) {
    return ContiguousSet.create(
            Range.closedOpen(iteration * pop(input).max(), (iteration + 1) * pop(input).max()),
            DiscreteDomain.integers());
}

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

/**
 * Returns a set containing the indices live before the initialization
 * schedule; that is, the indices holding initial data. (Note that, as a
 * span, not every contained index will be occupied.) The returned range
 * will be//from  w w w .j ava 2  s  .  c o m
 * {@link Range#canonical(com.google.common.collect.DiscreteDomain) canonical}.
 * The range is not cached so as to be responsive to changes in initial data
 * index functions.
 * @return a range spanning the indices holding initial data under the
 * current index functions
 * @see #initialDataIndices()
 */
public Range<Integer> initialDataIndexSpan() {
    Range<Integer> range = null;
    for (Pair<ImmutableList<Object>, IndexFunction> p : initialData())
        for (int i = 0; i < p.first.size(); ++i)
            try {
                int x = p.second.applyAsInt(i);
                range = (range == null) ? Range.singleton(x) : range.span(Range.singleton(x));
            } catch (Throwable ex) {
                throw new AssertionError("index functions should not throw", ex);
            }
    range = (range != null ? range : Range.closedOpen(0, 0));
    return range.canonical(DiscreteDomain.integers());
}

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

/**
 * Returns the physical indices written to the given storage during the
 * given group iteration.//from w w  w.j  a v a  2  s .  c om
 * @param s the storage being written to
 * @param iteration the group iteration number
 * @return the physical indices written
 */
public ImmutableSortedSet<Integer> writes(Storage s, int iteration) {
    ImmutableSortedSet.Builder<Integer> builder = ImmutableSortedSet.naturalOrder();
    for (Actor a : actors())
        builder.addAll(
                a.writes(s, Range.closedOpen(iteration * schedule.get(a), (iteration + 1) * schedule.get(a))));
    return builder.build();
}