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

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

Introduction

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

Prototype

public boolean encloses(Range<C> other) 

Source Link

Document

Returns true if the bounds of other do not extend outside the bounds of this range.

Usage

From source file:jetbrains.jetpad.projectional.view.ScrollUtil.java

static int moveDelta(Range<Integer> container, Range<Integer> range) {
    if (container.encloses(range))
        return 0;
    if (container.upperEndpoint() - container.lowerEndpoint() < range.upperEndpoint() - range.lowerEndpoint()) {
        return container.lowerEndpoint() - range.lowerEndpoint();
    }//from  w  ww.  jav a 2  s .  c o  m

    if (container.contains(range.upperEndpoint())) {
        return container.lowerEndpoint() - range.lowerEndpoint();
    }

    if (container.contains(range.lowerEndpoint())) {
        return container.upperEndpoint() - range.upperEndpoint();
    }

    if (container.upperEndpoint() < range.lowerEndpoint()) {
        return container.upperEndpoint() - range.upperEndpoint();
    } else if (container.lowerEndpoint() > range.upperEndpoint()) {
        return container.lowerEndpoint() - range.lowerEndpoint();
    } else {
        throw new IllegalStateException("This can't happen");
    }
}

From source file:org.apache.aurora.scheduler.cron.CrontabEntry.java

private static void checkEnclosed(String fieldName, Range<Integer> fieldEnclosure, RangeSet<Integer> field) {

    checkArgument(fieldEnclosure.encloses(field.span()),
            String.format("Bad specification for field %s: span(%s) = %s is not enclosed by boundary %s.",
                    fieldName, field, field.span(), fieldEnclosure));
}

From source file:org.apache.hadoop.hive.ql.optimizer.calcite.druid.DruidIntervalUtils.java

protected static List<Range> condenseRanges(List<Range> ranges) {
    if (ranges.size() <= 1) {
        return ranges;
    }/*from www  .  j  a  va2 s.co  m*/

    Comparator<Range> startThenEnd = new Comparator<Range>() {
        @Override
        public int compare(Range lhs, Range rhs) {
            int compare = 0;
            if (lhs.hasLowerBound() && rhs.hasLowerBound()) {
                compare = lhs.lowerEndpoint().compareTo(rhs.lowerEndpoint());
            } else if (!lhs.hasLowerBound() && rhs.hasLowerBound()) {
                compare = -1;
            } else if (lhs.hasLowerBound() && !rhs.hasLowerBound()) {
                compare = 1;
            }
            if (compare != 0) {
                return compare;
            }
            if (lhs.hasUpperBound() && rhs.hasUpperBound()) {
                compare = lhs.upperEndpoint().compareTo(rhs.upperEndpoint());
            } else if (!lhs.hasUpperBound() && rhs.hasUpperBound()) {
                compare = -1;
            } else if (lhs.hasUpperBound() && !rhs.hasUpperBound()) {
                compare = 1;
            }
            return compare;
        }
    };

    TreeSet<Range> sortedIntervals = Sets.newTreeSet(startThenEnd);
    sortedIntervals.addAll(ranges);

    List<Range> retVal = Lists.newArrayList();

    Iterator<Range> intervalsIter = sortedIntervals.iterator();
    Range currInterval = intervalsIter.next();
    while (intervalsIter.hasNext()) {
        Range next = intervalsIter.next();
        if (currInterval.encloses(next)) {
            continue;
        }
        if (mergeable(currInterval, next)) {
            currInterval = currInterval.span(next);
        } else {
            retVal.add(currInterval);
            currInterval = next;
        }
    }
    retVal.add(currInterval);

    return retVal;
}

From source file:net.bican.iplib.IPAddresses.java

private static Set<Range<IPAddress>> fromConnectedInterval(final Range<IPAddress> interval) {
    if (interval.isEmpty()) {
        return null;
    }/*from   ww w .ja  v a 2 s  .  c  om*/
    int prefix = 0;
    final LongDiscreteDomain<IPAddress> domain = interval.lowerEndpoint().getDomain();
    while (prefix <= domain.maxPrefix()) {
        final Range<IPAddress> thisRange = IPAddresses.canonical(interval, domain);
        final Range<IPAddress> otherRange = IPAddresses.fromCIDR(new CIDR(thisRange.lowerEndpoint(), prefix));
        if (thisRange.equals(otherRange)) {
            TreeSet<Range<IPAddress>> result = new TreeSet<>(IPAddressRangeComparator.getComparator());
            result.add(otherRange);
            return result;
        } else if (thisRange.encloses(otherRange)) {
            final Set<Range<IPAddress>> result = new TreeSet<>(IPAddressRangeComparator.getComparator());
            result.add(otherRange);
            Range<IPAddress> newRange1 = Range.closedOpen(thisRange.lowerEndpoint(),
                    otherRange.lowerEndpoint());
            Range<IPAddress> newRange2 = Range.openClosed(otherRange.upperEndpoint(),
                    thisRange.upperEndpoint());
            final Set<Range<IPAddress>> results1 = IPAddresses.fromConnectedInterval(newRange1);
            if (results1 != null) {
                result.addAll(results1);
            }
            final Set<Range<IPAddress>> results2 = IPAddresses.fromConnectedInterval(newRange2);
            if (results2 != null) {
                result.addAll(results2);
            }
            return result;
        }
        prefix++;
    }
    return new TreeSet<>(Collections.singleton(interval));
}

From source file:com.wandrell.tabletop.interval.util.IntervalArithmeticsUtils.java

/**
 * Indicates if an interval contains a value.
 * /*from   w  w  w .  ja  v a2s .c  om*/
 * @param interval
 *            the interval for the check
 * @param value
 *            the value to check
 * @return {@code true} if the value is contained, {@code false} otherwise
 */
public static final Boolean isContaining(final Interval interval, final Integer value) {
    final Range<Integer> rangeA;
    final Range<Integer> rangeB;

    checkNotNull(interval, "Received a null pointer as interval");
    checkNotNull(value, "Received a null pointer as value");

    checkArgument(isValid(interval), "Received an invalid interval");

    rangeA = Range.closed(interval.getLowerLimit(), interval.getUpperLimit());
    rangeB = Range.singleton(value);

    return rangeA.encloses(rangeB);
}

From source file:eu.trentorise.opendata.semtext.SemText.java

/**
 * Returns the terms enclosed by enclosingSpan as a new TreeRangeMap.
 *//*  www .  j  av  a 2  s . c  om*/
private static TreeRangeMap<Integer, Term> enclosingNewTerms(Span enclosingSpan,
        RangeMap<Integer, Term> termRanges) {
    TreeRangeMap<Integer, Term> ret = TreeRangeMap.create();

    Range enclosingRange = spanToRange(enclosingSpan);

    RangeMap<Integer, Term> subRangeMap = termRanges.subRangeMap(enclosingRange);

    for (Map.Entry<Range<Integer>, Term> termEntry : subRangeMap.asMapOfRanges().entrySet()) {
        if (enclosingRange.encloses(spanToRange(termEntry.getValue()))) {
            ret.put(termEntry.getKey(), termEntry.getValue());
        }
    }

    return ret;
}

From source file:com.wandrell.tabletop.interval.util.IntervalArithmeticsUtils.java

/**
 * Indicates if an interval contains another.
 * /*ww w  .j av  a 2  s. c o m*/
 * @param containing
 *            the interval which may be containing the other
 * @param contained
 *            the interval which may be contained
 * @return {@code true} if the interval is contained, {@code false}
 *         otherwise
 */
public static final Boolean isContaining(final Interval containing, final Interval contained) {
    final Range<Integer> rangeContaining;
    final Range<Integer> rangeContained;

    checkNotNull(containing, "Received a null pointer as the first interval");
    checkNotNull(contained, "Received a null pointer as the second interval");

    checkArgument(isValid(containing), "Received an invalid interval as the first argument");
    checkArgument(isValid(contained), "Received an invalid interval as the second argument");

    rangeContaining = Range.closed(containing.getLowerLimit(), containing.getUpperLimit());
    rangeContained = Range.closed(contained.getLowerLimit(), contained.getUpperLimit());

    return rangeContaining.encloses(rangeContained);
}

From source file:org.robotframework.ide.eclipse.main.plugin.assist.ProposalMatch.java

Optional<ProposalMatch> mapAndShiftToFragment(final int startIndex, final int length) {
    final List<Range<Integer>> rangesInDomain = new ArrayList<>();

    final Range<Integer> targetDomain = Range.closedOpen(startIndex, startIndex + length);
    for (final Range<Integer> match : this) {
        if (match.encloses(targetDomain)) {
            rangesInDomain.add(targetDomain);
        } else if (targetDomain.encloses(match)) {
            rangesInDomain.add(match);/*from w w w .  j  a v a  2  s.  c o m*/
        } else if (match.lowerEndpoint().intValue() <= startIndex
                && startIndex <= match.upperEndpoint().intValue()) {
            rangesInDomain.add(Range.closedOpen(startIndex, match.upperEndpoint()));
        } else if (match.lowerEndpoint().intValue() <= startIndex + length
                && startIndex + startIndex <= match.upperEndpoint().intValue()) {
            rangesInDomain.add(Range.closedOpen(match.lowerEndpoint(), startIndex + length));
        }
    }

    final List<Range<Integer>> newMatches = new ArrayList<>();
    for (final Range<Integer> match : rangesInDomain) {
        newMatches
                .add(Range.closedOpen(match.lowerEndpoint() - startIndex, match.upperEndpoint() - startIndex));
    }
    return newMatches.isEmpty() ? Optional.<ProposalMatch>empty() : Optional.of(new ProposalMatch(newMatches));
}

From source file:com.comphenix.protocol.RangeParser.java

/**
 * Parse ranges from an array of elements.
 * @param args - array of elements.//from  w w w  .java  2s. c o m
 * @param offset - beginning offset.
 * @param lastIndex - the last index of the array to read.
 * @param legalRange - range of legal values.
 * @return The parsed ranges.
 */
public static List<Range<Integer>> getRanges(Deque<String> input, Range<Integer> legalRange) {
    List<String> tokens = tokenizeInput(input);
    List<Range<Integer>> ranges = new ArrayList<Range<Integer>>();

    for (int i = 0; i < tokens.size(); i++) {
        Range<Integer> range;
        String current = tokens.get(i);
        String next = i + 1 < tokens.size() ? tokens.get(i + 1) : null;

        // Yoda equality is done for null-safety
        if ("-".equals(current)) {
            throw new IllegalArgumentException("A hyphen must appear between two numbers.");
        } else if ("-".equals(next)) {
            if (i + 2 >= tokens.size())
                throw new IllegalArgumentException("Cannot form a range without a upper limit.");

            // This is a proper range
            range = Range.closed(Integer.parseInt(current), Integer.parseInt(tokens.get(i + 2)));
            ranges.add(range);

            // Skip the two next tokens
            i += 2;

        } else {
            // Just a single number
            range = Range.singleton(Integer.parseInt(current));
            ranges.add(range);
        }

        // Validate ranges
        if (!legalRange.encloses(range)) {
            throw new IllegalArgumentException(range + " is not in the range " + range.toString());
        }
    }

    return simplify(ranges, legalRange.upperEndpoint());
}

From source file:com.giaybac.traprange.extractor.PDFTableExtractor.java

/**
 *
 * Remove all texts in excepted lines/*from ww w.j  a  v  a2  s.c  o m*/
 *
 * TexPositions are sorted by .getY() ASC
 *
 * @param lineRanges
 * @param textPositions
 * @return
 */
private List<TextPosition> getTextsByLineRanges(List<Range<Integer>> lineRanges,
        List<TextPosition> textPositions) {
    List<TextPosition> retVal = new ArrayList<>();
    int idx = 0;
    int lineIdx = 0;
    while (idx < textPositions.size() && lineIdx < lineRanges.size()) {
        TextPosition textPosition = textPositions.get(idx);
        Range<Integer> textRange = Range.closed((int) textPosition.getY(),
                (int) (textPosition.getY() + textPosition.getHeight()));
        Range<Integer> lineRange = lineRanges.get(lineIdx);
        if (lineRange.encloses(textRange)) {
            retVal.add(textPosition);
            idx++;
        } else if (lineRange.upperEndpoint() < textRange.lowerEndpoint()) {
            lineIdx++;
        } else {
            idx++;
        }
    }
    //return
    return retVal;
}