Example usage for com.google.common.collect Iterators get

List of usage examples for com.google.common.collect Iterators get

Introduction

In this page you can find the example usage for com.google.common.collect Iterators get.

Prototype

public static <T> T get(Iterator<T> iterator, int position) 

Source Link

Document

Advances iterator position + 1 times, returning the element at the position th position.

Usage

From source file:org.jnario.lib.JnarioIteratorExtensions.java

/**
  * Returns the element at the specified position in the arguments.
  */*ww  w  .ja  v  a  2  s  .c o m*/
  * @param index index of the element to return
  * @return the element at the specified position in this argument list
  * @throws IndexOutOfBoundsException if the index is out of range
  *         (<tt>index &lt; 0 || index &gt;= size()</tt>)
  */
public static <T> T get(Iterator<T> iterator, int index) {
    return Iterators.get(iterator, index);
}

From source file:io.druid.client.selector.RandomServerSelectorStrategy.java

@Override
public QueryableDruidServer pick(Set<QueryableDruidServer> servers, DataSegment segment) {
    return Iterators.get(servers.iterator(), random.nextInt(servers.size()));
}

From source file:org.apache.druid.client.selector.RandomServerSelectorStrategy.java

@Override
public QueryableDruidServer pick(Set<QueryableDruidServer> servers, DataSegment segment) {
    return Iterators.get(servers.iterator(), ThreadLocalRandom.current().nextInt(servers.size()));
}

From source file:org.matsim.contrib.dynagent.examples.random.RandomDynAgentLogic.java

static <E> E chooseRandomElement(Set<E> set) {
    int randomIndex = MatsimRandom.getRandom().nextInt(set.size());
    return Iterators.get(set.iterator(), randomIndex);
}

From source file:de.faustedition.facsimile.FacsimileTile.java

public ImageReader createImageReader() throws IOException {
    final ImageInputStream imageInputStream = ImageIO.createImageInputStream(file);
    final ImageReader imageReader = Iterators.get(ImageIO.getImageReaders(imageInputStream), 0);
    imageReader.setInput(imageInputStream);
    return imageReader;
}

From source file:org.opendaylight.yangtools.yang.data.api.StackedReversePathArguments.java

@Override
public PathArgument get(final int index) {
    return Iterators.get(iterator(), index);
}

From source file:org.ow2.sirocco.cloudmanager.api.openstack.server.resources.nova.ServerActions.java

@Override
public Response action(InputStream stream) {
    if (stream == null) {
        return badRequest("ServerAction", "undefined : empty payload");
    }//from  w w w.  j a  v a  2s.  co  m

    ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = null; // src can be a File, URL, InputStream etc
    try {
        rootNode = mapper.readValue(stream, JsonNode.class);
    } catch (IOException e) {
        return computeFault(500, "Error while reading JSON", e.getMessage());
    }

    String actionName = Iterators.get(rootNode.getFieldNames(), 0);
    Action action = getAction(actionName);
    if (action == null) {
        return computeFault(500, "Can not retrieve action", "Action not found for " + actionName);
    } else {
        return action.invoke(getServerId(), rootNode);
    }
}

From source file:at.ac.univie.isc.asio.CaptureEvents.java

/**
 * Get the i-th captured event of the expected type.
 *//*from w w w .  j a  va2  s . c o  m*/
public EVENT get(final int index) {
    assert index < captured.size();
    return Iterators.get(captured().iterator(), index);
}

From source file:com.blurengine.blur.modules.filters.lexer.FilterRecursiveDescentParser.java

private Filter factor() {
    Filter result = null;/* w w w  . j av a 2  s  .  c o  m*/

    next();
    if (current == null) {
        throw new RuntimeException("Expression Malformed: " + this.input);
    }
    switch (current.getType()) {
    case PLAINTEXT:
        String filterId = current.getData().trim();
        check(!filterId.isEmpty(), "Filter name is somehow empty.");

        if (this.index < this.tokens.size() - 1) {
            // Peek to make sure syntax is correct.
            Token token = Iterators.get(this.tokens.iterator(), index + 1);
            check(token.getType() != TokenType.PLAINTEXT, "A token must follow a filter.");
        }
        result = filterIdGetter.apply(filterId);
        Preconditions.checkArgument(result != null, "Filter id '%s' not found.", filterId);
        next();
        return result;
    case TRUE:
        result = Filters.ALWAYS_ALLOW;
        next();
        return result;
    case FALSE:
        result = Filters.ALWAYS_DENY;
        next();
        return result;
    case NOT:
        return factor().inverse();
    case LEFTPAREN:
        result = expression();
        next();
        return result;
    }
    return result;
}

From source file:com.ansorgit.plugins.bash.lang.psi.util.BashAbstractProcessor.java

/**
 * Returns the best results. It takes all the elements which have been rated the best
 * and returns the first / last, depending on the parameter.
 *
 * @param results          The results to check
 * @param firstResult      If the first element of the best element list should be returned.
 * @param referenceElement//from w ww  .  java 2  s.  c  o m
 * @return The result
 */
private PsiElement findBestResult(Multimap<Integer, PsiElement> results, boolean firstResult,
        PsiElement referenceElement) {
    if (!hasResults()) {
        return null;
    }

    if (firstResult) {
        return Iterators.get(results.values().iterator(), 0);
    }

    //if the first should not be used return the best element
    int referenceLevel = preferNeigbourhood && (referenceElement != null)
            ? BashPsiUtils.blockNestingLevel(referenceElement)
            : 0;

    // find the best suitable result rating
    // The best one is as close as possible to the given referenceElement
    int bestRating = Integer.MAX_VALUE;
    int bestDelta = Integer.MAX_VALUE;
    for (int rating : results.keySet()) {
        final int delta = Math.abs(referenceLevel - rating);
        if (delta < bestDelta) {
            bestDelta = delta;
            bestRating = rating;
        }
    }

    // now get the best result
    // if there are equal definitions on the same level we prefer the first if the neighbourhood is not preferred
    if (preferNeigbourhood) {
        return Iterators.getLast(results.get(bestRating).iterator());
    } else {
        //return the element which has the lowest textOffset
        long smallestOffset = Integer.MAX_VALUE;
        PsiElement bestElement = null;

        for (PsiElement e : results.get(bestRating)) {
            //if the element is injected compute the text offset in the real file
            int textOffset = e.getTextOffset();
            if (BashPsiUtils.isInjectedElement(e)) {
                //fixme optimize this
                PsiLanguageInjectionHost injectionHost = InjectedLanguageManager.getInstance(e.getProject())
                        .getInjectionHost(e);
                if (injectionHost != null) {
                    textOffset = textOffset + injectionHost.getTextOffset();
                }
            }

            if (textOffset < smallestOffset) {
                smallestOffset = textOffset;
                bestElement = e;
            }
        }

        return bestElement;
    }
}