Java Code Examples for java.util.Collections#asLifoQueue()

The following examples show how to use java.util.Collections#asLifoQueue() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: james-project   File: DefaultStager.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param stage the annotation that specifies this stage
 * @param mode  execution order
 */
public DefaultStager(Class<A> stage, Order mode) {
    this.stage = stage;

    Queue<Stageable> localStageables;
    switch (mode) {
        case FIRST_IN_FIRST_OUT: {
            localStageables = new ArrayDeque<>();
            break;
        }

        case FIRST_IN_LAST_OUT: {
            localStageables = Collections.asLifoQueue(new ArrayDeque<>());
            break;
        }

        default: {
            throw new IllegalArgumentException("Unknown mode: " + mode);
        }
    }
    stageables = localStageables;
}
 
Example 2
Source Project: j2objc   File: CollectionsTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * serialization/deserialization.
 */
@SuppressWarnings({ "unchecked", "boxing" })
public void testSerializationSelf_asLifoQueue() throws Exception {
    Integer testInt[] = new Integer[100];
    for (int i = 0; i < testInt.length; i++) {
        testInt[i] = new Integer(i);
    }
    Deque deque = new ArrayDeque<Integer>();
    Queue<Integer> que = Collections.asLifoQueue(deque);
    for (int i = 0; i < testInt.length; i++) {
        que.add(testInt[i]);
    }
    SerializationTest.verifySelf(que, new SerializableAssert() {
        public void assertDeserialized(Serializable initial, Serializable deserialized) {
            Queue<Integer> initque = (Queue) initial;
            Queue<Integer> deserque = (Queue) deserialized;
            while (!initque.isEmpty()) {
                assertEquals(initque.remove(), deserque.remove());
            }
        }
    });
}
 
Example 3
Source Project: j2objc   File: Collections2Test.java    License: Apache License 2.0 5 votes vote down vote up
public void test_Queue_forEach() {
    Deque<Integer> deque = new ArrayDeque<Integer>();
    deque.addFirst(2);
    deque.addFirst(1);
    deque.addFirst(0);

    Queue<Integer> queue = Collections.asLifoQueue(deque);
    ArrayList<Integer> output = new ArrayList<Integer>();
    queue.forEach(v -> output.add(v));

    assertEquals(3, output.size());
    assertEquals(0, (int)output.get(0));
    assertEquals(1, (int)output.get(1));
    assertEquals(2, (int)output.get(2));
}
 
Example 4
Source Project: j2objc   File: CollectionsTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_asLifoQueue() throws Exception {
    Integer testInt[] = new Integer[100];
    Integer test101 = new Integer(101);
    for (int i = 0; i < testInt.length; i++) {
        testInt[i] = new Integer(i);
    }
    Deque deque = new ArrayDeque<Integer>();
    Queue<Integer> que = Collections.asLifoQueue(deque);
    for (int i = 0; i < testInt.length; i++) {
        que.add(testInt[i]);
    }
    assertEquals(100, deque.size());
    assertEquals(100, que.size());
    for (int i = testInt.length - 1; i >= 0; i--) {
        assertEquals(testInt[i], deque.pop());
    }
    assertEquals(0, deque.size());
    assertEquals(0, que.size());
    for (int i = 0; i < testInt.length; i++) {
        deque.push(testInt[i]);
    }
    assertEquals(100, deque.size());
    assertEquals(100, que.size());
    Collection col = new LinkedList<Integer>();
    col.add(test101);
    que.addAll(col);
    assertEquals(test101, que.remove());
    for (int i = testInt.length - 1; i >= 0; i--) {
        assertEquals(testInt[i], que.remove());
    }
    assertEquals(0, deque.size());
    assertEquals(0, que.size());
}
 
Example 5
public static <T> Queue<T> stack() {
    return Collections.asLifoQueue(new ArrayDeque<>());
}
 
Example 6
Source Project: vjtools   File: MoreQueues.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 支持后进先出的栈,用ArrayDeque实现, 经过Collections#asLifoQueue转换顺序
 * 
 * 需设置初始长度,默认为16,数组满时成倍扩容
 * 
 * @see Collections#asLifoQueue
 */
public static <E> Queue<E> createStack(int initSize) {
	return Collections.asLifoQueue(new ArrayDeque<E>(initSize));
}
 
Example 7
Source Project: vjtools   File: MoreQueues.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 支持后进先出的无阻塞的并发栈,用ConcurrentLinkedDeque实现,经过Collections#asLifoQueue转换顺序
 * 
 * 另对于BlockingQueue接口, JDK暂无Lifo倒转实现,因此只能直接使用未调转顺序的LinkedBlockingDeque
 * 
 * @see Collections#asLifoQueue
 */
public static <E> Queue<E> createConcurrentStack() {
	return (Queue<E>) Collections.asLifoQueue(QueueUtil.newConcurrentNonBlockingDeque());
}
 
Example 8
Source Project: vjtools   File: MoreQueues.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 支持后进先出的栈,用ArrayDeque实现, 经过Collections#asLifoQueue()转换顺序
 * 
 * 需设置初始长度,默认为16,数组满时成倍扩容
 * 
 * @see Collections#asLifoQueue()
 */
public static <E> Queue<E> createStack(int initSize) {
	return Collections.asLifoQueue(new ArrayDeque<E>(initSize));
}
 
Example 9
Source Project: vjtools   File: MoreQueues.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 支持后进先出的无阻塞的并发栈,用ConcurrentLinkedDeque实现,经过Collections#asLifoQueue()转换顺序
 * 
 * 另对于BlockingQueue接口, JDK暂无Lifo倒转实现,因此只能直接使用未调转顺序的LinkedBlockingDeque
 * 
 * @see Collections#asLifoQueue()
 */
public static <E> Queue<E> createConcurrentStack() {
	return (Queue<E>) Collections.asLifoQueue(QueueUtil.newConcurrentNonBlockingDeque());
}