jdk/src/share/classes/java/util/concurrent/LinkedBlockingDeque.java
author martin
Wed, 29 Jul 2009 13:56:15 -0700
changeset 3419 3ae6dc68c20d
parent 3415 79309d6eab38
child 5506 202f599c92aa
permissions -rw-r--r--
6866554: Misc. javadoc warnings Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    37
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    38
import java.util.AbstractQueue;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    39
import java.util.Collection;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    40
import java.util.Iterator;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    41
import java.util.NoSuchElementException;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    42
import java.util.concurrent.locks.Condition;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    43
import java.util.concurrent.locks.ReentrantLock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * An optionally-bounded {@linkplain BlockingDeque blocking deque} based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * linked nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p> The optional capacity bound constructor argument serves as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * way to prevent excessive expansion. The capacity, if unspecified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * is equal to {@link Integer#MAX_VALUE}.  Linked nodes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * dynamically created upon each insertion unless this would bring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * deque above capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>Most operations run in constant time (ignoring time spent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * blocking).  Exceptions include {@link #remove(Object) remove},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * {@link #removeFirstOccurrence removeFirstOccurrence}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * #removeLastOccurrence removeLastOccurrence}, {@link #contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * contains}, {@link #iterator iterator.remove()}, and the bulk
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * operations, all of which run in linear time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>This class and its iterator implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <em>optional</em> methods of the {@link Collection} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Iterator} interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @author  Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
public class LinkedBlockingDeque<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    implements BlockingDeque<E>,  java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Implemented as a simple doubly-linked list protected by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * single lock and using conditions to manage blocking.
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    81
     *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    82
     * To implement weakly consistent iterators, it appears we need to
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    83
     * keep all Nodes GC-reachable from a predecessor dequeued Node.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    84
     * That would cause two problems:
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    85
     * - allow a rogue Iterator to cause unbounded memory retention
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    86
     * - cause cross-generational linking of old Nodes to new Nodes if
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    87
     *   a Node was tenured while live, which generational GCs have a
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    88
     *   hard time dealing with, causing repeated major collections.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    89
     * However, only non-deleted Nodes need to be reachable from
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    90
     * dequeued Nodes, and reachability does not necessarily have to
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    91
     * be of the kind understood by the GC.  We use the trick of
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    92
     * linking a Node that has just been dequeued to itself.  Such a
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    93
     * self-link implicitly means to jump to "first" (for next links)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
    94
     * or "last" (for prev links).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * We have "diamond" multiple interface/abstract class inheritance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * here, and that introduces ambiguities. Often we want the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * BlockingDeque javadoc combined with the AbstractQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * implementation, so a lot of method specs are duplicated here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final long serialVersionUID = -387911632671998426L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /** Doubly-linked list node class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static final class Node<E> {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   108
        /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   109
         * The item, or null if this node has been removed.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   110
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        E item;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   112
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   113
        /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   114
         * One of:
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   115
         * - the real predecessor Node
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   116
         * - this Node, meaning the predecessor is tail
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   117
         * - null, meaning there is no predecessor
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   118
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Node<E> prev;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   120
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   121
        /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   122
         * One of:
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   123
         * - the real successor Node
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   124
         * - this Node, meaning the successor is head
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   125
         * - null, meaning there is no successor
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   126
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        Node<E> next;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   128
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Node(E x, Node<E> p, Node<E> n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            item = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            prev = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            next = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   136
    /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   137
     * Pointer to first node.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   138
     * Invariant: (first == null && last == null) ||
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   139
     *            (first.prev == null && first.item != null)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   140
     */
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   141
    transient Node<E> first;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   142
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   143
    /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   144
     * Pointer to last node.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   145
     * Invariant: (first == null && last == null) ||
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   146
     *            (last.next == null && last.item != null)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   147
     */
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   148
    transient Node<E> last;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   149
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /** Number of items in the deque */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private transient int count;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   152
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /** Maximum number of items in the deque */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private final int capacity;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   155
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /** Main lock guarding all access */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   157
    final ReentrantLock lock = new ReentrantLock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   158
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /** Condition for waiting takes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private final Condition notEmpty = lock.newCondition();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   161
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /** Condition for waiting puts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private final Condition notFull = lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   166
     * Creates a {@code LinkedBlockingDeque} with a capacity of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * {@link Integer#MAX_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public LinkedBlockingDeque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        this(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   174
     * Creates a {@code LinkedBlockingDeque} with the given (fixed) capacity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param capacity the capacity of this deque
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   177
     * @throws IllegalArgumentException if {@code capacity} is less than 1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public LinkedBlockingDeque(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (capacity <= 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        this.capacity = capacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   185
     * Creates a {@code LinkedBlockingDeque} with a capacity of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * {@link Integer#MAX_VALUE}, initially containing the elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * the given collection, added in traversal order of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param c the collection of elements to initially contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public LinkedBlockingDeque(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this(Integer.MAX_VALUE);
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   196
        final ReentrantLock lock = this.lock;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   197
        lock.lock(); // Never contended, but necessary for visibility
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   198
        try {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   199
            for (E e : c) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   200
                if (e == null)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   201
                    throw new NullPointerException();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   202
                if (!linkLast(e))
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   203
                    throw new IllegalStateException("Deque full");
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   204
            }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   205
        } finally {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   206
            lock.unlock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   207
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    // Basic linking and unlinking operations, called only while holding lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Links e as first element, or returns false if full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private boolean linkFirst(E e) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   217
        // assert lock.isHeldByCurrentThread();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (count >= capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        Node<E> f = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        Node<E> x = new Node<E>(e, null, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        first = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (last == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            last = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            f.prev = x;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   227
        ++count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Links e as last element, or returns false if full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private boolean linkLast(E e) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   236
        // assert lock.isHeldByCurrentThread();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (count >= capacity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Node<E> l = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        Node<E> x = new Node<E>(e, l, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        last = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (first == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            first = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            l.next = x;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   246
        ++count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Removes and returns first element, or null if empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private E unlinkFirst() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   255
        // assert lock.isHeldByCurrentThread();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        Node<E> f = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (f == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        Node<E> n = f.next;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   260
        E item = f.item;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   261
        f.item = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   262
        f.next = f; // help GC
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        first = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (n == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            last = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            n.prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        --count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        notFull.signal();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   270
        return item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Removes and returns last element, or null if empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private E unlinkLast() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   277
        // assert lock.isHeldByCurrentThread();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        Node<E> l = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (l == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        Node<E> p = l.prev;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   282
        E item = l.item;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   283
        l.item = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   284
        l.prev = l; // help GC
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        last = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (p == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            first = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            p.next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        --count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        notFull.signal();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   292
        return item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   296
     * Unlinks x.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   298
    void unlink(Node<E> x) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   299
        // assert lock.isHeldByCurrentThread();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        Node<E> p = x.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        Node<E> n = x.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (p == null) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   303
            unlinkFirst();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } else if (n == null) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   305
            unlinkLast();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            p.next = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            n.prev = p;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   309
            x.item = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   310
            // Don't mess with x's links.  They may still be in use by
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   311
            // an iterator.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   312
            --count;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   313
            notFull.signal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    // BlockingDeque methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @throws IllegalStateException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @throws NullPointerException  {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public void addFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (!offerFirst(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new IllegalStateException("Deque full");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @throws IllegalStateException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @throws NullPointerException  {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public void addLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (!offerLast(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            throw new IllegalStateException("Deque full");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public boolean offerFirst(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (e == null) throw new NullPointerException();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   342
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            return linkFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public boolean offerLast(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (e == null) throw new NullPointerException();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   356
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return linkLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public void putFirst(E e) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (e == null) throw new NullPointerException();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   371
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            while (!linkFirst(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                notFull.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public void putLast(E e) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (e == null) throw new NullPointerException();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   387
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            while (!linkLast(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                notFull.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public boolean offerFirst(E e, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (e == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        long nanos = unit.toNanos(timeout);
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   405
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   408
            while (!linkFirst(e)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                nanos = notFull.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   413
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public boolean offerLast(E e, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        if (e == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        long nanos = unit.toNanos(timeout);
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   427
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   430
            while (!linkLast(e)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                nanos = notFull.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   435
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public E removeFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        E x = pollFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (x == null) throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public E removeLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        E x = pollLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (x == null) throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    public E pollFirst() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   460
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            return unlinkFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public E pollLast() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   470
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            return unlinkLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public E takeFirst() throws InterruptedException {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   480
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            E x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            while ( (x = unlinkFirst()) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                notEmpty.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public E takeLast() throws InterruptedException {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   493
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            E x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            while ( (x = unlinkLast()) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                notEmpty.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public E pollFirst(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        long nanos = unit.toNanos(timeout);
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   508
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   511
            E x;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   512
            while ( (x = unlinkFirst()) == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                nanos = notEmpty.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   517
            return x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public E pollLast(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        long nanos = unit.toNanos(timeout);
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   526
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   529
            E x;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   530
            while ( (x = unlinkLast()) == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                nanos = notEmpty.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   535
            return x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public E getFirst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        E x = peekFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (x == null) throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    public E getLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        E x = peekLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (x == null) throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public E peekFirst() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   560
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            return (first == null) ? null : first.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public E peekLast() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   570
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return (last == null) ? null : last.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    public boolean removeFirstOccurrence(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (o == null) return false;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   581
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            for (Node<E> p = first; p != null; p = p.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                if (o.equals(p.item)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    unlink(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public boolean removeLastOccurrence(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (o == null) return false;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   598
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            for (Node<E> p = last; p != null; p = p.prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                if (o.equals(p.item)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    unlink(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    // BlockingQueue methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * Inserts the specified element at the end of this deque unless it would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * violate capacity restrictions.  When using a capacity-restricted deque,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * it is generally preferable to use method {@link #offer(Object) offer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * <p>This method is equivalent to {@link #addLast}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @throws IllegalStateException if the element cannot be added at this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *         time due to capacity restrictions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        addLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return offerLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    public void put(E e) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        putLast(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public boolean offer(E e, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        return offerLast(e, timeout, unit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Retrieves and removes the head of the queue represented by this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * This method differs from {@link #poll poll} only in that it throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * exception if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <p>This method is equivalent to {@link #removeFirst() removeFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @return the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @throws NoSuchElementException if this deque is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    public E remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        return pollFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        return takeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return pollFirst(timeout, unit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Retrieves, but does not remove, the head of the queue represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * this deque.  This method differs from {@link #peek peek} only in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * it throws an exception if this deque is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * <p>This method is equivalent to {@link #getFirst() getFirst}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @return the head of the queue represented by this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @throws NoSuchElementException if this deque is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public E element() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        return getFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        return peekFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * Returns the number of additional elements that this deque can ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * (in the absence of memory or resource constraints) accept without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * blocking. This is always equal to the initial capacity of this deque
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   703
     * less the current {@code size} of this deque.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * <p>Note that you <em>cannot</em> always tell if an attempt to insert
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   706
     * an element will succeed by inspecting {@code remainingCapacity}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * because it may be the case that another thread is about to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * insert or remove an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    public int remainingCapacity() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   711
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            return capacity - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    public int drainTo(Collection<? super E> c) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   727
        return drainTo(c, Integer.MAX_VALUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    public int drainTo(Collection<? super E> c, int maxElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            throw new IllegalArgumentException();
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   741
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   744
            int n = Math.min(maxElements, count);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   745
            for (int i = 0; i < n; i++) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   746
                c.add(first.item);   // In this order, in case add() throws.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   747
                unlinkFirst();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    // Stack methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @throws IllegalStateException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @throws NullPointerException  {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    public void push(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        addFirst(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    public E pop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        return removeFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    // Collection methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Removes the first occurrence of the specified element from this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * If the deque does not contain the element, it is unchanged.
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   777
     * More formally, removes the first element {@code e} such that
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   778
     * {@code o.equals(e)} (if such an element exists).
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   779
     * Returns {@code true} if this deque contained the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * (or equivalently, if this deque changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * <p>This method is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * {@link #removeFirstOccurrence(Object) removeFirstOccurrence}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @param o element to be removed from this deque, if present
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   786
     * @return {@code true} if this deque changed as a result of the call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        return removeFirstOccurrence(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * Returns the number of elements in this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @return the number of elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    public int size() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   798
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   808
     * Returns {@code true} if this deque contains the specified element.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   809
     * More formally, returns {@code true} if and only if this deque contains
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   810
     * at least one element {@code e} such that {@code o.equals(e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @param o object to be checked for containment in this deque
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   813
     * @return {@code true} if this deque contains the specified element
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        if (o == null) return false;
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   817
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            for (Node<E> p = first; p != null; p = p.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                if (o.equals(p.item))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   829
    /*
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   830
     * TODO: Add support for more efficient bulk operations.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   831
     *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   832
     * We don't want to acquire the lock for every iteration, but we
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   833
     * also want other threads a chance to interact with the
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   834
     * collection, especially when count is close to capacity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   836
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   837
//     /**
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   838
//      * Adds all of the elements in the specified collection to this
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   839
//      * queue.  Attempts to addAll of a queue to itself result in
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   840
//      * {@code IllegalArgumentException}. Further, the behavior of
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   841
//      * this operation is undefined if the specified collection is
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   842
//      * modified while the operation is in progress.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   843
//      *
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   844
//      * @param c collection containing elements to be added to this queue
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   845
//      * @return {@code true} if this queue changed as a result of the call
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   846
//      * @throws ClassCastException            {@inheritDoc}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   847
//      * @throws NullPointerException          {@inheritDoc}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   848
//      * @throws IllegalArgumentException      {@inheritDoc}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   849
//      * @throws IllegalStateException         {@inheritDoc}
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   850
//      * @see #add(Object)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   851
//      */
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   852
//     public boolean addAll(Collection<? extends E> c) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   853
//         if (c == null)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   854
//             throw new NullPointerException();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   855
//         if (c == this)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   856
//             throw new IllegalArgumentException();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   857
//         final ReentrantLock lock = this.lock;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   858
//         lock.lock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   859
//         try {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   860
//             boolean modified = false;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   861
//             for (E e : c)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   862
//                 if (linkLast(e))
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   863
//                     modified = true;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   864
//             return modified;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   865
//         } finally {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   866
//             lock.unlock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   867
//         }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   868
//     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Returns an array containing all of the elements in this deque, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * proper sequence (from first to last element).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * maintained by this deque.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * @return an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   883
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    public Object[] toArray() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   885
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            Object[] a = new Object[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            for (Node<E> p = first; p != null; p = p.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                a[k++] = p.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * Returns an array containing all of the elements in this deque, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * proper sequence; the runtime type of the returned array is that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * the specified array.  If the deque fits in the specified array, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * is returned therein.  Otherwise, a new array is allocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * runtime type of the specified array and the size of this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * <p>If this deque fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * (i.e., the array has more elements than this deque), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * the array immediately following the end of the deque is set to
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   908
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     *
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   915
     * <p>Suppose {@code x} is a deque known to contain only strings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * The following code can be used to dump the deque into a newly
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   917
     * allocated array of {@code String}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *     String[] y = x.toArray(new String[0]);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   922
     * Note that {@code toArray(new Object[0])} is identical in function to
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   923
     * {@code toArray()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @param a the array into which the elements of the deque are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @return an array containing all of the elements in this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *         this deque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   934
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    public <T> T[] toArray(T[] a) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   936
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            if (a.length < count)
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   940
                a = (T[])java.lang.reflect.Array.newInstance
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   941
                    (a.getClass().getComponentType(), count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            for (Node<E> p = first; p != null; p = p.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                a[k++] = (T)p.item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            if (a.length > k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                a[k] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    public String toString() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   955
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            return super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * Atomically removes all of the elements from this deque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * The deque will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    public void clear() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   969
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   972
            for (Node<E> f = first; f != null; ) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   973
                f.item = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   974
                Node<E> n = f.next;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   975
                f.prev = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   976
                f.next = null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   977
                f = n;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   978
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            first = last = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            notFull.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * Returns an iterator over the elements in this deque in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * The elements will be returned in order from first (head) to last (tail).
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
   990
     * The returned {@code Iterator} is a "weakly consistent" iterator that
3419
3ae6dc68c20d 6866554: Misc. javadoc warnings
martin
parents: 3415
diff changeset
   991
     * will never throw {@link java.util.ConcurrentModificationException
3ae6dc68c20d 6866554: Misc. javadoc warnings
martin
parents: 3415
diff changeset
   992
     * ConcurrentModificationException},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * and guarantees to traverse elements as they existed upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * construction of the iterator, and may (but is not guaranteed to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * reflect any modifications subsequent to construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @return an iterator over the elements in this deque in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * Returns an iterator over the elements in this deque in reverse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * sequential order.  The elements will be returned in order from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * last (tail) to first (head).
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1007
     * The returned {@code Iterator} is a "weakly consistent" iterator that
3419
3ae6dc68c20d 6866554: Misc. javadoc warnings
martin
parents: 3415
diff changeset
  1008
     * will never throw {@link java.util.ConcurrentModificationException
3ae6dc68c20d 6866554: Misc. javadoc warnings
martin
parents: 3415
diff changeset
  1009
     * ConcurrentModificationException},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * and guarantees to traverse elements as they existed upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * construction of the iterator, and may (but is not guaranteed to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * reflect any modifications subsequent to construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    public Iterator<E> descendingIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        return new DescendingItr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * Base class for Iterators for LinkedBlockingDeque
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    private abstract class AbstractItr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1023
         * The next node to return in next()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
         Node<E> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
         * nextItem holds on to item fields because once we claim that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
         * an element exists in hasNext(), we must return item read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
         * under lock (in advance()) even if it was in the process of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
         * being removed when hasNext() was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        E nextItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
         * Node returned by most recent call to next. Needed by remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
         * Reset to null if this element is deleted by a call to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        private Node<E> lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1041
        abstract Node<E> firstNode();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1042
        abstract Node<E> nextNode(Node<E> n);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1043
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        AbstractItr() {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1045
            // set to initial position
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1046
            final ReentrantLock lock = LinkedBlockingDeque.this.lock;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1047
            lock.lock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1048
            try {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1049
                next = firstNode();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1050
                nextItem = (next == null) ? null : next.item;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1051
            } finally {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1052
                lock.unlock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1053
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        /**
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1057
         * Advances next.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
         */
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1059
        void advance() {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1060
            final ReentrantLock lock = LinkedBlockingDeque.this.lock;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1061
            lock.lock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1062
            try {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1063
                // assert next != null;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1064
                Node<E> s = nextNode(next);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1065
                if (s == next) {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1066
                    next = firstNode();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1067
                } else {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1068
                    // Skip over removed nodes.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1069
                    // May be necessary if multiple interior Nodes are removed.
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1070
                    while (s != null && s.item == null)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1071
                        s = nextNode(s);
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1072
                    next = s;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1073
                }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1074
                nextItem = (next == null) ? null : next.item;
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1075
            } finally {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1076
                lock.unlock();
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1077
            }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1078
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            return next != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            if (next == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            lastRet = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            E x = nextItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            advance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            Node<E> n = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            if (n == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            lastRet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            final ReentrantLock lock = LinkedBlockingDeque.this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            try {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1101
                if (n.item != null)
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1102
                    unlink(n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1109
    /** Forward iterator */
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1110
    private class Itr extends AbstractItr {
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1111
        Node<E> firstNode() { return first; }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1112
        Node<E> nextNode(Node<E> n) { return n.next; }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1113
    }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1114
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1115
    /** Descending iterator */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    private class DescendingItr extends AbstractItr {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1117
        Node<E> firstNode() { return last; }
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1118
        Node<E> nextNode(Node<E> n) { return n.prev; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * Save the state of this deque to a stream (that is, serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @serialData The capacity (int), followed by elements (each an
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1125
     * {@code Object}) in the proper order, followed by a null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * @param s the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        throws java.io.IOException {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1130
        final ReentrantLock lock = this.lock;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            // Write out capacity and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            // Write out all elements in the proper order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            for (Node<E> p = first; p != null; p = p.next)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                s.writeObject(p.item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            // Use trailing null as sentinel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * Reconstitute this deque from a stream (that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * @param s the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        first = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        last = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        // Read in all elements and place in queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        for (;;) {
3415
79309d6eab38 6805775: LinkedBlockingQueue Nodes should unlink themselves before becoming garbage
dl
parents: 2
diff changeset
  1158
            @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            E item = (E)s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            add(item);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
}