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

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

Introduction

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

Prototype

public boolean contains(C value) 

Source Link

Document

Returns true if value is within the bounds of this range.

Usage

From source file:org.terasology.dynamicCities.utilities.ProbabilityDistribution.java

public T get() {
    float random = rng.nextFloat(0, 1);
    for (Range<Float> range : ranges.keySet()) {
        if (range.contains(random)) {
            return ranges.get(range);
        }//from   ww  w .jav a 2s.com
    }
    logger.error("Could not retrieve a valid value with a random number value of " + random);
    return null;
}

From source file:com.github.fge.grappa.buffers.LineCounter.java

private int doBinarySearch(int low, int high, int index) {
    // Guaranteed to always succeed at this point
    if (high - low <= 1)
        return lines.get(low).contains(index) ? low : high;

    int middle = (low + high) / 2;
    Range<Integer> range = lines.get(middle);
    if (range.contains(index))
        return middle;

    return index < range.lowerEndpoint() ? doBinarySearch(low, middle, index)
            : doBinarySearch(middle, high, index);
}

From source file:com.github.parboiled1.grappa.backport.buffers.LineCounter.java

private int doBinarySearch(final int low, final int high, final int index) {
    // Guaranteed to always succeed at this point
    if (high - low <= 1)
        return lines.get(low).contains(index) ? low : high;

    final int middle = (low + high) / 2;
    final Range<Integer> range = lines.get(middle);
    if (range.contains(index))
        return middle;

    return index < range.lowerEndpoint() ? doBinarySearch(low, middle, index)
            : doBinarySearch(middle, high, index);
}

From source file:org.robotframework.ide.eclipse.main.plugin.project.build.fix.RemoveVariableFixer.java

@Override
public Optional<ICompletionProposal> asContentProposal(final IMarker marker, final IDocument document,
        final RobotSuiteFile suiteModel) {
    if (variableName == null) {
        return Optional.empty();
    }/*  w w w .j  a va 2s  .  co m*/
    final Optional<RobotVariablesSection> section = suiteModel.findSection(RobotVariablesSection.class);
    if (!section.isPresent()) {
        return Optional.empty();
    }
    for (final RobotVariable variable : section.get().getChildren()) {
        final Range<Integer> defRange = getRange(marker);
        if (defRange.contains(variable.getDefinitionPosition().getOffset())) {
            try {
                return createProposal(document, variable);
            } catch (final BadLocationException e) {
                return Optional.empty();
            }
        }
    }
    return Optional.empty();
}

From source file:org.robotframework.ide.eclipse.main.plugin.project.build.fix.RemoveKeywordFixer.java

@Override
public Optional<ICompletionProposal> asContentProposal(final IMarker marker, final IDocument document,
        final RobotSuiteFile suiteModel) {
    if (keywordName == null) {
        return Optional.empty();
    }/*from   w w  w  .j  av  a 2 s .c o  m*/
    final Optional<RobotKeywordsSection> section = suiteModel.findSection(RobotKeywordsSection.class);
    if (!section.isPresent()) {
        return Optional.empty();
    }
    for (final RobotKeywordDefinition keyword : section.get().getChildren()) {
        final Range<Integer> defRange = getRange(marker);
        if (defRange.contains(keyword.getDefinitionPosition().getOffset())) {
            try {
                return createProposal(document, keyword);
            } catch (final BadLocationException e) {
                return Optional.empty();
            }
        }
    }
    return Optional.empty();
}

From source file:org.eclipse.smarthome.transform.scale.internal.ScaleTransformationService.java

/**
 * <p>/* w w w.j  av  a 2  s.c o  m*/
 * Transforms the input <code>source</code> by matching searching the range where it fits
 * i.e. [min..max]=value or ]min..max]=value
 * </p>
 *
 * @param properties
 *            the list of properties defining all the available ranges
 * @param source
 *            the input to transform
 *
 * @{inheritDoc
 *
 */
@Override
protected String internalTransform(Map<Range<Double>, String> data, String source)
        throws TransformationException {

    try {
        Double value = new Double(source);

        for (Range<Double> range : data.keySet()) {
            if (range.contains(value)) {
                return data.get(range);
            }
        }

        throw new TransformationException("No matching range for '" + source + "'");
    } catch (NumberFormatException e) {
        throw new TransformationException("Scale can only be used with numeric inputs");
    }
}

From source file:org.sonatype.sisu.bl.support.port.internal.DefaultPortReservationService.java

private boolean isBlocked(int port) {
    for (Range r : this.blockedPortRanges) {
        if (r.contains(port)) {
            return true;
        }//from ww w.  j  a  va  2  s.  c  om
    }
    return blockedPorts.contains(port);
}

From source file:net.opentsdb.contrib.tsquare.web.view.DataPointsTimeGrouper.java

public Double getThenAdvanceIfWithinRange(final Range<Long> timestampRange) {
    if (exhausted) {
        return VALUE_WHEN_EXHAUSTED;
    } else if (timestampRange.contains(currentDataPoint.timestamp())) {
        final double value = currentDataPoint.toDouble();
        advance();/*  ww  w .  j  a v  a2  s.  c om*/
        return Double.valueOf(value);
    } else {
        return null;
    }
}

From source file:org.onos.yangtools.yang.data.impl.codec.AbstractIntegerStringCodec.java

private void validate(final N value) {
    if (rangeConstraints.isEmpty()) {
        return;/*  w w  w .  j  ava  2  s .  co m*/
    }
    for (final Range<N> constraint : rangeConstraints) {
        if (constraint.contains(value)) {
            return;
        }
    }
    throw new IllegalArgumentException("Value '" + value + "'  is not in required range " + rangeConstraints);
}

From source file:org.rf.ide.core.testdata.model.table.exec.CommentedVariablesFilter.java

public boolean isInCommentedPart(final RobotFileOutput rfo, final RobotToken token) {
    boolean result = false;

    Optional<Integer> startCommentRange = Optional.empty();
    final RobotFile fileModel = rfo.getFileModel();
    final int tokenOffset = token.getStartOffset();
    final Optional<Integer> robotLineIndex = fileModel.getRobotLineIndexBy(tokenOffset);
    if (robotLineIndex.isPresent()) {
        final RobotLine robotLine = fileModel.getFileContent().get(robotLineIndex.get());
        final List<IRobotLineElement> lineElements = robotLine.getLineElements();
        for (final IRobotLineElement lineElem : lineElements) {
            final List<IRobotTokenType> types = lineElem.getTypes();
            if (types.contains(RobotTokenType.START_HASH_COMMENT)
                    || types.contains(RobotTokenType.COMMENT_CONTINUE)) {
                startCommentRange = Optional.of(lineElem.getStartOffset());
                break;
            }//from  w  ww .  j a  v  a  2  s .  c om
        }

        if (startCommentRange.isPresent()) {
            final Range<Integer> range = Range.closed(startCommentRange.get(),
                    robotLine.getEndOfLine().getStartOffset());
            result = range.contains(tokenOffset);
        }
    }

    return result;
}