Example usage for java.util ArrayDeque poll

List of usage examples for java.util ArrayDeque poll

Introduction

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

Prototype

public E poll() 

Source Link

Document

Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.

Usage

From source file:Main.java

public static void main(String[] args) {

    ArrayDeque<Integer> deque = new ArrayDeque<Integer>(8);

    deque.add(25);//from www  . ja v  a 2s.co  m
    deque.add(2);
    deque.add(1);
    deque.add(18);

    System.out.println(deque);

    int retval = deque.poll();
    System.out.println("Element removed is " + retval);

    System.out.println(deque);
}

From source file:com.espertech.esper.dispatch.DispatchServiceImpl.java

private static void dispatchFromQueue(ArrayDeque<Dispatchable> dispatchQueue) {
    while (true) {
        Dispatchable next = dispatchQueue.poll();
        if (next != null) {
            next.execute();/*from  www . j  ava  2s .c  o  m*/
        } else {
            break;
        }
    }
}

From source file:bachelorthesis.captchabuilder.builder.CaptchaBuildSequenceParser.java

/**
 * Creates new elementCreators from the list CaptchaElementCreatorBuilders
 * in the CaptchaBuilder.//from   w  w w.  j a  v a 2  s .  c om
 *
 * @param builder
 *                <p/>
 * @throws ParseException
 */
public static void shortParse(CaptchaBuilder builder) throws ParseException {
    ArrayDeque<CaptchaElementCreatorBuilder> elementBuilders = builder.getBuilders().clone();
    ArrayDeque<BuildSequenceOptions> sequence = new ArrayDeque<>();
    for (String lvl1Arg : builder.getBuildSequence().split(CaptchaConstants.buildSequencelvl1Delim)) {
        if (!lvl1Arg.isEmpty()) {
            try {
                String[] optionArgs = lvl1Arg.split(CaptchaConstants.buildSequencelvl2Delim);
                sequence.offer(BuildSequenceOptions.valueOf(optionArgs[0]));
            } catch (IllegalArgumentException e) {
                throw new ParseException(e.getMessage());
            }
        }
    }

    for (BuildSequenceOptions buildSequence : sequence) {
        switch (buildSequence) {
        case BACKGROUND: {
            CaptchaElementCreatorBuilder elementBuilder = elementBuilders.poll();
            if (elementBuilder instanceof BackgroundProducerBuilder) {
                builder.addBackground((BackgroundProducer) elementBuilder.create());
            } else {
                throw new ParseException("ShortParse Failed ... How is that possible?\n"
                        + "Class Mismatch: Got " + elementBuilder.getClass().getSimpleName() + " and expected "
                        + BackgroundProducerBuilder.class.getSimpleName());
            }
        }
            break;
        case BORDER: {
            CaptchaElementCreatorBuilder elementBuilder = elementBuilders.poll();
            if (elementBuilder instanceof BorderProducerBuilder) {
                builder.addBorder((BorderProducer) elementBuilder.create());
            } else {
                throw new ParseException("ShortParse Failed ... How is that possible?\n"
                        + "Class Mismatch: Got " + elementBuilder.getClass().getSimpleName() + " and expected "
                        + BorderProducerBuilder.class.getSimpleName());
            }
        }
            break;
        case GIMP: {
            CaptchaElementCreatorBuilder elementBuilder = elementBuilders.poll();
            if (elementBuilder instanceof GimpyRendererBuilder) {
                builder.gimp((GimpyRenderer) elementBuilder.create());
            } else {
                throw new ParseException("ShortParse Failed ... How is that possible?\n"
                        + "Class Mismatch: Got " + elementBuilder.getClass().getSimpleName() + " and expected "
                        + GimpyRendererBuilder.class.getSimpleName());
            }
        }
            break;
        case NOISE: {
            CaptchaElementCreatorBuilder elementBuilder = elementBuilders.poll();
            if (elementBuilder instanceof NoiseProducerBuilder) {
                builder.addNoise((NoiseProducer) elementBuilder.create());
            } else {
                throw new ParseException("ShortParse Failed ... How is that possible?\n"
                        + "Class Mismatch: Got " + elementBuilder.getClass().getSimpleName() + " and expected "
                        + NoiseProducerBuilder.class.getSimpleName());
            }
        }
            break;
        case TEXT: {
            CaptchaElementCreatorBuilder elementBuilder1 = elementBuilders.poll();
            CaptchaElementCreatorBuilder elementBuilder2 = elementBuilders.poll();
            if (elementBuilder1 instanceof TextProducerBuilder
                    && elementBuilder2 instanceof WordRendererBuilder) {
                builder.addText((TextProducer) elementBuilder1.create(),
                        (WordRenderer) elementBuilder2.create());
            } else {
                throw new ParseException("ShortParse Failed ... How is that possible?\n"
                        + "Class Mismatch: Got " + elementBuilder1.getClass().getSimpleName() + " and expected "
                        + TextProducerBuilder.class.getSimpleName() + "\n" + "Class Mismatch: Got "
                        + elementBuilder2.getClass().getSimpleName() + " and expected "
                        + WordRendererBuilder.class.getSimpleName());
            }
        }
        }
    }
}

From source file:com.espertech.esper.collection.TestTimeWindow.java

public void testAdd() {
    assertTrue(window.getOldestTimestamp() == null);
    assertTrue(window.isEmpty());/*w  w w.ja  va 2s  .  co m*/

    window.add(19, beans[0]);
    assertTrue(window.getOldestTimestamp() == 19L);
    assertFalse(window.isEmpty());
    window.add(19, beans[1]);
    assertTrue(window.getOldestTimestamp() == 19L);
    window.add(20, beans[2]);
    assertTrue(window.getOldestTimestamp() == 19L);
    window.add(20, beans[3]);
    window.add(21, beans[4]);
    window.add(22, beans[5]);
    assertTrue(window.getOldestTimestamp() == 19L);

    ArrayDeque<EventBean> beanList = window.expireEvents(19);
    assertTrue(beanList == null);

    beanList = window.expireEvents(20);
    assertTrue(beanList.size() == 2);
    assertTrue(beanList.poll() == beans[0]);
    assertTrue(beanList.poll() == beans[1]);

    beanList = window.expireEvents(21);
    assertTrue(beanList.size() == 2);
    assertTrue(beanList.poll() == beans[2]);
    assertTrue(beanList.poll() == beans[3]);
    assertFalse(window.isEmpty());
    assertTrue(window.getOldestTimestamp() == 21);

    beanList = window.expireEvents(22);
    assertTrue(beanList.size() == 1);
    assertTrue(beanList.poll() == beans[4]);
    assertFalse(window.isEmpty());
    assertTrue(window.getOldestTimestamp() == 22);

    beanList = window.expireEvents(23);
    assertTrue(beanList.size() == 1);
    assertTrue(beanList.poll() == beans[5]);
    assertTrue(window.isEmpty());
    assertTrue(window.getOldestTimestamp() == null);

    beanList = window.expireEvents(23);
    assertTrue(beanList == null);
    assertTrue(window.isEmpty());
    assertTrue(window.getOldestTimestamp() == null);
}

From source file:androidx.navigation.NavDeepLinkBuilder.java

private void fillInIntent() {
    NavDestination node = null;/*ww  w .java 2 s .c o  m*/
    ArrayDeque<NavDestination> possibleDestinations = new ArrayDeque<>();
    possibleDestinations.add(mGraph);
    while (!possibleDestinations.isEmpty() && node == null) {
        NavDestination destination = possibleDestinations.poll();
        if (destination.getId() == mDestId) {
            node = destination;
        } else if (destination instanceof NavGraph) {
            for (NavDestination child : (NavGraph) destination) {
                possibleDestinations.add(child);
            }
        }
    }
    if (node == null) {
        final String dest = NavDestination.getDisplayName(mContext, mDestId);
        throw new IllegalArgumentException(
                "navigation destination " + dest + " is unknown to this NavController");
    }
    mIntent.putExtra(NavController.KEY_DEEP_LINK_IDS, node.buildDeepLinkIds());
}

From source file:com.heliosdecompiler.helios.gui.controller.FileTreeController.java

@FXML
public void initialize() {
    this.rootItem = new TreeItem<>(new TreeNode("[root]"));
    this.root.setRoot(this.rootItem);
    this.root.setCellFactory(new TreeCellFactory<>(node -> {
        if (node.getParent() == null) {
            ContextMenu export = new ContextMenu();

            MenuItem exportItem = new MenuItem("Export");

            export.setOnAction(e -> {
                File file = messageHandler.chooseFile().withInitialDirectory(new File("."))
                        .withTitle(Message.GENERIC_CHOOSE_EXPORT_LOCATION_JAR.format())
                        .withExtensionFilter(new FileFilter(Message.FILETYPE_JAVA_ARCHIVE.format(), "*.jar"),
                                true)//from  w w  w .ja v a2  s . c  o m
                        .promptSave();

                OpenedFile openedFile = (OpenedFile) node.getMetadata().get(OpenedFile.OPENED_FILE);

                Map<String, byte[]> clone = new HashMap<>(openedFile.getContents());

                backgroundTaskHelper.submit(
                        new BackgroundTask(Message.TASK_SAVING_FILE.format(node.getDisplayName()), true, () -> {
                            try {
                                if (!file.exists()) {
                                    if (!file.createNewFile()) {
                                        throw new IOException("Could not create export file");
                                    }
                                }

                                try (ZipOutputStream zipOutputStream = new ZipOutputStream(
                                        new FileOutputStream(file))) {
                                    for (Map.Entry<String, byte[]> ent : clone.entrySet()) {
                                        ZipEntry zipEntry = new ZipEntry(ent.getKey());
                                        zipOutputStream.putNextEntry(zipEntry);
                                        zipOutputStream.write(ent.getValue());
                                        zipOutputStream.closeEntry();
                                    }
                                }

                                messageHandler.handleMessage(Message.GENERIC_EXPORTED.format());
                            } catch (IOException ex) {
                                messageHandler.handleException(Message.ERROR_IOEXCEPTION_OCCURRED.format(), ex);
                            }
                        }));
            });

            export.getItems().add(exportItem);
            return export;
        }
        return null;
    }));

    root.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
        if (event.getCode() == KeyCode.ENTER) {
            TreeItem<TreeNode> selected = this.root.getSelectionModel().getSelectedItem();
            if (selected != null) {
                if (selected.getChildren().size() != 0) {
                    selected.setExpanded(!selected.isExpanded());
                } else {
                    getParentController().getAllFilesViewerController().handleClick(selected.getValue());
                }
            }
        }
    });

    Tooltip tooltip = new Tooltip();
    StringBuilder search = new StringBuilder();

    List<TreeItem<TreeNode>> searchContext = new ArrayList<>();
    AtomicInteger searchIndex = new AtomicInteger();

    root.focusedProperty().addListener((observable, oldValue, newValue) -> {
        if (!newValue) {
            tooltip.hide();
            search.setLength(0);
        }
    });

    root.boundsInLocalProperty().addListener((observable, oldValue, newValue) -> {
        Bounds bounds = root.localToScreen(newValue);
        tooltip.setAnchorX(bounds.getMinX());
        tooltip.setAnchorY(bounds.getMinY());
    });

    root.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
        if (tooltip.isShowing() && event.getCode() == KeyCode.UP) {
            if (searchIndex.decrementAndGet() < 0) {
                searchIndex.set(searchContext.size() - 1);
            }
        } else if (tooltip.isShowing() && event.getCode() == KeyCode.DOWN) {
            if (searchIndex.incrementAndGet() >= searchContext.size()) {
                searchIndex.set(0);
            }
        } else {
            return;
        }
        event.consume();

        root.scrollTo(root.getRow(searchContext.get(searchIndex.get())));
        root.getSelectionModel().select(searchContext.get(searchIndex.get()));
    });

    root.addEventHandler(KeyEvent.KEY_TYPED, event -> {
        if (event.getCharacter().charAt(0) == '\b') {
            if (search.length() > 0) {
                search.setLength(search.length() - 1);
            }
        } else if (event.getCharacter().charAt(0) == '\u001B') { //esc
            tooltip.hide();
            search.setLength(0);
            return;
        } else if (search.length() > 0
                || (search.length() == 0 && StringUtils.isAlphanumeric(event.getCharacter()))) {
            search.append(event.getCharacter());
            if (!tooltip.isShowing()) {
                tooltip.show(root.getScene().getWindow());
            }
        }

        if (!tooltip.isShowing())
            return;

        String str = search.toString();
        tooltip.setText("Search for: " + str);

        searchContext.clear();

        ArrayDeque<TreeItem<TreeNode>> deque = new ArrayDeque<>();
        deque.addAll(rootItem.getChildren());

        while (!deque.isEmpty()) {
            TreeItem<TreeNode> item = deque.poll();
            if (item.getValue().getDisplayName().contains(str)) {
                searchContext.add(item);
            }
            if (item.isExpanded() && item.getChildren().size() > 0)
                deque.addAll(item.getChildren());
        }

        searchIndex.set(0);
        if (searchContext.size() > 0) {
            root.scrollTo(root.getRow(searchContext.get(0)));
            root.getSelectionModel().select(searchContext.get(0));
        }
    });

    openedFileController.loadedFiles().addListener((MapChangeListener<String, OpenedFile>) change -> {
        if (change.getValueAdded() != null) {
            updateTree(change.getValueAdded());
        }
        if (change.getValueRemoved() != null) {
            this.rootItem.getChildren()
                    .removeIf(ti -> ti.getValue().equals(change.getValueRemoved().getRoot()));
        }
    });
}

From source file:com.google.gwt.emultest.java.util.ArrayDequeTest.java

public void testPoll() {
    Object o1 = new Object();
    Object o2 = new Object();

    ArrayDeque<Object> deque = new ArrayDeque<>();
    assertNull(deque.poll());

    deque.add(o1);//  ww  w  . ja  v  a2  s . com
    assertEquals(o1, deque.poll());
    assertTrue(deque.isEmpty());

    deque.add(o1);
    deque.add(o2);
    assertEquals(o1, deque.poll());
    checkDequeSizeAndContent(deque, o2);
    assertEquals(o2, deque.poll());
    assertTrue(deque.isEmpty());
    assertNull(deque.poll());
}