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

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

Introduction

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

Prototype

public static <T> Iterator<T> consumingIterator(final Iterator<T> iterator) 

Source Link

Document

Returns a view of the supplied iterator that removes each element from the supplied iterator as it is returned.

Usage

From source file:com.threerings.nio.SelectorIterable.java

public Iterator<SelectionKey> iterator() {
    return Iterators.consumingIterator(select().iterator());
}

From source file:org.apache.fluo.recipes.core.export.ExportObserverImpl.java

@Override
public void process(TransactionBase tx, Bytes row, Column col) throws Exception {
    ExportBucket bucket = new ExportBucket(tx, row);

    Bytes continueRow = bucket.getContinueRow();

    Iterator<ExportEntry> input = bucket.getExportIterator(continueRow);
    MemLimitIterator memLimitIter = new MemLimitIterator(input, memLimit, 8 + queueId.length());

    Iterator<SequencedExport<K, V>> exportIterator = Iterators.transform(memLimitIter,
            ee -> new SequencedExport<>(serializer.deserialize(ee.key, keyType),
                    serializer.deserialize(ee.value, valType), ee.seq));

    exportIterator = Iterators.consumingIterator(exportIterator);

    exporter.export(exportIterator);/*from  www  .  j  av a  2s . c  o  m*/

    if (input.hasNext() || continueRow != null) {
        // not everything was processed so notify self OR new data may have been inserted above the
        // continue row
        bucket.notifyExportObserver();
    }

    if (input.hasNext()) {
        if (!memLimitIter.hasNext()) {
            // stopped because of mem limit... set continue key
            bucket.setContinueRow(input.next());
            continueRow = null;
        }
    }

    if (continueRow != null) {
        bucket.clearContinueRow();
    }
}

From source file:org.sosy_lab.cpachecker.core.algorithm.testgen.pathanalysis.BasicPathSelector.java

@Override
public PredicatePathAnalysisResult findNewFeasiblePathUsingPredicates(final ARGPath pExecutedPath,
        ReachedSet reachedStates) throws CPAException, InterruptedException {
    /*//from   w ww  .  java 2 s. com
     * create copy of the given path, because it will be modified with this algorithm.
     * represents the current new valid path.
     */
    MutableARGPath newARGPath = pExecutedPath.mutableCopy();
    PathInfo pathInfo = new PathInfo(newARGPath.size());
    //    ARGPath newARGPathView = Collections.unmodifiableList(newARGPath);
    /*
     * only by edge representation of the new path.
     */
    List<CFAEdge> newPath = Lists.newArrayList(newARGPath.asEdgesList());
    /*
     * element removed from the path in the previous iteration
     */
    Pair<ARGState, CFAEdge> lastElement = null;
    Pair<ARGState, CFAEdge> currentElement;
    /*
     * this is a variation of the solve_path_constraint(..., path_constraint, stack) function of DART.
     *
     */
    /*
     * create a descending consuming iterator to iterate through the path from last to first, while consuming elements.
     * Elements are consumed because the new path is a subpath of the original.
     */
    Iterator<Pair<ARGState, CFAEdge>> descendingPathElements = Iterators
            .consumingIterator(newARGPath.descendingIterator());
    pathValidator.handleNewCheck(pExecutedPath);
    while (descendingPathElements.hasNext()) {
        pathInfo.increaseNodeCount();
        currentElement = descendingPathElements.next();
        CFAEdge edge = currentElement.getSecond();
        //handle last node of the given path. (should never be a decision node, so we skip it)
        if (edge == null) {
            lastElement = currentElement;
            continue;
        }
        pathValidator.handleNext(pathInfo, edge);

        CFANode node = edge.getPredecessor();
        //num of leaving edges does not include a summary edge, so the check is valid.
        if (node.getNumLeavingEdges() != 2) {
            pathValidator.handleSinglePathElement(currentElement);
            lastElement = currentElement;
            continue;
        }
        /*
         * current node is a branching / deciding node. select the edge that isn't represented
         * with the current path.
         */
        pathInfo.increaseBranchCount();
        CFANode decidingNode = node;
        CFAEdge wrongEdge = edge;

        /*
         * (DART: negate the path constraint)
         */
        Optional<CFAEdge> otherEdge = CFAUtils2.getAlternativeLeavingEdge(decidingNode, wrongEdge);
        //no edge found should not happen because we filtered nodes such that only those with more than one leaving edge encounter this.; If it does make it visible.
        assert otherEdge.isPresent();

        /*
         * (DART: the j = pathLength-1 case)
         */
        if (pathValidator.isVisitedBranching(newARGPath, currentElement, node, otherEdge.get())) {
            logger.log(Level.FINER,
                    "Branch on path was handled in an earlier iteration -> skipping branching.");
            lastElement = currentElement;
            pathValidator.handleVisitedBranching(newARGPath, currentElement);
            continue;
        }

        if (lastElement == null) {
            /*
             * if the last element is not set, we encountered a branching node where both paths are infeasible
             * for the current value mapping or both successors were handled already with a previous iteration.
             * (the successors are in reached and the CPAAlgorithms stops if all successors were reached before).
             */
            logger.log(Level.FINER,
                    "encountered an executed path that continues into an already reached region. -> Skipping");
            lastElement = currentElement;
            continue;
        }

        /*
         * identified a decision node and selected a new edge.
         * extract the edge-list of the path and add the new edge to it.
         * Don't modify the ARGPath yet, because it is possible that the current decision is infeasible
         */
        logger.logf(Level.FINER, "identified new path candidate (visited branchings: %d, nodes: %d)",
                pathInfo.getBranchCount(), pathInfo.getNodeCount());
        newPath = Lists.newArrayList(newARGPath.asEdgesList());
        newPath.add(otherEdge.get());
        /*
         * evaluate path candidate symbolically using SMT-solving
         */
        stats.beforePathCheck();
        CounterexampleTraceInfo traceInfo = pathValidator.validatePathCandidate(currentElement, newPath);
        stats.afterPathCheck();
        /*
         * check if path is feasible. If it's not continue to identify another decision node
         * If path is feasible, add the ARGState belonging to the decision node and the new edge to the ARGPath. Exit and Return result.
         */
        if (!traceInfo.isSpurious()) {
            newARGPath.add(Pair.of(currentElement.getFirst(), otherEdge.get()));
            logger.logf(Level.FINEST, "selected new path %s", newPath.toString());
            PredicatePathAnalysisResult result = new PredicatePathAnalysisResult(traceInfo,
                    currentElement.getFirst(), lastElement.getFirst(), newARGPath.immutableCopy());
            pathValidator.handleValidPath(result);
            return result;
        } else {
            lastElement = currentElement;
            logger.logf(Level.FINER, "path candidate is infeasible");
            continue;
        }

    }
    //all possible paths explored. (DART: the j = -1 case)
    logger.logf(Level.FINER, "No possible path left to explore");
    return PredicatePathAnalysisResult.INVALID;
}

From source file:org.sosy_lab.cpachecker.core.algorithm.testgen.pathanalysis.LocationAndValueStateTrackingPathAnalysisStrategy.java

@Override
public PredicatePathAnalysisResult findNewFeasiblePathUsingPredicates(final ARGPath pExecutedPath,
        final ReachedSet reached) throws CPAException, InterruptedException {
    /*//from w ww .j a  va 2  s  .c  om
     * create copy of the given path, because it will be modified with this algorithm.
     * represents the current new valid path.
     */
    MutableARGPath newARGPath = pExecutedPath.mutableCopy();
    /*
     * only by edge representation of the new path.
     */
    List<CFAEdge> newPath = Lists.newArrayList(newARGPath.asEdgesList());
    /*
     * element removed from the path in the previous iteration
     */
    Pair<ARGState, CFAEdge> lastElement = null;
    Pair<ARGState, CFAEdge> currentElement;
    /*
     * create a descending consuming iterator to iterate through the path from last to first, while consuming elements.
     * Elements are consumed because the new path is a subpath of the original.
     */
    long branchCounter = 0;
    long nodeCounter = 0;
    Iterator<Pair<ARGState, CFAEdge>> branchingEdges = Iterators
            .consumingIterator(newARGPath.descendingIterator());
    while (branchingEdges.hasNext()) {
        nodeCounter++;
        currentElement = branchingEdges.next();
        CFAEdge edge = currentElement.getSecond();
        if (edge == null) {
            lastElement = currentElement;
            continue;
        }
        CFANode node = edge.getPredecessor();
        //num of leaving edges does not include a summary edge, so the check is valid.
        if (node.getNumLeavingEdges() != 2) {
            lastElement = currentElement;
            continue;
        }
        //current node is a branching / deciding node. select the edge that isn't represented with the current path.
        CFANode decidingNode = node;

        // WARNING: some hack don't know if any good or enough
        // ----->
        final AbstractState currentElementTmp = currentElement.getFirst();
        if (from(handledDecisions).anyMatch(new Predicate<AbstractState>() {

            @Override
            public boolean apply(AbstractState pInput) {
                return AbstractStates.extractStateByType(currentElementTmp, ValueAnalysisState.class)
                        .equals(AbstractStates.extractStateByType(pInput, ValueAnalysisState.class))
                        && AbstractStates.extractStateByType(currentElementTmp, LocationState.class)
                                .getLocationNode().getNodeNumber() == AbstractStates
                                        .extractStateByType(pInput, LocationState.class).getLocationNode()
                                        .getNodeNumber();
            }
        }))
        // < ------
        {

            logger.log(Level.FINER,
                    "Branch on path was handled in an earlier iteration -> skipping branching.");
            lastElement = currentElement;
            continue;
        }
        //      cpa.getTransferRelation().
        if (lastElement == null) {
            //if the last element is not set, we encountered a branching node where both paths are infeasible for the current value mapping.
            logger.log(Level.FINER, "encountered an executed path that might be spurious.");
            lastElement = currentElement;
            continue;
        }
        CFAEdge wrongEdge = edge;
        CFAEdge otherEdge = null;
        for (CFAEdge cfaEdge : CFAUtils.leavingEdges(decidingNode)) {
            if (cfaEdge.equals(wrongEdge)) {
                continue;
            } else {
                otherEdge = cfaEdge;
                break;
            }
        }
        logger.logf(Level.FINER, "identified valid branching (skipped branching count: %d, nodes: %d)",
                branchCounter++, nodeCounter);
        //no edge found should not happen; If it does make it visible.
        assert otherEdge != null;
        /*
         * identified a decision node and selected a new edge.
         * extract the edge-list of the path and add the new edge to it.
         * Don't modify the ARGPath yet, because it is possible that the current decision is infeasible
         */
        newPath = Lists.newArrayList(newARGPath.asEdgesList());
        newPath.add(otherEdge);
        /*
         * check if path is feasible. If it's not continue to identify another decision node
         * If path is feasible, add the ARGState belonging to the decision node and the new edge to the ARGPath. Exit and Return result.
         */
        stats.beforePathCheck();
        CounterexampleTraceInfo traceInfo = pathChecker.checkPath(newPath);
        stats.afterPathCheck();

        if (!traceInfo.isSpurious()) {
            newARGPath.add(Pair.of(currentElement.getFirst(), otherEdge));
            logger.logf(Level.FINEST, "selected new path %s", newPath.toString());
            handledDecisions.add(currentElement.getFirst());
            return new PredicatePathAnalysisResult(traceInfo, currentElement.getFirst(), lastElement.getFirst(),
                    newARGPath.immutableCopy());
        } else {
            lastElement = currentElement;
            continue;
        }

    }
    return PredicatePathAnalysisResult.INVALID;
}

From source file:org.opendaylight.openflowplugin.impl.rpc.RpcManagerImpl.java

@Override
public void close() {
    for (final Iterator<RpcContext> iterator = Iterators
            .consumingIterator(contexts.values().iterator()); iterator.hasNext();) {
        iterator.next().close();//from  w  ww .  j ava 2  s .co  m
    }
}

From source file:org.apache.fluo.recipes.export.ExportObserver.java

@Override
public void process(TransactionBase tx, Bytes row, Column column) throws Exception {
    ExportBucket bucket = new ExportBucket(tx, row);

    Bytes continueRow = bucket.getContinueRow();

    Iterator<ExportEntry> input = bucket.getExportIterator(continueRow);
    MemLimitIterator memLimitIter = new MemLimitIterator(input, memLimit, 8 + queueId.length());

    Iterator<SequencedExport<K, V>> exportIterator = Iterators.transform(memLimitIter,
            ee -> new SequencedExport<>(serializer.deserialize(ee.key, keyType),
                    serializer.deserialize(ee.value, valType), ee.seq));

    exportIterator = Iterators.consumingIterator(exportIterator);

    exporter.processExports(exportIterator);

    if (input.hasNext()) {
        // not everything was processed so notify self
        bucket.notifyExportObserver();/*  www .  j  ava2 s . c om*/

        if (!memLimitIter.hasNext()) {
            // stopped because of mem limit... set continue key
            bucket.setContinueRow(input.next());
            continueRow = null;
        }
    }

    if (continueRow != null) {
        bucket.clearContinueRow();
    }
}

From source file:org.sosy_lab.cpachecker.core.algorithm.testgen.pathanalysis.CUTEBasicPathSelector.java

@Override
public PredicatePathAnalysisResult findNewFeasiblePathUsingPredicates(final ARGPath pExecutedPath,
        ReachedSet reachedStates) throws CPAException, InterruptedException {
    /*//from   w  w  w  .ja va 2s .c o  m
     * create copy of the given path, because it will be modified with this algorithm.
     * represents the current new valid path.
     */
    MutableARGPath newARGPath = pExecutedPath.mutableCopy();
    PathInfo pathInfo = new PathInfo(newARGPath.size());
    //    ARGPath newARGPathView = Collections.unmodifiableList(newARGPath);
    /*
     * only by edge representation of the new path.
     */
    List<CFAEdge> newPath = Lists.newArrayList(newARGPath.asEdgesList());
    /*
     * element removed from the path in the previous iteration
     */
    Pair<ARGState, CFAEdge> lastElement = null;
    Pair<ARGState, CFAEdge> currentElement;
    /*
     * this is a variation of the solve_path_constraint(..., path_constraint, stack) function of DART.
     *
     */
    /*
     * create a descending consuming iterator to iterate through the path from last to first, while consuming elements.
     * Elements are consumed because the new path is a subpath of the original.
     */
    Iterator<Pair<ARGState, CFAEdge>> descendingPathElements = Iterators
            .consumingIterator(newARGPath.descendingIterator());

    while (descendingPathElements.hasNext()) {
        pathInfo.increaseNodeCount();
        currentElement = descendingPathElements.next();
        CFAEdge edge = currentElement.getSecond();
        Pair<CFAEdge, Boolean> oldElement = null;
        //handle last node of the given path. (should never be a decision node, so we skip it)
        if (edge == null) {
            lastElement = currentElement;
            continue;
        }
        oldElement = handleNextNode(pathInfo.getCurrentPathSize(), edge, oldElement);
        CFANode node = edge.getPredecessor();
        //num of leaving edges does not include a summary edge, so the check is valid.
        if (node.getNumLeavingEdges() != 2) {
            lastElement = currentElement;
            continue;
        }
        /*
         * current node is a branching / deciding node. select the edge that isn't represented
         * with the current path.
         */
        pathInfo.increaseBranchCount();
        CFANode decidingNode = node;
        CFAEdge wrongEdge = edge;

        /*
         * (DART: negate the path constraint)
         */
        Optional<CFAEdge> otherEdge = CFAUtils2.getAlternativeLeavingEdge(decidingNode, wrongEdge);
        //      if(branchingHistory.isMatch(otherEdge, oldEdge))
        //no edge found should not happen because we filtered nodes such that only those with more than one leaving edge encounter this.; If it does make it visible.
        assert otherEdge.isPresent();
        logger.logf(Level.FINEST, "StackState: %d %d (%d)", pathInfo.getCurrentPathSize(),
                branchingHistory.getCurrentDepths(), branchingHistory.getPathDepths());
        /*
         * (DART: the j = -1 case)
         */
        //      if(pathValidator.isVisitedBranching(newARGPath, currentElement, node, otherEdge))
        if (isVisited(currentElement, oldElement, otherEdge.get())) {
            logger.log(Level.FINER,
                    "Branch on path was handled in an earlier iteration -> skipping branching.");
            lastElement = currentElement;
            continue;
        }

        if (lastElement == null) {
            /*
             * if the last element is not set, we encountered a branching node where both paths are infeasible
             * for the current value mapping or both successors were handled already with a previous iteration.
             * (the successors are in reached and the CPAAlgorithms stops if all successors were reached before).
             */
            logger.log(Level.FINER,
                    "encountered an executed path that continues into an already reached region. -> Skipping");
            lastElement = currentElement;
            continue;
        }

        /*
         * identified a decision node and selected a new edge.
         * extract the edge-list of the path and add the new edge to it.
         * Don't modify the ARGPath yet, because it is possible that the current decision is infeasible
         */
        logger.logf(Level.FINER, "identified new path candidate (visited branchings: %d, nodes: %d)",
                pathInfo.getBranchCount(), pathInfo.getNodeCount());
        newPath = Lists.newArrayList(newARGPath.asEdgesList());
        newPath.add(otherEdge.get());
        /*
         * evaluate path candidate symbolically using SMT-solving
         */
        stats.beforePathCheck();
        CounterexampleTraceInfo traceInfo = pathChecker.checkPath(newPath);
        stats.afterPathCheck();
        /*
         * check if path is feasible. If it's not continue to identify another decision node
         * If path is feasible, add the ARGState belonging to the decision node and the new edge to the ARGPath. Exit and Return result.
         */
        if (!traceInfo.isSpurious()) {
            newARGPath.add(Pair.of(currentElement.getFirst(), otherEdge.get()));
            logger.logf(Level.FINEST, "selected new path %s", newPath.toString());
            //        pathValidator.handleValidPath(newARGPath, traceInfo);
            branchingHistory.resetTo(newARGPath);
            return new PredicatePathAnalysisResult(traceInfo, currentElement.getFirst(), lastElement.getFirst(),
                    newARGPath.immutableCopy());
        } else {
            lastElement = currentElement;
            logger.logf(Level.FINER, "path candidate is infeasible");
            //        pathValidator.handleSpuriousPath(newPath);
            continue;
        }

    }
    //all possible paths explored. (DART: the j = -1 case)
    logger.logf(Level.FINER, "No possible path left to explore");
    return PredicatePathAnalysisResult.INVALID;
}

From source file:org.apache.james.queue.api.ManageableMailQueueContract.java

@Test
default void browseShouldNotFailWhenConcurrentDequeue() throws Exception {
    enQueue(defaultMail().name("name1").build());
    enQueue(defaultMail().name("name2").build());
    enQueue(defaultMail().name("name3").build());

    ManageableMailQueue.MailQueueIterator items = getManageableMailQueue().browse();

    getManageableMailQueue().deQueue();/*from  www. j ava2s.c  o m*/

    assertThatCode(() -> Iterators.consumingIterator(items)).doesNotThrowAnyException();
}

From source file:org.openengsb.core.common.internal.VirtualConnectorManager.java

private Iterator<Registration> getFactoriesForVirtualConnectorForRemoval(
        final VirtualConnectorProvider provider) {
    Iterator<Registration> consumingIterator = Iterators.consumingIterator(registeredFactories.iterator());
    return Iterators.filter(consumingIterator, new Predicate<Registration>() {
        @Override/*from ww  w . j  a v a  2s  .c  o  m*/
        public boolean apply(Registration input) {
            return ObjectUtils.equals(input.virtualConnector, provider);
        }
    });
}

From source file:org.apache.aurora.scheduler.async.preemptor.PendingTaskProcessor.java

@Override
public void run() {
    metrics.recordTaskProcessorRun();/*from   w ww .j  ava  2s .c  om*/
    storage.read(new Storage.Work.Quiet<Void>() {
        @Override
        public Void apply(StoreProvider store) {
            Multimap<String, PreemptionVictim> slavesToActiveTasks = clusterState.getSlavesToActiveTasks();

            if (slavesToActiveTasks.isEmpty()) {
                // No preemption victims to consider.
                return null;
            }

            // Group the offers by slave id so they can be paired with active tasks from the same slave.
            Map<String, HostOffer> slavesToOffers = Maps.uniqueIndex(offerManager.getOffers(),
                    OFFER_TO_SLAVE_ID);

            Set<String> allSlaves = Sets
                    .newHashSet(Iterables.concat(slavesToOffers.keySet(), slavesToActiveTasks.keySet()));

            // The algorithm below attempts to find a reservation for every task group by matching
            // it against all available slaves until a preemption slot is found. Groups are evaluated
            // in a round-robin fashion to ensure fairness (e.g.: G1, G2, G3, G1, G2).
            // A slave is removed from further matching once a reservation is made. Similarly, all
            // identical task group instances are removed from further iteration if none of the
            // available slaves could yield a preemption proposal. A consuming iterator is used for
            // task groups to ensure iteration order is preserved after a task group is removed.
            LoadingCache<IJobKey, AttributeAggregate> jobStates = attributeCache(store);
            List<TaskGroupKey> pendingGroups = fetchIdlePendingGroups(store);
            Iterator<TaskGroupKey> groups = Iterators.consumingIterator(pendingGroups.iterator());
            while (!pendingGroups.isEmpty()) {
                boolean matched = false;
                TaskGroupKey group = groups.next();
                ITaskConfig task = group.getTask();

                metrics.recordPreemptionAttemptFor(task);
                Iterator<String> slaveIterator = allSlaves.iterator();
                while (slaveIterator.hasNext()) {
                    String slaveId = slaveIterator.next();
                    Optional<ImmutableSet<PreemptionVictim>> candidates = preemptionVictimFilter
                            .filterPreemptionVictims(task, slavesToActiveTasks.get(slaveId),
                                    jobStates.getUnchecked(task.getJob()),
                                    Optional.fromNullable(slavesToOffers.get(slaveId)), store);

                    metrics.recordSlotSearchResult(candidates, task);
                    if (candidates.isPresent()) {
                        // Slot found -> remove slave to avoid multiple task reservations.
                        slaveIterator.remove();
                        slotCache.put(new PreemptionProposal(candidates.get(), slaveId), group);
                        matched = true;
                        break;
                    }
                }
                if (!matched) {
                    // No slot found for the group -> remove group and reset group iterator.
                    pendingGroups.removeAll(ImmutableSet.of(group));
                    groups = Iterators.consumingIterator(pendingGroups.iterator());
                }
            }
            return null;
        }
    });
}