Example usage for java.util Queue poll

List of usage examples for java.util Queue poll

Introduction

In this page you can find the example usage for java.util Queue poll.

Prototype

E poll();

Source Link

Document

Retrieves and removes the head of this queue, or returns null if this queue is empty.

Usage

From source file:Main.java

/**
 * Creates a list by pulling off the head of a queue one item at a time and
 * adding it to an output list. The input queue is emptied by this method.
 * /*  w  w w  . j av  a2s.  c  o m*/
 * @param <T>
 * @param queue
 * @param outputList
 * @param reverse
 *            whether or not to reverse the resulting list
 * @return
 */
public static <T> List<T> createList(Queue<T> queue, List<T> outputList, boolean reverse) {
    if (outputList == null) {
        outputList = new ArrayList<T>(queue.size());
    }
    while (!queue.isEmpty()) {
        outputList.add(queue.poll());
    }
    if (reverse) {
        Collections.reverse(outputList);
    }
    return outputList;
}

From source file:fr.inria.atlanmod.emf.graphs.Connectedness.java

/**
 * Returns the {@link Set} of {@link EObject}s that can be reached by
 * navigating {@link EReference}s, starting from <code>initialEObject</code>
 * //w ww.j  a  v  a 2 s . co m
 * @param initialEObject
 *            The initial {@link EObject}
 * @return The {@link Set} of reachable {@link EObject}s
 */
private static Set<EObject> getReachableEObjects(EObject initialEObject) {
    Set<EObject> visited = new HashSet<>();
    Queue<EObject> next = new LinkedList<EObject>();
    next.add(initialEObject);

    while (!next.isEmpty()) {
        EObject activeEObject = next.poll();
        if (visited.add(activeEObject)) {
            next.addAll(activeEObject.eContents());
            next.addAll(activeEObject.eCrossReferences());
        }
    }

    return visited;
}

From source file:org.apache.mahout.clustering.lda.LDAPrintTopics.java

private static void maybeEnqueue(Queue<Pair<String, Double>> q, String word, double score,
        int numWordsToPrint) {
    if (q.size() >= numWordsToPrint && score > q.peek().getSecond()) {
        q.poll();
    }//from   w ww.ja  v  a  2  s .  com
    if (q.size() < numWordsToPrint) {
        q.add(new Pair<String, Double>(word, score));
    }
}

From source file:org.stem.db.compaction.CompactionManager.java

private static void markAllOriginalFFsAsBlank(Queue<FatFile> originalFFs, MountPoint mp) throws IOException {
    while (!originalFFs.isEmpty()) {
        FatFile ff = originalFFs.poll();
        ff.reallocate();/*from   w  w  w.  j  a  v a 2 s.c  o m*/
        StorageService.instance.submitFF(ff, mp); // TODO: inside we must re-count DataTracker
    }
}

From source file:com.rapleaf.hank.cli.AddRing.java

private static void addRing(ClientConfigurator configurator, String ringGroupName, int ringNumber,
        String hostsString) throws Exception {
    // create the ring
    Coordinator coord = configurator.getCoordinator();
    RingGroup ringGroup = coord.getRingGroupConfig(ringGroupName);
    Ring newRing = ringGroup.addRing(ringNumber);

    // add all the hosts to the ring
    String[] hosts = hostsString.split(",");
    List<Host> hostConfigs = new LinkedList<Host>();
    for (String host : hosts) {
        PartDaemonAddress address = PartDaemonAddress.parse(host);
        hostConfigs.add(newRing.addHost(address));
    }//from ww w .  ja v  a2 s .co  m

    // assign all the domains to the hosts
    DomainGroup domainGroup = ringGroup.getDomainGroup();
    DomainGroupVersion latestVersion = domainGroup.getLatestVersion();
    int verNum = latestVersion.getVersionNumber();
    for (DomainGroupVersionDomainVersion domainVersion : latestVersion.getDomainVersions()) {
        Domain domain = domainVersion.getDomain();

        Queue<HostDomain> q = new LinkedList<HostDomain>();
        for (Host hostConfig : hostConfigs) {
            q.add(hostConfig.addDomain(domainGroup.getDomainId(domain.getName())));
        }

        for (int i = 0; i < domain.getNumParts(); i++) {
            HostDomain hdc = q.poll();
            hdc.addPartition(i, verNum);
            q.add(hdc);
        }
    }
}

From source file:org.jbpm.services.task.jaxb.ComparePair.java

public static void compareOrig(Object origObj, Object newObj, Class objClass) {
    ComparePair compare = new ComparePair(origObj, newObj, objClass);
    Queue<ComparePair> compares = new LinkedList<ComparePair>();
    compares.add(compare);/*from   ww  w.  j av a2 s.  c  o  m*/
    while (!compares.isEmpty()) {
        compares.addAll(compares.poll().compare());
    }
}

From source file:playground.johannes.statistics.SampledGraphStatistics.java

public static SortedSet<Collection<SampledVertex>> getDisconnectedComponents(SampledGraph g) {
    TreeSet<Collection<SampledVertex>> clusters = new TreeSet<Collection<SampledVertex>>(new SizeComparator());
    CentralityGraphDecorator graphDecorator = new CentralityGraphDecorator(g);
    UnweightedDijkstra dijkstra = new UnweightedDijkstra((CentralityGraph) graphDecorator.getSparseGraph());
    Queue<CentralityVertex> vertices = new LinkedList<CentralityVertex>();
    for (SparseVertex v : graphDecorator.getSparseGraph().getVertices())
        vertices.add((CentralityVertex) v);

    CentralityVertex source;//from w w  w .j  av  a 2s  . co m
    while ((source = vertices.poll()) != null) {
        List<CentralityVertex> reached = dijkstra.run(source);
        reached.add(source);
        List<SampledVertex> reached2 = new LinkedList<SampledVertex>();
        for (CentralityVertex cv : reached)
            reached2.add((SampledVertex) graphDecorator.getVertex(cv));
        clusters.add(reached2);
        vertices.removeAll(reached);
    }

    return clusters;
}

From source file:bixo.operations.ProcessRobotsTask.java

/**
 * Clear out the queue by outputting all entries with <groupingKey>.
 * /*from   ww  w.  j ava 2s.  c  om*/
 * We do this to empty the queue when there's some kind of error.
 * 
 * @param urls Queue of URLs to empty out
 * @param groupingKey grouping key to use for all entries.
 * @param collector tuple output collector
 */
public static void emptyQueue(Queue<GroupedUrlDatum> urls, String groupingKey, TupleEntryCollector collector,
        FlowProcess process) {
    GroupedUrlDatum datum;
    while ((datum = urls.poll()) != null) {
        ScoredUrlDatum scoreUrl = new ScoredUrlDatum(datum.getUrl(), groupingKey, UrlStatus.UNFETCHED, 1.0);
        scoreUrl.setPayload(datum.getPayload());
        // TODO KKr - move synchronization up, to avoid lots of contention with other threads?
        synchronized (collector) {
            collector.add(BixoPlatform.clone(scoreUrl.getTuple(), process));
        }
    }
}

From source file:playground.johannes.statistics.GraphStatistics.java

public static SortedSet<Collection<Vertex>> getDisconnectedComponents(Graph g) {
    TreeSet<Collection<Vertex>> clusters = new TreeSet<Collection<Vertex>>(new SizeComparator());
    CentralityGraphDecorator graphDecorator = new CentralityGraphDecorator(g);
    UnweightedDijkstra dijkstra = new UnweightedDijkstra((CentralityGraph) graphDecorator.getSparseGraph());
    Queue<CentralityVertex> vertices = new LinkedList<CentralityVertex>();
    for (SparseVertex v : graphDecorator.getSparseGraph().getVertices())
        vertices.add((CentralityVertex) v);

    CentralityVertex source;// w  ww  . j a v a  2s  .c  o  m
    while ((source = vertices.poll()) != null) {
        List<CentralityVertex> reached = dijkstra.run(source);
        reached.add(source);
        List<Vertex> reached2 = new LinkedList<Vertex>();
        for (CentralityVertex cv : reached)
            reached2.add(graphDecorator.getVertex(cv));
        clusters.add(reached2);
        vertices.removeAll(reached);
    }

    return clusters;
}

From source file:com.mohawk.webcrawler.ScriptCompiler.java

/**
 *
 * @param tokens//from  www. jav  a 2 s .  c o m
 * @param parentScope
 */
private static void addScope(Queue<String> tokens, Queue<? super BaseToken> parentScope)
        throws CompilationException {

    while (!tokens.isEmpty()) {

        String token = tokens.poll();
        if ("end".equals(token) || "else".equals(token) || "elseif".equals(token)) {
            parentScope.add(new BaseEndScope(token));
            break;
        } else if ("if".equals(token)) {
            String expression = tokens.poll();

            If_Verb ifVerb = new If_Verb();
            ifVerb.setExpression(expression);

            parentScope.add(ifVerb);
            addScope(tokens, ifVerb.createScope());

            // check if elseif or else is defined
            LinkedList<BaseToken> ifScope = ifVerb.getScope();
            Object elseToken = ifScope.peekLast();

            if (elseToken instanceof BaseEndScope) {
                // remove elseif or else from if scope
                ifScope.pollLast();

                while (elseToken instanceof BaseEndScope) {

                    String elseStr = ((BaseEndScope) elseToken).getName();
                    if ("end".equals(elseStr))
                        break;
                    else if ("elseif".equals(elseStr)) {

                        String exp = tokens.poll();
                        ElseIf_Verb elseIfVerb = new ElseIf_Verb();
                        elseIfVerb.setExpression(exp);
                        ifVerb.addElseIf(elseIfVerb);

                        addScope(tokens, elseIfVerb.createScope());
                        elseToken = elseIfVerb.getScope().pollLast();
                    } else if ("else".equals(elseStr)) {

                        Else_Verb elseVerb = new Else_Verb();
                        ifVerb.setElse(elseVerb);

                        addScope(tokens, elseVerb.createScope());
                        elseToken = elseVerb.getScope().pollLast();
                    }
                }
            }
        } else if ("while".equals(token)) {

            String evaluation = tokens.poll();

            While_Verb whileVerb = new While_Verb();
            whileVerb.setExpression(evaluation);

            parentScope.add(whileVerb);
            addScope(tokens, whileVerb.createScope());
        } else if (LangCore.isVerb(token)) { // verb
            try {
                parentScope.add(LangCore.createVerbToken((String) token));
            } catch (Exception e) {
                e.printStackTrace();
                throw new CompilationException(e.getLocalizedMessage());
            }
        } else if (LangCore.isLiteral(token)) { // literal
            try {
                parentScope.add(new BaseLiteral(LangCore.createLiteralObject(token)));
            } catch (LanguageException e) {
                throw new CompilationException(e.getLocalizedMessage());
            }
        } else if (LangCore.isOperator(token)) { // operator
            try {
                parentScope.add(LangCore.createOperatorToken(token));
            } catch (LanguageException e) {
                throw new CompilationException(e.getLocalizedMessage());
            }
        } else // default to variable
            parentScope.add(new BaseVariable(token));
    }
}