Example usage for com.google.common.collect PeekingIterator remove

List of usage examples for com.google.common.collect PeekingIterator remove

Introduction

In this page you can find the example usage for com.google.common.collect PeekingIterator remove.

Prototype

@Override
void remove();

Source Link

Document

Implementations may or may not support removal when a call to #peek() has occurred since the most recent call to #next() .

Usage

From source file:org.renjin.eval.Calls.java

/**
 * Argument matching is done by a three-pass process:
 * <ol>/*from w  w w  . java 2s  .  c om*/
 * <li><strong>Exact matching on tags.</strong> For each named supplied argument the list of formal arguments
 *  is searched for an item whose name matches exactly. It is an error to have the same formal
 * argument match several actuals or vice versa.</li>
 *
 * <li><strong>Partial matching on tags.</strong> Each remaining named supplied argument is compared to the
 * remaining formal arguments using partial matching. If the name of the supplied argument
 * matches exactly with the first part of a formal argument then the two arguments are considered
 * to be matched. It is an error to have multiple partial matches.
 *  Notice that if f <- function(fumble, fooey) fbody, then f(f = 1, fo = 2) is illegal,
 * even though the 2nd actual argument only matches fooey. f(f = 1, fooey = 2) is legal
 * though since the second argument matches exactly and is removed from consideration for
 * partial matching. If the formal arguments contain ... then partial matching is only applied to
 * arguments that precede it.
 *
 * <li><strong>Positional matching.</strong> Any unmatched formal arguments are bound to unnamed supplied arguments,
 * in order. If there is a ... argument, it will take up the remaining arguments, tagged or not.
 * If any arguments remain unmatched an error is declared.
 *
 * @param actuals the actual arguments supplied to the list
 * @param populateMissing
 */
public static PairList matchArguments(PairList formals, PairList actuals, boolean populateMissing) {

    PairList.Builder result = new PairList.Builder();

    List<PairList.Node> unmatchedActuals = Lists.newArrayList();
    for (PairList.Node argNode : actuals.nodes()) {
        unmatchedActuals.add(argNode);
    }

    List<PairList.Node> unmatchedFormals = Lists.newArrayList(formals.nodes());

    // do exact matching
    for (ListIterator<PairList.Node> formalIt = unmatchedFormals.listIterator(); formalIt.hasNext();) {
        PairList.Node formal = formalIt.next();
        if (formal.hasTag()) {
            Symbol name = formal.getTag();
            Collection<PairList.Node> matches = Collections2.filter(unmatchedActuals,
                    PairList.Predicates.matches(name));

            if (matches.size() == 1) {
                PairList.Node match = first(matches);
                result.add(name, match.getValue());
                formalIt.remove();
                unmatchedActuals.remove(match);

            } else if (matches.size() > 1) {
                throw new EvalException(
                        String.format("Multiple named values provided for argument '%s'", name.getPrintName()));
            }
        }
    }

    // do partial matching
    Collection<PairList.Node> remainingNamedFormals = filter(unmatchedFormals, PairList.Predicates.hasTag());
    for (Iterator<PairList.Node> actualIt = unmatchedActuals.iterator(); actualIt.hasNext();) {
        PairList.Node actual = actualIt.next();
        if (actual.hasTag()) {
            Collection<PairList.Node> matches = Collections2.filter(remainingNamedFormals,
                    PairList.Predicates.startsWith(actual.getTag()));

            if (matches.size() == 1) {
                PairList.Node match = first(matches);
                result.add(match.getTag(), actual.getValue());
                actualIt.remove();
                unmatchedFormals.remove(match);

            } else if (matches.size() > 1) {
                throw new EvalException(
                        String.format("Provided argument '%s' matches multiple named formal arguments: %s",
                                actual.getTag().getPrintName(), argumentTagList(matches)));
            }
        }
    }

    // match any unnamed args positionally

    Iterator<PairList.Node> formalIt = unmatchedFormals.iterator();
    PeekingIterator<PairList.Node> actualIt = Iterators.peekingIterator(unmatchedActuals.iterator());
    while (formalIt.hasNext()) {
        PairList.Node formal = formalIt.next();
        if (Symbols.ELLIPSES.equals(formal.getTag())) {
            PromisePairList.Builder promises = new PromisePairList.Builder();
            while (actualIt.hasNext()) {
                PairList.Node actual = actualIt.next();
                promises.add(actual.getRawTag(), actual.getValue());
            }
            result.add(formal.getTag(), promises.build());

        } else if (hasNextUnTagged(actualIt)) {
            result.add(formal.getTag(), nextUnTagged(actualIt).getValue());

        } else if (populateMissing) {
            result.add(formal.getTag(), Symbol.MISSING_ARG);
        }
    }
    if (actualIt.hasNext()) {
        throw new EvalException(String.format("Unmatched positional arguments"));
    }

    return result.build();
}

From source file:org.calrissian.mango.collect.CloseableIterators.java

/**
 * Returns a {@code PeekingCloseableIterator} backed by the given closeable iterator.
 *
 * Calls to peek do not change the state of the iterator.  The subsequent call to next
 * after peeking will always return the same value.
 *//*from   w w  w.j  a v a2  s.co  m*/
public static <T> PeekingCloseableIterator<T> peekingIterator(final CloseableIterator<T> iterator) {
    final PeekingIterator<T> peeking = Iterators.peekingIterator(iterator);
    return new PeekingCloseableIterator<T>() {
        @Override
        public void closeQuietly() {
            iterator.closeQuietly();
        }

        @Override
        public void close() throws IOException {
            iterator.close();
        }

        @Override
        public T peek() {
            return peeking.peek();
        }

        @Override
        public T next() {
            return peeking.next();
        }

        @Override
        public void remove() {
            peeking.remove();
        }

        @Override
        public boolean hasNext() {
            return peeking.hasNext();
        }
    };
}

From source file:r.base.Calls.java

/**
 * Argument matching is done by a three-pass process:
 * <ol>/*from ww  w  . j  av a 2s.c  om*/
 * <li><strong>Exact matching on tags.</strong> For each named supplied argument the list of formal arguments
 *  is searched for an item whose name matches exactly. It is an error to have the same formal
 * argument match several actuals or vice versa.</li>
 *
 * <li><strong>Partial matching on tags.</strong> Each remaining named supplied argument is compared to the
 * remaining formal arguments using partial matching. If the name of the supplied argument
 * matches exactly with the first part of a formal argument then the two arguments are considered
 * to be matched. It is an error to have multiple partial matches.
 *  Notice that if f <- function(fumble, fooey) fbody, then f(f = 1, fo = 2) is illegal,
 * even though the 2nd actual argument only matches fooey. f(f = 1, fooey = 2) is legal
 * though since the second argument matches exactly and is removed from consideration for
 * partial matching. If the formal arguments contain ... then partial matching is only applied to
 * arguments that precede it.
 *
 * <li><strong>Positional matching.</strong> Any unmatched formal arguments are bound to unnamed supplied arguments,
 * in order. If there is a ... argument, it will take up the remaining arguments, tagged or not.
 * If any arguments remain unmatched an error is declared.
 *
 * @param actuals the actual arguments supplied to the list
 */
public static PairList matchArguments(PairList formals, PairList actuals) {

    PairList.Builder result = new PairList.Builder();

    List<PairList.Node> unmatchedActuals = Lists.newArrayList();
    for (PairList.Node argNode : actuals.nodes()) {
        unmatchedActuals.add(argNode);
    }

    List<PairList.Node> unmatchedFormals = Lists.newArrayList(formals.nodes());

    // do exact matching
    for (ListIterator<PairList.Node> formalIt = unmatchedFormals.listIterator(); formalIt.hasNext();) {
        PairList.Node formal = formalIt.next();
        if (formal.hasTag()) {
            Symbol name = (Symbol) formal.getTag();
            Collection<PairList.Node> matches = Collections2.filter(unmatchedActuals,
                    PairList.Predicates.matches(name));

            if (matches.size() == 1) {
                PairList.Node match = first(matches);
                result.add(name, match.getValue());
                formalIt.remove();
                unmatchedActuals.remove(match);

            } else if (matches.size() > 1) {
                throw new EvalException(
                        String.format("Multiple named values provided for argument '%s'", name.getPrintName()));
            }
        }
    }

    // do partial matching
    Collection<PairList.Node> remainingNamedFormals = filter(unmatchedFormals, PairList.Predicates.hasTag());
    for (Iterator<PairList.Node> actualIt = unmatchedActuals.iterator(); actualIt.hasNext();) {
        PairList.Node actual = actualIt.next();
        if (actual.hasTag()) {
            Collection<PairList.Node> matches = Collections2.filter(remainingNamedFormals,
                    PairList.Predicates.startsWith(actual.getTag()));

            if (matches.size() == 1) {
                PairList.Node match = first(matches);
                result.add(match.getTag(), actual.getValue());
                actualIt.remove();
                unmatchedFormals.remove(match);

            } else if (matches.size() > 1) {
                throw new EvalException(
                        String.format("Provided argument '%s' matches multiple named formal arguments: %s",
                                actual.getTag().getPrintName(), argumentTagList(matches)));
            }
        }
    }

    // match any unnamed args positionally

    Iterator<PairList.Node> formalIt = unmatchedFormals.iterator();
    PeekingIterator<PairList.Node> actualIt = Iterators.peekingIterator(unmatchedActuals.iterator());
    while (formalIt.hasNext()) {
        PairList.Node formal = formalIt.next();
        if (Symbols.ELLIPSES.equals(formal.getTag())) {
            PromisePairList.Builder promises = new PromisePairList.Builder();
            while (actualIt.hasNext()) {
                PairList.Node actual = actualIt.next();
                promises.add(actual.getRawTag(), actual.getValue());
            }
            result.add(formal.getTag(), promises.build());

        } else if (hasNextUnTagged(actualIt)) {
            result.add(formal.getTag(), nextUnTagged(actualIt).getValue());

        } else {
            result.add(formal.getTag(), Symbol.MISSING_ARG);
        }
    }
    if (actualIt.hasNext()) {
        throw new EvalException(String.format("Unmatched positional arguments"));
    }

    return result.build();
}

From source file:org.diqube.cache.ConstantTimeCache.java

private void cleanupCache(long now) {
    PeekingIterator<Triple<Long, K1, K2>> it = Iterators.peekingIterator(timeouts.iterator());
    while (it.hasNext() && it.peek().getLeft() < now) {
        Triple<Long, K1, K2> t = it.next();
        values.remove(new Pair<>(t.getMiddle(), t.getRight()));
        secondLevelKeys.computeIfPresent(t.getMiddle(), (k, v) -> {
            Set<K2> res = new ConcurrentSkipListSet<>(v);
            res.remove(t.getRight());/*from  ww w.  j a va 2  s  .  c  om*/
            if (res.isEmpty())
                return null;
            return res;
        });
        it.remove();
    }
}

From source file:org.apache.accumulo.gc.GarbageCollectionAlgorithm.java

protected void confirmDeletesFromReplication(Iterator<Entry<String, Status>> replicationNeededIterator,
        Iterator<Entry<String, String>> candidateMapIterator) {
    PeekingIterator<Entry<String, Status>> pendingReplication = Iterators
            .peekingIterator(replicationNeededIterator);
    PeekingIterator<Entry<String, String>> candidates = Iterators.peekingIterator(candidateMapIterator);
    while (pendingReplication.hasNext() && candidates.hasNext()) {
        Entry<String, Status> pendingReplica = pendingReplication.peek();
        Entry<String, String> candidate = candidates.peek();

        String filePendingReplication = pendingReplica.getKey();
        String fullPathCandidate = candidate.getValue();

        int comparison = filePendingReplication.compareTo(fullPathCandidate);
        if (comparison < 0) {
            pendingReplication.next();//  w  ww.j  av  a 2  s  .c o m
        } else if (comparison > 1) {
            candidates.next();
        } else {
            // We want to advance both, and try to delete the candidate if we can
            candidates.next();
            pendingReplication.next();

            // We cannot delete a file if it is still needed for replication
            if (!StatusUtil.isSafeForRemoval(pendingReplica.getValue())) {
                // If it must be replicated, we must remove it from the candidate set to prevent deletion
                candidates.remove();
            }
        }
    }
}