Example usage for java.util LinkedList pollFirst

List of usage examples for java.util LinkedList pollFirst

Introduction

In this page you can find the example usage for java.util LinkedList pollFirst.

Prototype

public E pollFirst() 

Source Link

Document

Retrieves and removes the first element of this list, or returns null if this list is empty.

Usage

From source file:eu.stratosphere.nephele.multicast.MulticastManager.java

/**
 * This method creates a tree with an arbitrary fan out (two means binary tree).
 * If topology information or penalties are available, it considers that.
 * If fanout is set to 1, it creates a sequential tree.
 * if fanout is set to Integer.MAXVALUE, it creates a unicast tree.
 * //from w w w  .j a v  a  2  s  .c  o  m
 * @param nodes
 * @param fanout
 * @return
 */
private MulticastForwardingTable createDefaultTree(LinkedList<TreeNode> nodes, int fanout) {

    // Store nodes that already have a parent, but no children
    LinkedList<TreeNode> connectedNodes = new LinkedList<TreeNode>();

    final TreeNode rootnode = nodes.pollFirst();
    TreeNode actualnode = rootnode;

    while (nodes.size() > 0) { // We still have unconnected nodes...

        for (int i = 0; i < fanout; i++) {

            if (nodes.size() > 0) {
                // pick the closest one and attach to actualnode
                TreeNode child = pollClosestNode(actualnode, nodes);
                actualnode.addChild(child);

                // The child is now connected and can be used as forwarder in the next iteration..
                connectedNodes.add(child);
            } else {
                break;
            }
        }

        // OK.. take the next node to attach children to it..
        // TODO: Optimization? "pollBest()" ?
        actualnode = connectedNodes.pollFirst();

    }
    LOG.info("created multicast tree with following topology:\n" + rootnode.printTree());

    return rootnode.createForwardingTable();

}

From source file:com.moorestudio.seniorimageprocessing.SeniorSorter.java

public void sortImages() {
    LinkedList<Map.Entry<String, Long>> timestampList = new LinkedList<>(timestampData.entrySet());
    sort(timestampList, (x, y) -> x.getValue() > y.getValue() ? -1 : x.getValue().equals(y.getValue()) ? 0 : 1);
    // Sort in reverse so that the most recent timestamps are first.e so that the most recent timestamps are first.

    LinkedList<Map.Entry<File, Long>> imageDataList = new LinkedList<>(imageData.entrySet());
    sort(imageDataList, (x, y) -> x.getValue() > y.getValue() ? -1 : x.getValue().equals(y.getValue()) ? 0 : 1); // Sort in reverse so that the most recent timestamps are first.

    // For the gui update
    int idCount = imageDataList.size();

    //Take the first image and the first timestamp scan taken, which is last in the list, 
    //and sync the camera time to the timestamp time.Both are throwaways.
    if (!timestampList.isEmpty() && !imageDataList.isEmpty() && parent.syncTime) {
        Map.Entry<File, Long> iData = imageDataList.pollLast();
        Map.Entry<String, Long> tsData = timestampList.pollLast();

        //Make the offset
        cameraTimeOffset = tsData.getValue() - iData.getValue();
    }//from  w w w.  java 2 s.co  m

    //add the file to the top timestamp student until it is no longer more than it
    while (!timestampList.isEmpty() && !imageDataList.isEmpty()) {
        Map.Entry<File, Long> iData = imageDataList.peekFirst();
        Map.Entry<String, Long> tsData = timestampList.pollFirst();
        ArrayList<File> studentImages = new ArrayList<>();
        while (!imageDataList.isEmpty() && iData.getValue() + cameraTimeOffset > tsData.getValue()) {
            iData = imageDataList.pollFirst();
            studentImages.add(iData.getKey());
            iData = imageDataList.peekFirst();
            //update the GUI
            parent.addProgress((.125 / parent.numThreads) / idCount);
        }
        if (!studentImages.isEmpty()) {
            parent.addImagesToStudent(tsData.getKey(), studentImages);
        }
    }

    //add the unsorted images to the parent's unsorted queue
    for (Map.Entry<File, Long> entry : imageDataList) {
        parent.unsortedFiles.add(entry.getKey());
        //update the GUI
        parent.addProgress((.125 / parent.numThreads) / idCount);
    }
}

From source file:org.openconcerto.sql.model.SQLTable.java

static private void fireTableModified(DispatchingState newTuple) {
    final LinkedList<DispatchingState> linkedList = events.get();
    // add new event
    linkedList.addLast(newTuple);/*from ww w .j  a  v  a  2 s.co m*/
    // process all pending events
    DispatchingState currentTuple;
    while ((currentTuple = linkedList.peekFirst()) != null) {
        final Iterator<SQLTableModifiedListener> iter = currentTuple.get0();
        final SQLTableEvent currentEvt = currentTuple.get1();
        while (iter.hasNext()) {
            final SQLTableModifiedListener l = iter.next();
            l.tableModified(currentEvt);
        }
        // not removeFirst() since the item might have been already removed
        linkedList.pollFirst();
    }
}

From source file:eu.stratosphere.nephele.multicast.MulticastManager.java

/**
 * Returns a list of (physical) Nodes (=hosts) within the multicast tree. Each node contains the local ChannelIDs,
 * records//from   ww  w  .  j a  v a2s.  co m
 * must be forwarded to. The first node in the List is the only multicast sender.
 * 
 * @param sourceChannelID
 * @return
 */
private LinkedList<TreeNode> extractTreeNodes(final InstanceConnectionInfo source, final JobID jobID,
        final ChannelID sourceChannelID, final boolean randomize) {

    final ExecutionGraph eg = this.scheduler.getExecutionGraphByID(jobID);

    final ExecutionEdge outputChannel = eg.getEdgeByID(sourceChannelID);

    final ExecutionGate broadcastGate = outputChannel.getOutputGate();

    final LinkedList<ExecutionEdge> outputChannels = new LinkedList<ExecutionEdge>();

    // Get all broadcast output channels
    final int numberOfOutputChannels = broadcastGate.getNumberOfEdges();
    for (int i = 0; i < numberOfOutputChannels; ++i) {
        final ExecutionEdge c = broadcastGate.getEdge(i);

        if (c.isBroadcast()) {
            outputChannels.add(c);
        }
    }

    final LinkedList<TreeNode> treeNodes = new LinkedList<TreeNode>();

    LinkedList<ChannelID> actualLocalTargets = new LinkedList<ChannelID>();

    int firstConnectionID = 0;
    // search for local targets for the tree node
    for (Iterator<ExecutionEdge> iter = outputChannels.iterator(); iter.hasNext();) {

        final ExecutionEdge actualOutputChannel = iter.next();

        // the connection ID should not be needed for the root node (as it is not set as remote receiver)
        // but in order to maintain consistency, it also gets the connectionID of the first channel pointing to it
        firstConnectionID = actualOutputChannel.getConnectionID();

        final ExecutionVertex targetVertex = actualOutputChannel.getInputGate().getVertex();

        // is the target vertex running on the same instance?
        if (targetVertex.getAllocatedResource().getInstance().getInstanceConnectionInfo().equals(source)) {

            actualLocalTargets.add(actualOutputChannel.getInputChannelID());
            iter.remove();
        }

    }

    // create sender node (root) with source instance
    TreeNode actualNode = new TreeNode(
            eg.getVertexByChannelID(sourceChannelID).getAllocatedResource().getInstance(), source,
            firstConnectionID, actualLocalTargets);

    treeNodes.add(actualNode);

    // now we have the root-node.. lets extract all other nodes

    LinkedList<TreeNode> receiverNodes = new LinkedList<TreeNode>();

    while (outputChannels.size() > 0) {

        final ExecutionEdge firstChannel = outputChannels.pollFirst();

        // each receiver nodes' endpoint is associated with the connection ID
        // of the first channel pointing to this node.
        final int connectionID = firstChannel.getConnectionID();

        final ExecutionVertex firstTarget = firstChannel.getInputGate().getVertex();

        final InstanceConnectionInfo actualInstance = firstTarget.getAllocatedResource().getInstance()
                .getInstanceConnectionInfo();

        actualLocalTargets = new LinkedList<ChannelID>();

        // add first local target
        actualLocalTargets.add(firstChannel.getInputChannelID());

        // now we iterate through the remaining channels to find other local targets...
        for (Iterator<ExecutionEdge> iter = outputChannels.iterator(); iter.hasNext();) {

            final ExecutionEdge actualOutputChannel = iter.next();

            final ExecutionVertex actualTarget = actualOutputChannel.getInputGate().getVertex();

            // is the target vertex running on the same instance?
            if (actualTarget.getAllocatedResource().getInstance().getInstanceConnectionInfo()
                    .equals(actualInstance)) {
                actualLocalTargets.add(actualOutputChannel.getInputChannelID());

                iter.remove();

            }

        } // end for

        // create tree node for current instance
        actualNode = new TreeNode(firstTarget.getAllocatedResource().getInstance(), actualInstance,
                connectionID, actualLocalTargets);

        receiverNodes.add(actualNode);

    } // end while

    // Do we want to shuffle the receiver nodes?
    // Only randomize the receivers, as the sender (the first one) has to stay the same
    if (randomize) {
        Collections.shuffle(receiverNodes);
    } else {
        // Sort Tree Nodes according to host name..
        Collections.sort(receiverNodes);
    }

    treeNodes.addAll(receiverNodes);

    return treeNodes;

}

From source file:org.ocsoft.rosetto.models.base.elements.values.ListValue.java

/**
 * ????????./*from   ww w .  j  a v  a  2  s.c  o  m*/
 * ????????????????.
 * ???????????.
 * ????ActionCall??.
 * @param func ??
 * @return ????
 */
public Map<String, RosettoValue> bind(RosettoFunction func, Scope currentScope) {
    if (func == null)
        throw new IllegalArgumentException("???null??");

    //??
    Map<String, RosettoValue> result = new HashMap<String, RosettoValue>();
    //????
    LinkedList<String> funcArgs = new LinkedList<String>();

    //???????????
    int requiredArgsCount = 0;

    //????????
    for (String s : func.getArguments()) {
        int eqIndex = s.indexOf("=");
        if (eqIndex == -1) {
            //?????????
            funcArgs.add(s);
            //????????
            requiredArgsCount++;
        } else {
            //????
            String key = s.substring(0, eqIndex);
            RosettoValue value = Values.create(s.substring(eqIndex + 1, s.length()));
            //?
            funcArgs.add(key);
            //???????
            result.put(key, value);
        }
    }

    //??????

    //??
    for (Entry<String, RosettoValue> e : getMap().entrySet()) {
        //?Entry??funcArgs?????funcArg
        boolean removed = funcArgs.remove(e.getKey());
        //????
        if (removed)
            requiredArgsCount--;
        //??
        result.put(e.getKey(), e.getValue());
    }

    //??
    String mutableArg = searchMutableArg(funcArgs);
    if (mutableArg != null) {
        //????????

        //????????
        //??????????-1
        if (requiredArgsCount - 1 > getList().size()) {
            throw new IllegalArgumentException("???????: "
                    + getList().toString() + "|" + func.getArguments());
        }

        //????????????????
        //??
        List<RosettoValue> margs = new LinkedList<RosettoValue>();
        for (int i = 0; i < getList().size(); i++) {
            RosettoValue v = getList().get(i);
            if (!funcArgs.isEmpty()) {
                //???????????pop?????????
                String farg = funcArgs.pollFirst();
                //??????????
                if (farg.equals(mutableArg)) {
                    //??????????????
                    if (!funcArgs.isEmpty()) {
                        throw new IllegalArgumentException("mutable args must be last element");
                    }
                    //??
                    margs.add(v);
                }
                //????????
                result.put(farg, v);
            } else {
                //??
                margs.add(v);
            }
        }

        //?????
        if (margs.size() > 0) {
            //?????
            String margName = mutableArg.substring(1);
            //listvalue???
            result.put(margName, new ListValue(margs));
        }

    } else {
        //???????
        if (requiredArgsCount > getList().size()) {
            throw new IllegalArgumentException("???????: "
                    + getList().toString() + "|" + func.getArguments());
        } else if (funcArgs.size() < getList().size()) {
            throw new IllegalArgumentException(
                    "?????: " + getList().toString() + "|" + func.getArguments());
        }
        for (RosettoValue value : getList()) {
            //?????????
            if (value.getType() == ValueType.ACTION_CALL) {
                //ActionCall??????
                result.put(funcArgs.pollFirst(), ((ActionCall) value).evaluate(currentScope));
            } else {
                result.put(funcArgs.pollFirst(), value);
            }
        }
    }
    return result;
}

From source file:com.mirth.connect.donkey.server.channel.RecoveryTask.java

private Void doCall() throws Exception {
    StorageSettings storageSettings = channel.getStorageSettings();
    Long maxMessageId = null;//from ww w .  j  a  v a  2 s.  com
    // The number of messages that were attempted to be recovered
    long attemptedMessages = 0L;
    // The number of messages that were successfully recovered
    long recoveredMessages = 0L;

    // The buffer size for each sub-task
    int sourceBufferSize = 1;
    int unfinishedBufferSize = 10;
    int pendingBufferSize = 10;
    // The minimum message Id that can be retrieved for the next query.
    long sourceMinMessageId = 0L;
    long unfinishedMinMessageId = 0L;
    long pendingMinMessageId = 0L;
    // The completed status of each sub-task
    boolean sourceComplete = false;
    boolean unfinishedComplete = false;
    boolean pendingComplete = false;
    // The queue buffer for each sub-task
    LinkedList<ConnectorMessage> sourceConnectorMessages = new LinkedList<ConnectorMessage>();
    LinkedList<Message> unfinishedMessages = new LinkedList<Message>();
    LinkedList<Message> pendingMessages = new LinkedList<Message>();

    do {
        ThreadUtils.checkInterruptedStatus();
        DonkeyDao dao = channel.getDaoFactory().getDao();

        try {
            if (maxMessageId == null) {
                // Cache the max messageId of the channel to be used in the query below
                maxMessageId = dao.getMaxMessageId(channel.getChannelId());
            }

            if (!sourceComplete && sourceConnectorMessages.isEmpty()) {
                // Fill the buffer
                sourceConnectorMessages
                        .addAll(dao.getConnectorMessages(channel.getChannelId(), channel.getServerId(), 0,
                                Status.RECEIVED, 0, sourceBufferSize, sourceMinMessageId, maxMessageId));

                // Mark the sub-task as completed if no messages were retrieved by the query to prevent the query from running again
                if (sourceConnectorMessages.isEmpty()) {
                    sourceComplete = true;
                } else {
                    /*
                     * If the source queue is on, these messages are usually ignored. Therefore
                     * we only retrieve one of these messages until we know for sure that we'll
                     * need to recover them.
                     */
                    sourceBufferSize = 100;
                }
            }

            if (!unfinishedComplete && unfinishedMessages.isEmpty()) {
                // Fill the buffer
                unfinishedMessages.addAll(dao.getUnfinishedMessages(channel.getChannelId(),
                        channel.getServerId(), unfinishedBufferSize, unfinishedMinMessageId));

                // Mark the sub-task as completed if no messages were retrieved by the query to prevent the query from running again
                if (unfinishedMessages.isEmpty()) {
                    unfinishedComplete = true;
                }
            }

            if (!pendingComplete && pendingMessages.isEmpty()) {
                // Fill the buffer
                pendingMessages.addAll(dao.getPendingConnectorMessages(channel.getChannelId(),
                        channel.getServerId(), pendingBufferSize, pendingMinMessageId));

                // Mark the sub-task as completed if no messages were retrieved by the query to prevent the query from running again
                if (pendingMessages.isEmpty()) {
                    pendingComplete = true;
                }
            }
        } finally {
            dao.close();
        }

        // Retrieve the first message of each sub-task
        ConnectorMessage sourceConnectorMessage = sourceConnectorMessages.peekFirst();
        Message unfinishedMessage = unfinishedMessages.peekFirst();
        Message pendingMessage = pendingMessages.peekFirst();

        if (!storageSettings.isMessageRecoveryEnabled()) {
            sourceComplete = true;
            unfinishedComplete = true;
            pendingComplete = true;
            if (unfinishedMessage != null || pendingMessage != null || (sourceConnectorMessage != null
                    && channel.getSourceConnector().isRespondAfterProcessing())) {
                logger.info("Incomplete messages found for channel " + channel.getName() + " ("
                        + channel.getChannelId()
                        + ") but message storage settings do not support recovery. Skipping recovery task.");
            }
        } else {
            Long messageId = null;

            try {
                /*
                 * Perform a 3-way merge. The sub-task that has the lowest messageId will be
                 * executed first. However it is possible for the unfinishedMessage and
                 * pendingMessage to have the same messageId. In these cases the unfinished
                 * sub-task should be executed and the pending sub-task should be ignored
                 */
                if (sourceConnectorMessage != null
                        && (unfinishedMessage == null
                                || sourceConnectorMessage.getMessageId() < unfinishedMessage.getMessageId())
                        && (pendingMessage == null
                                || sourceConnectorMessage.getMessageId() < pendingMessage.getMessageId())) {
                    if (!channel.getSourceConnector().isRespondAfterProcessing() && unfinishedComplete
                            && pendingComplete) {
                        /*
                         * If the other two sub-tasks are completed already and the source queue
                         * is enabled for this channel, then there is no need to continue
                         * recovering source RECEIVED messages because they will be picked up by
                         * the source queue.
                         */
                        sourceComplete = true;
                    } else {
                        // Store the messageId so we can log it out if an exception occurs
                        messageId = sourceConnectorMessage.getMessageId();
                        // Remove the message from the buffer and update the minMessageId
                        sourceMinMessageId = sourceConnectorMessages.pollFirst().getMessageId() + 1;

                        if (attemptedMessages++ == 0) {
                            logger.info("Starting message recovery for channel " + channel.getName() + " ("
                                    + channel.getChannelId() + "). Incomplete messages found.");
                        }

                        // Execute the recovery process for this message
                        channel.process(sourceConnectorMessage, true);
                        // Use this to decrement the queue size
                        channel.getSourceQueue().decrementSize();
                        // Increment the number of successfully recovered messages
                        recoveredMessages++;
                    }
                } else if (unfinishedMessage != null && (pendingMessage == null
                        || unfinishedMessage.getMessageId() <= pendingMessage.getMessageId())) {
                    // Store the messageId so we can log it out if an exception occurs
                    messageId = unfinishedMessage.getMessageId();
                    // Remove the message from the buffer and update the minMessageId
                    unfinishedMinMessageId = unfinishedMessages.pollFirst().getMessageId() + 1;

                    // If the unfinishedMessage and pendingMessage have the same messageId, remove the pendingMessage from the buffer
                    if (pendingMessage != null
                            && unfinishedMessage.getMessageId() == pendingMessage.getMessageId()) {
                        pendingMinMessageId = pendingMessages.pollFirst().getMessageId() + 1;
                        pendingMessage = pendingMessages.peekFirst();
                    }

                    if (attemptedMessages++ == 0) {
                        logger.info("Starting message recovery for channel " + channel.getName() + " ("
                                + channel.getChannelId() + "). Incomplete messages found.");
                    }

                    // Execute the recovery process for this message
                    recoverUnfinishedMessage(unfinishedMessage);
                    // Increment the number of successfully recovered messages
                    recoveredMessages++;
                } else if (pendingMessage != null) {
                    // Store the messageId so we can log it out if an exception occurs
                    messageId = pendingMessage.getMessageId();
                    // Remove the message from the buffer and update the minMessageId
                    pendingMinMessageId = pendingMessages.pollFirst().getMessageId() + 1;

                    if (attemptedMessages++ == 0) {
                        logger.info("Starting message recovery for channel " + channel.getName() + " ("
                                + channel.getChannelId() + "). Incomplete messages found.");
                    }

                    // Execute the recovery process for this message
                    recoverPendingMessage(pendingMessage);
                    // Increment the number of successfully recovered messages
                    recoveredMessages++;
                }
            } catch (InterruptedException e) {
                // This should only occur if a halt was requested so stop the entire recovery task
                throw e;
            } catch (Exception e) {
                /*
                 * If an exception occurs we skip the message and log an error. This is to
                 * prevent one bad exception or message from locking the entire channel.
                 * 
                 * If a non-Exception gets thrown (OutofMemoryError, etc) then it will
                 * intentionally not be caught here and the recovery task will be stopped.
                 */
                logger.error("Failed to recover message " + messageId + " for channel " + channel.getName()
                        + " (" + channel.getChannelId() + "): \n" + ExceptionUtils.getStackTrace(e));
            }
        }
    } while (!unfinishedComplete || !pendingComplete || !sourceComplete);

    if (attemptedMessages > 0) {
        logger.info("Completed message recovery for channel " + channel.getName() + " ("
                + channel.getChannelId() + "). Successfully recovered " + recoveredMessages + " out of "
                + attemptedMessages + " messages.");
    }

    return null;
}