jdk/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7976 f273c0d04215
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * An unbounded {@linkplain BlockingQueue blocking queue} that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the same ordering rules as class {@link PriorityQueue} and supplies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * blocking retrieval operations.  While this queue is logically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * unbounded, attempted additions may fail due to resource exhaustion
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * (causing <tt>OutOfMemoryError</tt>). This class does not permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <tt>null</tt> elements.  A priority queue relying on {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Comparable natural ordering} also does not permit insertion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * non-comparable objects (doing so results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <tt>ClassCastException</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>This class and its iterator implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <em>optional</em> methods of the {@link Collection} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Iterator} interfaces.  The Iterator provided in method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * #iterator()} is <em>not</em> guaranteed to traverse the elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * the PriorityBlockingQueue in any particular order. If you need
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * ordered traversal, consider using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <tt>Arrays.sort(pq.toArray())</tt>.  Also, method <tt>drainTo</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * can be used to <em>remove</em> some or all elements in priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * order and place them in another collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>Operations on this class make no guarantees about the ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * of elements with equal priority. If you need to enforce an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * ordering, you can define custom classes or comparators that use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * secondary key to break ties in primary priority values.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * example, here is a class that applies first-in-first-out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * tie-breaking to comparable elements. To use it, you would insert a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <tt>new FIFOEntry(anEntry)</tt> instead of a plain entry object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * class FIFOEntry&lt;E extends Comparable&lt;? super E&gt;&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *     implements Comparable&lt;FIFOEntry&lt;E&gt;&gt; {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *   final static AtomicLong seq = new AtomicLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *   final long seqNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   final E entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *   public FIFOEntry(E entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     seqNum = seq.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     this.entry = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   public E getEntry() { return entry; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *   public int compareTo(FIFOEntry&lt;E&gt; other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     int res = entry.compareTo(other.entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     if (res == 0 &amp;&amp; other.entry != this.entry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *       res = (seqNum &lt; other.seqNum ? -1 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
public class PriorityBlockingQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    implements BlockingQueue<E>, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static final long serialVersionUID = 5595510919245408276L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private final PriorityQueue<E> q;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private final ReentrantLock lock = new ReentrantLock(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private final Condition notEmpty = lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Creates a <tt>PriorityBlockingQueue</tt> with the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * initial capacity (11) that orders its elements according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * their {@linkplain Comparable natural ordering}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public PriorityBlockingQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        q = new PriorityQueue<E>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Creates a <tt>PriorityBlockingQueue</tt> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * initial capacity that orders its elements according to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * {@linkplain Comparable natural ordering}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param initialCapacity the initial capacity for this priority queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *         than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public PriorityBlockingQueue(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        q = new PriorityQueue<E>(initialCapacity, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Creates a <tt>PriorityBlockingQueue</tt> with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * capacity that orders its elements according to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param initialCapacity the initial capacity for this priority queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param  comparator the comparator that will be used to order this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *         priority queue.  If {@code null}, the {@linkplain Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *         natural ordering} of the elements will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *         than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public PriorityBlockingQueue(int initialCapacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                 Comparator<? super E> comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        q = new PriorityQueue<E>(initialCapacity, comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Creates a <tt>PriorityBlockingQueue</tt> containing the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * in the specified collection.  If the specified collection is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * {@link SortedSet} or a {@link PriorityQueue},  this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * priority queue will be ordered according to the same ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Otherwise, this priority queue will be ordered according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * {@linkplain Comparable natural ordering} of its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param  c the collection whose elements are to be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *         into this priority queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws ClassCastException if elements of the specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *         cannot be compared to one another according to the priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *         queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public PriorityBlockingQueue(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        q = new PriorityQueue<E>(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Inserts the specified element into this priority queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return <tt>true</tt> (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Inserts the specified element into this priority queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @return <tt>true</tt> (as specified by {@link Queue#offer})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            boolean ok = q.offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            assert ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            notEmpty.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Inserts the specified element into this priority queue. As the queue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * unbounded this method will never block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public void put(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        offer(e); // never need to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Inserts the specified element into this priority queue. As the queue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * unbounded this method will never block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param timeout This parameter is ignored as the method never blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param unit This parameter is ignored as the method never blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @return <tt>true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @throws ClassCastException if the specified element cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *         with elements currently in the priority queue according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *         priority queue's ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public boolean offer(E e, long timeout, TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return offer(e); // never need to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return q.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                while (q.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    notEmpty.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                notEmpty.signal(); // propagate to non-interrupted thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                throw ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            E x = q.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            assert x != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                E x = q.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                if (x != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    nanos = notEmpty.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    notEmpty.signal(); // propagate to non-interrupted thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    throw ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return q.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Returns the comparator used to order the elements in this queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * or <tt>null</tt> if this queue uses the {@linkplain Comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * natural ordering} of its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return the comparator used to order the elements in this queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *         or <tt>null</tt> if this queue uses the natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *         ordering of its elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public Comparator<? super E> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return q.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return q.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            lock.unlock();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Always returns <tt>Integer.MAX_VALUE</tt> because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * a <tt>PriorityBlockingQueue</tt> is not capacity constrained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return <tt>Integer.MAX_VALUE</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public int remainingCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Removes a single instance of the specified element from this queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * if it is present.  More formally, removes an element {@code e} such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * that {@code o.equals(e)}, if this queue contains one or more such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * elements.  Returns {@code true} if and only if this queue contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * the specified element (or equivalently, if this queue changed as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param o element to be removed from this queue, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return <tt>true</tt> if this queue changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return q.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns {@code true} if this queue contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * More formally, returns {@code true} if and only if this queue contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * at least one element {@code e} such that {@code o.equals(e)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param o object to be checked for containment in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @return <tt>true</tt> if this queue contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        final ReentrantLock lock = this.lock;
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 q.contains(o);
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
     * Returns an array containing all of the elements in this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * The returned array elements are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return q.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return q.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public int drainTo(Collection<? super E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            E e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            while ( (e = q.poll()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                c.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public int drainTo(Collection<? super E> c, int maxElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (maxElements <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            E e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            while (n < maxElements && (e = q.poll()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                c.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * Atomically removes all of the elements from this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Returns an array containing all of the elements in this queue; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * runtime type of the returned array is that of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * The returned array elements are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * If the queue fits in the specified array, it is returned therein.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Otherwise, a new array is allocated with the runtime type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * the array immediately following the end of the queue is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <p>Suppose <tt>x</tt> is a queue known to contain only strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * The following code can be used to dump the queue into a newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * allocated array of <tt>String</tt>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *     String[] y = x.toArray(new String[0]);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * <tt>toArray()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            return q.toArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Returns an iterator over the elements in this queue. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * iterator does not return the elements in any particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * The returned <tt>Iterator</tt> is a "weakly consistent"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * iterator that will never throw {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * ConcurrentModificationException}, and guarantees to traverse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * elements as they existed upon construction of the iterator, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * may (but is not guaranteed to) reflect any modifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * subsequent to construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @return an iterator over the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return new Itr(toArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Snapshot iterator that works off copy of underlying q array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        final Object[] array; // Array of all elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        int cursor;           // index of next element to return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        int lastRet;          // index of last element, or -1 if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        Itr(Object[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            this.array = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            return cursor < array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (cursor >= array.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            lastRet = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return (E)array[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            Object x = array[lastRet];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            // Traverse underlying queue to find == element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            // not just a .equals element.
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
                for (Iterator it = q.iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    if (it.next() == x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Saves the state to a stream (that is, serializes it).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * merely wraps default serialization within lock.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * serialization strategy for items is left to underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Queue. Note that locking is not needed on deserialization, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * readObject is not defined, just relying on default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
}