Example usage for java.util ArrayDeque addLast

List of usage examples for java.util ArrayDeque addLast

Introduction

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

Prototype

public void addLast(E e) 

Source Link

Document

Inserts the specified element at the end of this deque.

Usage

From source file:Main.java

public static void main(String[] args) {

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

    deque.add(1);/*from  ww  w  .  jav  a2  s .  c  o m*/
    deque.add(2);
    deque.add(1);
    deque.add(18);
    deque.add(4);
    deque.add(24);

    // the values will be printed in the same order
    deque.addLast(10);
    deque.addLast(12);

    System.out.println(deque);
}

From source file:com.facebook.litho.dataflow.DataFlowGraph.java

private void regenerateSortedNodes() {
    mSortedNodes.clear();/*www. j  av  a  2s  . c o  m*/

    if (mBindings.size() == 0) {
        return;
    }

    final ArraySet<ValueNode> leafNodes = ComponentsPools.acquireArraySet();
    final SimpleArrayMap<ValueNode, Integer> nodesToOutputsLeft = new SimpleArrayMap<>();

    for (int i = 0, bindingsSize = mBindingToNodes.size(); i < bindingsSize; i++) {
        final ArraySet<ValueNode> nodes = mBindingToNodes.valueAt(i);
        for (int j = 0, nodesSize = nodes.size(); j < nodesSize; j++) {
            final ValueNode node = nodes.valueAt(j);
            final int outputCount = node.getOutputCount();
            if (outputCount == 0) {
                leafNodes.add(node);
            } else {
                nodesToOutputsLeft.put(node, outputCount);
            }
        }
    }

    if (!nodesToOutputsLeft.isEmpty() && leafNodes.isEmpty()) {
        throw new DetectedCycleException("Graph has nodes, but they represent a cycle with no leaf nodes!");
    }

    final ArrayDeque<ValueNode> nodesToProcess = ComponentsPools.acquireArrayDeque();
    nodesToProcess.addAll(leafNodes);

    while (!nodesToProcess.isEmpty()) {
        final ValueNode next = nodesToProcess.pollFirst();
        mSortedNodes.add(next);
        for (int i = 0, count = next.getInputCount(); i < count; i++) {
            final ValueNode input = next.getInputAt(i);
            final int outputsLeft = nodesToOutputsLeft.get(input) - 1;
            nodesToOutputsLeft.put(input, outputsLeft);
            if (outputsLeft == 0) {
                nodesToProcess.addLast(input);
            } else if (outputsLeft < 0) {
                throw new DetectedCycleException("Detected cycle.");
            }
        }
    }

    int expectedTotalNodes = nodesToOutputsLeft.size() + leafNodes.size();
    if (mSortedNodes.size() != expectedTotalNodes) {
        throw new DetectedCycleException(
                "Had unreachable nodes in graph -- this likely means there was a cycle");
    }

    Collections.reverse(mSortedNodes);
    mIsDirty = false;

    ComponentsPools.release(nodesToProcess);
    ComponentsPools.release(leafNodes);
}

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

public void testAddLast() {
    Object o1 = new Object();
    Object o2 = new Object();
    Object o3 = new Object();

    ArrayDeque<Object> deque = new ArrayDeque<>();
    deque.addLast(o1);
    checkDequeSizeAndContent(deque, o1);
    deque.addLast(o2);/*w  ww .  java 2 s  .c  om*/
    checkDequeSizeAndContent(deque, o1, o2);
    deque.addLast(o3);
    checkDequeSizeAndContent(deque, o1, o2, o3);

    try {
        deque.addLast(null);
        fail();
    } catch (NullPointerException expected) {
    }
}

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

public void testFailFastDescendingIterator() {
    ArrayDeque<Object> deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    Iterator<Object> it = deque.descendingIterator();
    it.next();//from w  ww . j  a v a  2  s.c o  m
    deque.removeLast();
    try {
        it.next();
    } catch (ConcurrentModificationException e) {
        fail();
    }
    deque.removeFirst();
    try {
        it.next();
        fail();
    } catch (ConcurrentModificationException expected) {
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.descendingIterator();
    it.next();
    deque.clear();
    try {
        it.next();
        fail();
    } catch (ConcurrentModificationException expected) {
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.descendingIterator();
    it.next();
    deque.addLast(new Object());
    try {
        it.next();
    } catch (ConcurrentModificationException e) {
        fail();
    }
    deque.addFirst(new Object());
    try {
        it.next();
        fail();
    } catch (ConcurrentModificationException expected) {
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.descendingIterator();
    it.next();
    it.next();
    deque.removeLast();
    try {
        it.remove();
    } catch (ConcurrentModificationException e) {
        fail();
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.descendingIterator();
    it.next();
    it.next();
    deque.removeLast();
    deque.removeLast();
    try {
        it.remove();
        fail();
    } catch (ConcurrentModificationException expected) {
    }
}

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

public void testFailFastIterator() {
    ArrayDeque<Object> deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    Iterator<Object> it = deque.iterator();
    it.next();//from w  w  w.  j  ava 2s  . c om
    deque.removeFirst();
    try {
        it.next();
    } catch (ConcurrentModificationException e) {
        fail();
    }
    deque.removeLast();
    try {
        it.next();
        fail();
    } catch (ConcurrentModificationException expected) {
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.iterator();
    it.next();
    deque.clear();
    try {
        it.next();
        fail();
    } catch (ConcurrentModificationException expected) {
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.iterator();
    it.next();
    deque.addFirst(new Object());
    try {
        it.next();
    } catch (ConcurrentModificationException e) {
        fail();
    }
    deque.addLast(new Object());
    try {
        it.next();
        fail();
    } catch (ConcurrentModificationException expected) {
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.iterator();
    it.next();
    it.next();
    deque.removeFirst();
    try {
        it.remove();
    } catch (ConcurrentModificationException e) {
        fail();
    }

    deque = new ArrayDeque<>(asList(getFullNonNullElements()));
    it = deque.iterator();
    it.next();
    it.next();
    deque.removeFirst();
    deque.removeFirst();
    try {
        it.remove();
        fail();
    } catch (ConcurrentModificationException expected) {
    }
}