jaroslav@1890: /* jaroslav@1890: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1890: * jaroslav@1890: * This code is free software; you can redistribute it and/or modify it jaroslav@1890: * under the terms of the GNU General Public License version 2 only, as jaroslav@1890: * published by the Free Software Foundation. Oracle designates this jaroslav@1890: * particular file as subject to the "Classpath" exception as provided jaroslav@1890: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1890: * jaroslav@1890: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1890: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1890: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1890: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1890: * accompanied this code). jaroslav@1890: * jaroslav@1890: * You should have received a copy of the GNU General Public License version jaroslav@1890: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1890: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1890: * jaroslav@1890: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1890: * or visit www.oracle.com if you need additional information or have any jaroslav@1890: * questions. jaroslav@1890: */ jaroslav@1890: jaroslav@1890: /* jaroslav@1890: * This file is available under and governed by the GNU General Public jaroslav@1890: * License version 2 only, as published by the Free Software Foundation. jaroslav@1890: * However, the following notice accompanied the original version of this jaroslav@1890: * file: jaroslav@1890: * jaroslav@1890: * Written by Doug Lea with assistance from members of JCP JSR-166 jaroslav@1890: * Expert Group and released to the public domain, as explained at jaroslav@1890: * http://creativecommons.org/publicdomain/zero/1.0/ jaroslav@1890: */ jaroslav@1890: jaroslav@1890: package java.util.concurrent; jaroslav@1890: jaroslav@1890: import java.util.AbstractQueue; jaroslav@1890: import java.util.Collection; jaroslav@1890: import java.util.Iterator; jaroslav@1890: import java.util.NoSuchElementException; jaroslav@1890: import java.util.concurrent.locks.Condition; jaroslav@1890: import java.util.concurrent.locks.ReentrantLock; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * An optionally-bounded {@linkplain BlockingDeque blocking deque} based on jaroslav@1890: * linked nodes. jaroslav@1890: * jaroslav@1890: *

The optional capacity bound constructor argument serves as a jaroslav@1890: * way to prevent excessive expansion. The capacity, if unspecified, jaroslav@1890: * is equal to {@link Integer#MAX_VALUE}. Linked nodes are jaroslav@1890: * dynamically created upon each insertion unless this would bring the jaroslav@1890: * deque above capacity. jaroslav@1890: * jaroslav@1890: *

Most operations run in constant time (ignoring time spent jaroslav@1890: * blocking). Exceptions include {@link #remove(Object) remove}, jaroslav@1890: * {@link #removeFirstOccurrence removeFirstOccurrence}, {@link jaroslav@1890: * #removeLastOccurrence removeLastOccurrence}, {@link #contains jaroslav@1890: * contains}, {@link #iterator iterator.remove()}, and the bulk jaroslav@1890: * operations, all of which run in linear time. jaroslav@1890: * jaroslav@1890: *

This class and its iterator implement all of the jaroslav@1890: * optional methods of the {@link Collection} and {@link jaroslav@1890: * Iterator} interfaces. jaroslav@1890: * jaroslav@1890: *

This class is a member of the jaroslav@1890: * jaroslav@1890: * Java Collections Framework. jaroslav@1890: * jaroslav@1890: * @since 1.6 jaroslav@1890: * @author Doug Lea jaroslav@1890: * @param the type of elements held in this collection jaroslav@1890: */ jaroslav@1890: public class LinkedBlockingDeque jaroslav@1890: extends AbstractQueue jaroslav@1890: implements BlockingDeque, java.io.Serializable { jaroslav@1890: jaroslav@1890: /* jaroslav@1890: * Implemented as a simple doubly-linked list protected by a jaroslav@1890: * single lock and using conditions to manage blocking. jaroslav@1890: * jaroslav@1890: * To implement weakly consistent iterators, it appears we need to jaroslav@1890: * keep all Nodes GC-reachable from a predecessor dequeued Node. jaroslav@1890: * That would cause two problems: jaroslav@1890: * - allow a rogue Iterator to cause unbounded memory retention jaroslav@1890: * - cause cross-generational linking of old Nodes to new Nodes if jaroslav@1890: * a Node was tenured while live, which generational GCs have a jaroslav@1890: * hard time dealing with, causing repeated major collections. jaroslav@1890: * However, only non-deleted Nodes need to be reachable from jaroslav@1890: * dequeued Nodes, and reachability does not necessarily have to jaroslav@1890: * be of the kind understood by the GC. We use the trick of jaroslav@1890: * linking a Node that has just been dequeued to itself. Such a jaroslav@1890: * self-link implicitly means to jump to "first" (for next links) jaroslav@1890: * or "last" (for prev links). jaroslav@1890: */ jaroslav@1890: jaroslav@1890: /* jaroslav@1890: * We have "diamond" multiple interface/abstract class inheritance jaroslav@1890: * here, and that introduces ambiguities. Often we want the jaroslav@1890: * BlockingDeque javadoc combined with the AbstractQueue jaroslav@1890: * implementation, so a lot of method specs are duplicated here. jaroslav@1890: */ jaroslav@1890: jaroslav@1890: private static final long serialVersionUID = -387911632671998426L; jaroslav@1890: jaroslav@1890: /** Doubly-linked list node class */ jaroslav@1890: static final class Node { jaroslav@1890: /** jaroslav@1890: * The item, or null if this node has been removed. jaroslav@1890: */ jaroslav@1890: E item; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * One of: jaroslav@1890: * - the real predecessor Node jaroslav@1890: * - this Node, meaning the predecessor is tail jaroslav@1890: * - null, meaning there is no predecessor jaroslav@1890: */ jaroslav@1890: Node prev; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * One of: jaroslav@1890: * - the real successor Node jaroslav@1890: * - this Node, meaning the successor is head jaroslav@1890: * - null, meaning there is no successor jaroslav@1890: */ jaroslav@1890: Node next; jaroslav@1890: jaroslav@1890: Node(E x) { jaroslav@1890: item = x; jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Pointer to first node. jaroslav@1890: * Invariant: (first == null && last == null) || jaroslav@1890: * (first.prev == null && first.item != null) jaroslav@1890: */ jaroslav@1890: transient Node first; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Pointer to last node. jaroslav@1890: * Invariant: (first == null && last == null) || jaroslav@1890: * (last.next == null && last.item != null) jaroslav@1890: */ jaroslav@1890: transient Node last; jaroslav@1890: jaroslav@1890: /** Number of items in the deque */ jaroslav@1890: private transient int count; jaroslav@1890: jaroslav@1890: /** Maximum number of items in the deque */ jaroslav@1890: private final int capacity; jaroslav@1890: jaroslav@1890: /** Main lock guarding all access */ jaroslav@1890: final ReentrantLock lock = new ReentrantLock(); jaroslav@1890: jaroslav@1890: /** Condition for waiting takes */ jaroslav@1890: private final Condition notEmpty = lock.newCondition(); jaroslav@1890: jaroslav@1890: /** Condition for waiting puts */ jaroslav@1890: private final Condition notFull = lock.newCondition(); jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Creates a {@code LinkedBlockingDeque} with a capacity of jaroslav@1890: * {@link Integer#MAX_VALUE}. jaroslav@1890: */ jaroslav@1890: public LinkedBlockingDeque() { jaroslav@1890: this(Integer.MAX_VALUE); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Creates a {@code LinkedBlockingDeque} with the given (fixed) capacity. jaroslav@1890: * jaroslav@1890: * @param capacity the capacity of this deque jaroslav@1890: * @throws IllegalArgumentException if {@code capacity} is less than 1 jaroslav@1890: */ jaroslav@1890: public LinkedBlockingDeque(int capacity) { jaroslav@1890: if (capacity <= 0) throw new IllegalArgumentException(); jaroslav@1890: this.capacity = capacity; jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Creates a {@code LinkedBlockingDeque} with a capacity of jaroslav@1890: * {@link Integer#MAX_VALUE}, initially containing the elements of jaroslav@1890: * the given collection, added in traversal order of the jaroslav@1890: * collection's iterator. jaroslav@1890: * jaroslav@1890: * @param c the collection of elements to initially contain jaroslav@1890: * @throws NullPointerException if the specified collection or any jaroslav@1890: * of its elements are null jaroslav@1890: */ jaroslav@1890: public LinkedBlockingDeque(Collection c) { jaroslav@1890: this(Integer.MAX_VALUE); jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); // Never contended, but necessary for visibility jaroslav@1890: try { jaroslav@1890: for (E e : c) { jaroslav@1890: if (e == null) jaroslav@1890: throw new NullPointerException(); jaroslav@1890: if (!linkLast(new Node(e))) jaroslav@1890: throw new IllegalStateException("Deque full"); jaroslav@1890: } jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: jaroslav@1890: // Basic linking and unlinking operations, called only while holding lock jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Links node as first element, or returns false if full. jaroslav@1890: */ jaroslav@1890: private boolean linkFirst(Node node) { jaroslav@1890: // assert lock.isHeldByCurrentThread(); jaroslav@1890: if (count >= capacity) jaroslav@1890: return false; jaroslav@1890: Node f = first; jaroslav@1890: node.next = f; jaroslav@1890: first = node; jaroslav@1890: if (last == null) jaroslav@1890: last = node; jaroslav@1890: else jaroslav@1890: f.prev = node; jaroslav@1890: ++count; jaroslav@1890: notEmpty.signal(); jaroslav@1890: return true; jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Links node as last element, or returns false if full. jaroslav@1890: */ jaroslav@1890: private boolean linkLast(Node node) { jaroslav@1890: // assert lock.isHeldByCurrentThread(); jaroslav@1890: if (count >= capacity) jaroslav@1890: return false; jaroslav@1890: Node l = last; jaroslav@1890: node.prev = l; jaroslav@1890: last = node; jaroslav@1890: if (first == null) jaroslav@1890: first = node; jaroslav@1890: else jaroslav@1890: l.next = node; jaroslav@1890: ++count; jaroslav@1890: notEmpty.signal(); jaroslav@1890: return true; jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Removes and returns first element, or null if empty. jaroslav@1890: */ jaroslav@1890: private E unlinkFirst() { jaroslav@1890: // assert lock.isHeldByCurrentThread(); jaroslav@1890: Node f = first; jaroslav@1890: if (f == null) jaroslav@1890: return null; jaroslav@1890: Node n = f.next; jaroslav@1890: E item = f.item; jaroslav@1890: f.item = null; jaroslav@1890: f.next = f; // help GC jaroslav@1890: first = n; jaroslav@1890: if (n == null) jaroslav@1890: last = null; jaroslav@1890: else jaroslav@1890: n.prev = null; jaroslav@1890: --count; jaroslav@1890: notFull.signal(); jaroslav@1890: return item; jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Removes and returns last element, or null if empty. jaroslav@1890: */ jaroslav@1890: private E unlinkLast() { jaroslav@1890: // assert lock.isHeldByCurrentThread(); jaroslav@1890: Node l = last; jaroslav@1890: if (l == null) jaroslav@1890: return null; jaroslav@1890: Node p = l.prev; jaroslav@1890: E item = l.item; jaroslav@1890: l.item = null; jaroslav@1890: l.prev = l; // help GC jaroslav@1890: last = p; jaroslav@1890: if (p == null) jaroslav@1890: first = null; jaroslav@1890: else jaroslav@1890: p.next = null; jaroslav@1890: --count; jaroslav@1890: notFull.signal(); jaroslav@1890: return item; jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Unlinks x. jaroslav@1890: */ jaroslav@1890: void unlink(Node x) { jaroslav@1890: // assert lock.isHeldByCurrentThread(); jaroslav@1890: Node p = x.prev; jaroslav@1890: Node n = x.next; jaroslav@1890: if (p == null) { jaroslav@1890: unlinkFirst(); jaroslav@1890: } else if (n == null) { jaroslav@1890: unlinkLast(); jaroslav@1890: } else { jaroslav@1890: p.next = n; jaroslav@1890: n.prev = p; jaroslav@1890: x.item = null; jaroslav@1890: // Don't mess with x's links. They may still be in use by jaroslav@1890: // an iterator. jaroslav@1890: --count; jaroslav@1890: notFull.signal(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: // BlockingDeque methods jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws IllegalStateException {@inheritDoc} jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public void addFirst(E e) { jaroslav@1890: if (!offerFirst(e)) jaroslav@1890: throw new IllegalStateException("Deque full"); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws IllegalStateException {@inheritDoc} jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public void addLast(E e) { jaroslav@1890: if (!offerLast(e)) jaroslav@1890: throw new IllegalStateException("Deque full"); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public boolean offerFirst(E e) { jaroslav@1890: if (e == null) throw new NullPointerException(); jaroslav@1890: Node node = new Node(e); jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: return linkFirst(node); jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public boolean offerLast(E e) { jaroslav@1890: if (e == null) throw new NullPointerException(); jaroslav@1890: Node node = new Node(e); jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: return linkLast(node); jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: * @throws InterruptedException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public void putFirst(E e) throws InterruptedException { jaroslav@1890: if (e == null) throw new NullPointerException(); jaroslav@1890: Node node = new Node(e); jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: while (!linkFirst(node)) jaroslav@1890: notFull.await(); jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: * @throws InterruptedException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public void putLast(E e) throws InterruptedException { jaroslav@1890: if (e == null) throw new NullPointerException(); jaroslav@1890: Node node = new Node(e); jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: while (!linkLast(node)) jaroslav@1890: notFull.await(); jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: * @throws InterruptedException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public boolean offerFirst(E e, long timeout, TimeUnit unit) jaroslav@1890: throws InterruptedException { jaroslav@1890: if (e == null) throw new NullPointerException(); jaroslav@1890: Node node = new Node(e); jaroslav@1890: long nanos = unit.toNanos(timeout); jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lockInterruptibly(); jaroslav@1890: try { jaroslav@1890: while (!linkFirst(node)) { jaroslav@1890: if (nanos <= 0) jaroslav@1890: return false; jaroslav@1890: nanos = notFull.awaitNanos(nanos); jaroslav@1890: } jaroslav@1890: return true; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: * @throws InterruptedException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public boolean offerLast(E e, long timeout, TimeUnit unit) jaroslav@1890: throws InterruptedException { jaroslav@1890: if (e == null) throw new NullPointerException(); jaroslav@1890: Node node = new Node(e); jaroslav@1890: long nanos = unit.toNanos(timeout); jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lockInterruptibly(); jaroslav@1890: try { jaroslav@1890: while (!linkLast(node)) { jaroslav@1890: if (nanos <= 0) jaroslav@1890: return false; jaroslav@1890: nanos = notFull.awaitNanos(nanos); jaroslav@1890: } jaroslav@1890: return true; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NoSuchElementException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public E removeFirst() { jaroslav@1890: E x = pollFirst(); jaroslav@1890: if (x == null) throw new NoSuchElementException(); jaroslav@1890: return x; jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NoSuchElementException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public E removeLast() { jaroslav@1890: E x = pollLast(); jaroslav@1890: if (x == null) throw new NoSuchElementException(); jaroslav@1890: return x; jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E pollFirst() { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: return unlinkFirst(); jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E pollLast() { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: return unlinkLast(); jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E takeFirst() throws InterruptedException { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: E x; jaroslav@1890: while ( (x = unlinkFirst()) == null) jaroslav@1890: notEmpty.await(); jaroslav@1890: return x; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E takeLast() throws InterruptedException { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: E x; jaroslav@1890: while ( (x = unlinkLast()) == null) jaroslav@1890: notEmpty.await(); jaroslav@1890: return x; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E pollFirst(long timeout, TimeUnit unit) jaroslav@1890: throws InterruptedException { jaroslav@1890: long nanos = unit.toNanos(timeout); jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lockInterruptibly(); jaroslav@1890: try { jaroslav@1890: E x; jaroslav@1890: while ( (x = unlinkFirst()) == null) { jaroslav@1890: if (nanos <= 0) jaroslav@1890: return null; jaroslav@1890: nanos = notEmpty.awaitNanos(nanos); jaroslav@1890: } jaroslav@1890: return x; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E pollLast(long timeout, TimeUnit unit) jaroslav@1890: throws InterruptedException { jaroslav@1890: long nanos = unit.toNanos(timeout); jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lockInterruptibly(); jaroslav@1890: try { jaroslav@1890: E x; jaroslav@1890: while ( (x = unlinkLast()) == null) { jaroslav@1890: if (nanos <= 0) jaroslav@1890: return null; jaroslav@1890: nanos = notEmpty.awaitNanos(nanos); jaroslav@1890: } jaroslav@1890: return x; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NoSuchElementException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public E getFirst() { jaroslav@1890: E x = peekFirst(); jaroslav@1890: if (x == null) throw new NoSuchElementException(); jaroslav@1890: return x; jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NoSuchElementException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public E getLast() { jaroslav@1890: E x = peekLast(); jaroslav@1890: if (x == null) throw new NoSuchElementException(); jaroslav@1890: return x; jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E peekFirst() { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: return (first == null) ? null : first.item; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E peekLast() { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: return (last == null) ? null : last.item; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: public boolean removeFirstOccurrence(Object o) { jaroslav@1890: if (o == null) return false; jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: for (Node p = first; p != null; p = p.next) { jaroslav@1890: if (o.equals(p.item)) { jaroslav@1890: unlink(p); jaroslav@1890: return true; jaroslav@1890: } jaroslav@1890: } jaroslav@1890: return false; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: public boolean removeLastOccurrence(Object o) { jaroslav@1890: if (o == null) return false; jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: for (Node p = last; p != null; p = p.prev) { jaroslav@1890: if (o.equals(p.item)) { jaroslav@1890: unlink(p); jaroslav@1890: return true; jaroslav@1890: } jaroslav@1890: } jaroslav@1890: return false; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: // BlockingQueue methods jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Inserts the specified element at the end of this deque unless it would jaroslav@1890: * violate capacity restrictions. When using a capacity-restricted deque, jaroslav@1890: * it is generally preferable to use method {@link #offer(Object) offer}. jaroslav@1890: * jaroslav@1890: *

This method is equivalent to {@link #addLast}. jaroslav@1890: * jaroslav@1890: * @throws IllegalStateException if the element cannot be added at this jaroslav@1890: * time due to capacity restrictions jaroslav@1890: * @throws NullPointerException if the specified element is null jaroslav@1890: */ jaroslav@1890: public boolean add(E e) { jaroslav@1890: addLast(e); jaroslav@1890: return true; jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NullPointerException if the specified element is null jaroslav@1890: */ jaroslav@1890: public boolean offer(E e) { jaroslav@1890: return offerLast(e); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: * @throws InterruptedException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public void put(E e) throws InterruptedException { jaroslav@1890: putLast(e); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: * @throws InterruptedException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public boolean offer(E e, long timeout, TimeUnit unit) jaroslav@1890: throws InterruptedException { jaroslav@1890: return offerLast(e, timeout, unit); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Retrieves and removes the head of the queue represented by this deque. jaroslav@1890: * This method differs from {@link #poll poll} only in that it throws an jaroslav@1890: * exception if this deque is empty. jaroslav@1890: * jaroslav@1890: *

This method is equivalent to {@link #removeFirst() removeFirst}. jaroslav@1890: * jaroslav@1890: * @return the head of the queue represented by this deque jaroslav@1890: * @throws NoSuchElementException if this deque is empty jaroslav@1890: */ jaroslav@1890: public E remove() { jaroslav@1890: return removeFirst(); jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E poll() { jaroslav@1890: return pollFirst(); jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E take() throws InterruptedException { jaroslav@1890: return takeFirst(); jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E poll(long timeout, TimeUnit unit) throws InterruptedException { jaroslav@1890: return pollFirst(timeout, unit); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Retrieves, but does not remove, the head of the queue represented by jaroslav@1890: * this deque. This method differs from {@link #peek peek} only in that jaroslav@1890: * it throws an exception if this deque is empty. jaroslav@1890: * jaroslav@1890: *

This method is equivalent to {@link #getFirst() getFirst}. jaroslav@1890: * jaroslav@1890: * @return the head of the queue represented by this deque jaroslav@1890: * @throws NoSuchElementException if this deque is empty jaroslav@1890: */ jaroslav@1890: public E element() { jaroslav@1890: return getFirst(); jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E peek() { jaroslav@1890: return peekFirst(); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Returns the number of additional elements that this deque can ideally jaroslav@1890: * (in the absence of memory or resource constraints) accept without jaroslav@1890: * blocking. This is always equal to the initial capacity of this deque jaroslav@1890: * less the current {@code size} of this deque. jaroslav@1890: * jaroslav@1890: *

Note that you cannot always tell if an attempt to insert jaroslav@1890: * an element will succeed by inspecting {@code remainingCapacity} jaroslav@1890: * because it may be the case that another thread is about to jaroslav@1890: * insert or remove an element. jaroslav@1890: */ jaroslav@1890: public int remainingCapacity() { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: return capacity - count; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws UnsupportedOperationException {@inheritDoc} jaroslav@1890: * @throws ClassCastException {@inheritDoc} jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: * @throws IllegalArgumentException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public int drainTo(Collection c) { jaroslav@1890: return drainTo(c, Integer.MAX_VALUE); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws UnsupportedOperationException {@inheritDoc} jaroslav@1890: * @throws ClassCastException {@inheritDoc} jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: * @throws IllegalArgumentException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public int drainTo(Collection c, int maxElements) { jaroslav@1890: if (c == null) jaroslav@1890: throw new NullPointerException(); jaroslav@1890: if (c == this) jaroslav@1890: throw new IllegalArgumentException(); jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: int n = Math.min(maxElements, count); jaroslav@1890: for (int i = 0; i < n; i++) { jaroslav@1890: c.add(first.item); // In this order, in case add() throws. jaroslav@1890: unlinkFirst(); jaroslav@1890: } jaroslav@1890: return n; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: // Stack methods jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws IllegalStateException {@inheritDoc} jaroslav@1890: * @throws NullPointerException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public void push(E e) { jaroslav@1890: addFirst(e); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * @throws NoSuchElementException {@inheritDoc} jaroslav@1890: */ jaroslav@1890: public E pop() { jaroslav@1890: return removeFirst(); jaroslav@1890: } jaroslav@1890: jaroslav@1890: // Collection methods jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Removes the first occurrence of the specified element from this deque. jaroslav@1890: * If the deque does not contain the element, it is unchanged. jaroslav@1890: * More formally, removes the first element {@code e} such that jaroslav@1890: * {@code o.equals(e)} (if such an element exists). jaroslav@1890: * Returns {@code true} if this deque contained the specified element jaroslav@1890: * (or equivalently, if this deque changed as a result of the call). jaroslav@1890: * jaroslav@1890: *

This method is equivalent to jaroslav@1890: * {@link #removeFirstOccurrence(Object) removeFirstOccurrence}. jaroslav@1890: * jaroslav@1890: * @param o element to be removed from this deque, if present jaroslav@1890: * @return {@code true} if this deque changed as a result of the call jaroslav@1890: */ jaroslav@1890: public boolean remove(Object o) { jaroslav@1890: return removeFirstOccurrence(o); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Returns the number of elements in this deque. jaroslav@1890: * jaroslav@1890: * @return the number of elements in this deque jaroslav@1890: */ jaroslav@1890: public int size() { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: return count; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Returns {@code true} if this deque contains the specified element. jaroslav@1890: * More formally, returns {@code true} if and only if this deque contains jaroslav@1890: * at least one element {@code e} such that {@code o.equals(e)}. jaroslav@1890: * jaroslav@1890: * @param o object to be checked for containment in this deque jaroslav@1890: * @return {@code true} if this deque contains the specified element jaroslav@1890: */ jaroslav@1890: public boolean contains(Object o) { jaroslav@1890: if (o == null) return false; jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: for (Node p = first; p != null; p = p.next) jaroslav@1890: if (o.equals(p.item)) jaroslav@1890: return true; jaroslav@1890: return false; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /* jaroslav@1890: * TODO: Add support for more efficient bulk operations. jaroslav@1890: * jaroslav@1890: * We don't want to acquire the lock for every iteration, but we jaroslav@1890: * also want other threads a chance to interact with the jaroslav@1890: * collection, especially when count is close to capacity. jaroslav@1890: */ jaroslav@1890: jaroslav@1890: // /** jaroslav@1890: // * Adds all of the elements in the specified collection to this jaroslav@1890: // * queue. Attempts to addAll of a queue to itself result in jaroslav@1890: // * {@code IllegalArgumentException}. Further, the behavior of jaroslav@1890: // * this operation is undefined if the specified collection is jaroslav@1890: // * modified while the operation is in progress. jaroslav@1890: // * jaroslav@1890: // * @param c collection containing elements to be added to this queue jaroslav@1890: // * @return {@code true} if this queue changed as a result of the call jaroslav@1890: // * @throws ClassCastException {@inheritDoc} jaroslav@1890: // * @throws NullPointerException {@inheritDoc} jaroslav@1890: // * @throws IllegalArgumentException {@inheritDoc} jaroslav@1890: // * @throws IllegalStateException {@inheritDoc} jaroslav@1890: // * @see #add(Object) jaroslav@1890: // */ jaroslav@1890: // public boolean addAll(Collection c) { jaroslav@1890: // if (c == null) jaroslav@1890: // throw new NullPointerException(); jaroslav@1890: // if (c == this) jaroslav@1890: // throw new IllegalArgumentException(); jaroslav@1890: // final ReentrantLock lock = this.lock; jaroslav@1890: // lock.lock(); jaroslav@1890: // try { jaroslav@1890: // boolean modified = false; jaroslav@1890: // for (E e : c) jaroslav@1890: // if (linkLast(e)) jaroslav@1890: // modified = true; jaroslav@1890: // return modified; jaroslav@1890: // } finally { jaroslav@1890: // lock.unlock(); jaroslav@1890: // } jaroslav@1890: // } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Returns an array containing all of the elements in this deque, in jaroslav@1890: * proper sequence (from first to last element). jaroslav@1890: * jaroslav@1890: *

The returned array will be "safe" in that no references to it are jaroslav@1890: * maintained by this deque. (In other words, this method must allocate jaroslav@1890: * a new array). The caller is thus free to modify the returned array. jaroslav@1890: * jaroslav@1890: *

This method acts as bridge between array-based and collection-based jaroslav@1890: * APIs. jaroslav@1890: * jaroslav@1890: * @return an array containing all of the elements in this deque jaroslav@1890: */ jaroslav@1890: @SuppressWarnings("unchecked") jaroslav@1890: public Object[] toArray() { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: Object[] a = new Object[count]; jaroslav@1890: int k = 0; jaroslav@1890: for (Node p = first; p != null; p = p.next) jaroslav@1890: a[k++] = p.item; jaroslav@1890: return a; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Returns an array containing all of the elements in this deque, in jaroslav@1890: * proper sequence; the runtime type of the returned array is that of jaroslav@1890: * the specified array. If the deque fits in the specified array, it jaroslav@1890: * is returned therein. Otherwise, a new array is allocated with the jaroslav@1890: * runtime type of the specified array and the size of this deque. jaroslav@1890: * jaroslav@1890: *

If this deque fits in the specified array with room to spare jaroslav@1890: * (i.e., the array has more elements than this deque), the element in jaroslav@1890: * the array immediately following the end of the deque is set to jaroslav@1890: * {@code null}. jaroslav@1890: * jaroslav@1890: *

Like the {@link #toArray()} method, this method acts as bridge between jaroslav@1890: * array-based and collection-based APIs. Further, this method allows jaroslav@1890: * precise control over the runtime type of the output array, and may, jaroslav@1890: * under certain circumstances, be used to save allocation costs. jaroslav@1890: * jaroslav@1890: *

Suppose {@code x} is a deque known to contain only strings. jaroslav@1890: * The following code can be used to dump the deque into a newly jaroslav@1890: * allocated array of {@code String}: jaroslav@1890: * jaroslav@1890: *

jaroslav@1890:      *     String[] y = x.toArray(new String[0]);
jaroslav@1890: * jaroslav@1890: * Note that {@code toArray(new Object[0])} is identical in function to jaroslav@1890: * {@code toArray()}. jaroslav@1890: * jaroslav@1890: * @param a the array into which the elements of the deque are to jaroslav@1890: * be stored, if it is big enough; otherwise, a new array of the jaroslav@1890: * same runtime type is allocated for this purpose jaroslav@1890: * @return an array containing all of the elements in this deque jaroslav@1890: * @throws ArrayStoreException if the runtime type of the specified array jaroslav@1890: * is not a supertype of the runtime type of every element in jaroslav@1890: * this deque jaroslav@1890: * @throws NullPointerException if the specified array is null jaroslav@1890: */ jaroslav@1890: @SuppressWarnings("unchecked") jaroslav@1890: public T[] toArray(T[] a) { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: if (a.length < count) jaroslav@1890: a = (T[])java.lang.reflect.Array.newInstance jaroslav@1890: (a.getClass().getComponentType(), count); jaroslav@1890: jaroslav@1890: int k = 0; jaroslav@1890: for (Node p = first; p != null; p = p.next) jaroslav@1890: a[k++] = (T)p.item; jaroslav@1890: if (a.length > k) jaroslav@1890: a[k] = null; jaroslav@1890: return a; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: public String toString() { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: Node p = first; jaroslav@1890: if (p == null) jaroslav@1890: return "[]"; jaroslav@1890: jaroslav@1890: StringBuilder sb = new StringBuilder(); jaroslav@1890: sb.append('['); jaroslav@1890: for (;;) { jaroslav@1890: E e = p.item; jaroslav@1890: sb.append(e == this ? "(this Collection)" : e); jaroslav@1890: p = p.next; jaroslav@1890: if (p == null) jaroslav@1890: return sb.append(']').toString(); jaroslav@1890: sb.append(',').append(' '); jaroslav@1890: } jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Atomically removes all of the elements from this deque. jaroslav@1890: * The deque will be empty after this call returns. jaroslav@1890: */ jaroslav@1890: public void clear() { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: for (Node f = first; f != null; ) { jaroslav@1890: f.item = null; jaroslav@1890: Node n = f.next; jaroslav@1890: f.prev = null; jaroslav@1890: f.next = null; jaroslav@1890: f = n; jaroslav@1890: } jaroslav@1890: first = last = null; jaroslav@1890: count = 0; jaroslav@1890: notFull.signalAll(); jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Returns an iterator over the elements in this deque in proper sequence. jaroslav@1890: * The elements will be returned in order from first (head) to last (tail). jaroslav@1890: * jaroslav@1890: *

The returned iterator is a "weakly consistent" iterator that jaroslav@1890: * will never throw {@link java.util.ConcurrentModificationException jaroslav@1890: * ConcurrentModificationException}, and guarantees to traverse jaroslav@1890: * elements as they existed upon construction of the iterator, and jaroslav@1890: * may (but is not guaranteed to) reflect any modifications jaroslav@1890: * subsequent to construction. jaroslav@1890: * jaroslav@1890: * @return an iterator over the elements in this deque in proper sequence jaroslav@1890: */ jaroslav@1890: public Iterator iterator() { jaroslav@1890: return new Itr(); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Returns an iterator over the elements in this deque in reverse jaroslav@1890: * sequential order. The elements will be returned in order from jaroslav@1890: * last (tail) to first (head). jaroslav@1890: * jaroslav@1890: *

The returned iterator is a "weakly consistent" iterator that jaroslav@1890: * will never throw {@link java.util.ConcurrentModificationException jaroslav@1890: * ConcurrentModificationException}, and guarantees to traverse jaroslav@1890: * elements as they existed upon construction of the iterator, and jaroslav@1890: * may (but is not guaranteed to) reflect any modifications jaroslav@1890: * subsequent to construction. jaroslav@1890: * jaroslav@1890: * @return an iterator over the elements in this deque in reverse order jaroslav@1890: */ jaroslav@1890: public Iterator descendingIterator() { jaroslav@1890: return new DescendingItr(); jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Base class for Iterators for LinkedBlockingDeque jaroslav@1890: */ jaroslav@1890: private abstract class AbstractItr implements Iterator { jaroslav@1890: /** jaroslav@1890: * The next node to return in next() jaroslav@1890: */ jaroslav@1890: Node next; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * nextItem holds on to item fields because once we claim that jaroslav@1890: * an element exists in hasNext(), we must return item read jaroslav@1890: * under lock (in advance()) even if it was in the process of jaroslav@1890: * being removed when hasNext() was called. jaroslav@1890: */ jaroslav@1890: E nextItem; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Node returned by most recent call to next. Needed by remove. jaroslav@1890: * Reset to null if this element is deleted by a call to remove. jaroslav@1890: */ jaroslav@1890: private Node lastRet; jaroslav@1890: jaroslav@1890: abstract Node firstNode(); jaroslav@1890: abstract Node nextNode(Node n); jaroslav@1890: jaroslav@1890: AbstractItr() { jaroslav@1890: // set to initial position jaroslav@1890: final ReentrantLock lock = LinkedBlockingDeque.this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: next = firstNode(); jaroslav@1890: nextItem = (next == null) ? null : next.item; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Returns the successor node of the given non-null, but jaroslav@1890: * possibly previously deleted, node. jaroslav@1890: */ jaroslav@1890: private Node succ(Node n) { jaroslav@1890: // Chains of deleted nodes ending in null or self-links jaroslav@1890: // are possible if multiple interior nodes are removed. jaroslav@1890: for (;;) { jaroslav@1890: Node s = nextNode(n); jaroslav@1890: if (s == null) jaroslav@1890: return null; jaroslav@1890: else if (s.item != null) jaroslav@1890: return s; jaroslav@1890: else if (s == n) jaroslav@1890: return firstNode(); jaroslav@1890: else jaroslav@1890: n = s; jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Advances next. jaroslav@1890: */ jaroslav@1890: void advance() { jaroslav@1890: final ReentrantLock lock = LinkedBlockingDeque.this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: // assert next != null; jaroslav@1890: next = succ(next); jaroslav@1890: nextItem = (next == null) ? null : next.item; jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: public boolean hasNext() { jaroslav@1890: return next != null; jaroslav@1890: } jaroslav@1890: jaroslav@1890: public E next() { jaroslav@1890: if (next == null) jaroslav@1890: throw new NoSuchElementException(); jaroslav@1890: lastRet = next; jaroslav@1890: E x = nextItem; jaroslav@1890: advance(); jaroslav@1890: return x; jaroslav@1890: } jaroslav@1890: jaroslav@1890: public void remove() { jaroslav@1890: Node n = lastRet; jaroslav@1890: if (n == null) jaroslav@1890: throw new IllegalStateException(); jaroslav@1890: lastRet = null; jaroslav@1890: final ReentrantLock lock = LinkedBlockingDeque.this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: if (n.item != null) jaroslav@1890: unlink(n); jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** Forward iterator */ jaroslav@1890: private class Itr extends AbstractItr { jaroslav@1890: Node firstNode() { return first; } jaroslav@1890: Node nextNode(Node n) { return n.next; } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** Descending iterator */ jaroslav@1890: private class DescendingItr extends AbstractItr { jaroslav@1890: Node firstNode() { return last; } jaroslav@1890: Node nextNode(Node n) { return n.prev; } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Save the state of this deque to a stream (that is, serialize it). jaroslav@1890: * jaroslav@1890: * @serialData The capacity (int), followed by elements (each an jaroslav@1890: * {@code Object}) in the proper order, followed by a null jaroslav@1890: * @param s the stream jaroslav@1890: */ jaroslav@1890: private void writeObject(java.io.ObjectOutputStream s) jaroslav@1890: throws java.io.IOException { jaroslav@1890: final ReentrantLock lock = this.lock; jaroslav@1890: lock.lock(); jaroslav@1890: try { jaroslav@1890: // Write out capacity and any hidden stuff jaroslav@1890: s.defaultWriteObject(); jaroslav@1890: // Write out all elements in the proper order. jaroslav@1890: for (Node p = first; p != null; p = p.next) jaroslav@1890: s.writeObject(p.item); jaroslav@1890: // Use trailing null as sentinel jaroslav@1890: s.writeObject(null); jaroslav@1890: } finally { jaroslav@1890: lock.unlock(); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * Reconstitute this deque from a stream (that is, jaroslav@1890: * deserialize it). jaroslav@1890: * @param s the stream jaroslav@1890: */ jaroslav@1890: private void readObject(java.io.ObjectInputStream s) jaroslav@1890: throws java.io.IOException, ClassNotFoundException { jaroslav@1890: s.defaultReadObject(); jaroslav@1890: count = 0; jaroslav@1890: first = null; jaroslav@1890: last = null; jaroslav@1890: // Read in all elements and place in queue jaroslav@1890: for (;;) { jaroslav@1890: @SuppressWarnings("unchecked") jaroslav@1890: E item = (E)s.readObject(); jaroslav@1890: if (item == null) jaroslav@1890: break; jaroslav@1890: add(item); jaroslav@1890: } jaroslav@1890: } jaroslav@1890: jaroslav@1890: }