jdk/src/share/classes/java/util/concurrent/ArrayBlockingQueue.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4110 ac033ba6ede4
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: 4110
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: 4110
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: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
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
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A bounded {@linkplain BlockingQueue blocking queue} backed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * array.  This queue orders elements FIFO (first-in-first-out).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <em>head</em> of the queue is that element that has been on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * queue the longest time.  The <em>tail</em> of the queue is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * element that has been on the queue the shortest time. New elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * are inserted at the tail of the queue, and the queue retrieval
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * operations obtain elements at the head of the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>This is a classic &quot;bounded buffer&quot;, in which a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * fixed-sized array holds elements inserted by producers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * extracted by consumers.  Once created, the capacity cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * increased.  Attempts to <tt>put</tt> an element into a full queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * will result in the operation blocking; attempts to <tt>take</tt> an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * element from an empty queue will similarly block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p> This class supports an optional fairness policy for ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * waiting producer and consumer threads.  By default, this ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * is not guaranteed. However, a queue constructed with fairness set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * to <tt>true</tt> grants threads access in FIFO order. Fairness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * generally decreases throughput but reduces variability and avoids
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * starvation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>This class and its iterator implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <em>optional</em> methods of the {@link Collection} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Iterator} interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public class ArrayBlockingQueue<E> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        implements BlockingQueue<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
     * Serialization ID. This class relies on default serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * even for the items array, which is default-serialized, even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * it is empty. Otherwise it could not be declared final, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * necessary here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final long serialVersionUID = -817911632652898426L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /** The queued items  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private final E[] items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** items index for next take, poll or remove */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private int takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /** items index for next put, offer, or add. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private int putIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /** Number of items in the queue */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Concurrency control uses the classic two-condition algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * found in any textbook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /** Main lock guarding all access */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private final ReentrantLock lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /** Condition for waiting takes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private final Condition notEmpty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /** Condition for waiting puts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private final Condition notFull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // Internal helper methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Circularly increment i.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    final int inc(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return (++i == items.length)? 0 : i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Inserts element at current put position, advances, and signals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private void insert(E x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        items[putIndex] = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        putIndex = inc(putIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        ++count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        notEmpty.signal();
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
     * Extracts element at current take position, advances, and signals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private E extract() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        final E[] items = this.items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        E x = items[takeIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        items[takeIndex] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        takeIndex = inc(takeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        --count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Utility for remove and iterator.remove: Delete item at position i.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Call only when holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    void removeAt(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        final E[] items = this.items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // if removing front item, just advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (i == takeIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            items[takeIndex] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            takeIndex = inc(takeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // slide over all others up through putIndex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                int nexti = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (nexti != putIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    items[i] = items[nexti];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    i = nexti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    items[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    putIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    break;
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
        --count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        notFull.signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Creates an <tt>ArrayBlockingQueue</tt> with the given (fixed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * capacity and default access policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param capacity the capacity of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @throws IllegalArgumentException if <tt>capacity</tt> is less than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public ArrayBlockingQueue(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this(capacity, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Creates an <tt>ArrayBlockingQueue</tt> with the given (fixed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * capacity and the specified access policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param capacity the capacity of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param fair if <tt>true</tt> then queue accesses for threads blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *        on insertion or removal, are processed in FIFO order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *        if <tt>false</tt> the access order is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @throws IllegalArgumentException if <tt>capacity</tt> is less than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public ArrayBlockingQueue(int capacity, boolean fair) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (capacity <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.items = (E[]) new Object[capacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        lock = new ReentrantLock(fair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        notEmpty = lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        notFull =  lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Creates an <tt>ArrayBlockingQueue</tt> with the given (fixed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * capacity, the specified access policy and initially containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * elements of the given collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * added in traversal order of the collection's iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param capacity the capacity of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param fair if <tt>true</tt> then queue accesses for threads blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *        on insertion or removal, are processed in FIFO order;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *        if <tt>false</tt> the access order is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param c the collection of elements to initially contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @throws IllegalArgumentException if <tt>capacity</tt> is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *         <tt>c.size()</tt>, or less than 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public ArrayBlockingQueue(int capacity, boolean fair,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                              Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        this(capacity, fair);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (capacity < c.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   221
        for (E e : c)
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   222
            add(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Inserts the specified element at the tail of this queue if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * possible to do so immediately without exceeding the queue's capacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * returning <tt>true</tt> upon success and throwing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <tt>IllegalStateException</tt> if this queue is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return <tt>true</tt> (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @throws IllegalStateException if this queue is full
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return super.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Inserts the specified element at the tail of this queue if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * possible to do so immediately without exceeding the queue's capacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * returning <tt>true</tt> upon success and <tt>false</tt> if this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * is full.  This method is generally preferable to method {@link #add},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * which can fail to insert an element only by throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (e == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (count == items.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                insert(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Inserts the specified element at the tail of this queue, waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * for space to become available if the queue is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public void put(E e) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (e == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        final E[] items = this.items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                while (count == items.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    notFull.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                notFull.signal(); // propagate to non-interrupted thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                throw ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            insert(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Inserts the specified element at the tail of this queue, waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * up to the specified wait time for space to become available if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * the queue is full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public boolean offer(E e, long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (e == null) throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (count != items.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    insert(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    nanos = notFull.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    notFull.signal(); // propagate to non-interrupted thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    throw ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (count == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            E x = extract();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                while (count == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    notEmpty.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                notEmpty.signal(); // propagate to non-interrupted thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                throw ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            E x = extract();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                if (count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    E x = extract();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    nanos = notEmpty.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    notEmpty.signal(); // propagate to non-interrupted thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    throw ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            lock.unlock();
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
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return (count == 0) ? null : items[takeIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    // this doc comment is overridden to remove the reference to collections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    // greater in size than Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Returns the number of elements in this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @return the number of elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    // this doc comment is a modified copy of the inherited doc comment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    // without the reference to unlimited queues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Returns the number of additional elements that this queue can ideally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * (in the absence of memory or resource constraints) accept without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * blocking. This is always equal to the initial capacity of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * less the current <tt>size</tt> of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <p>Note that you <em>cannot</em> always tell if an attempt to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * an element will succeed by inspecting <tt>remainingCapacity</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * because it may be the case that another thread is about to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * insert or remove an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public int remainingCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return items.length - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Removes a single instance of the specified element from this queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * if it is present.  More formally, removes an element <tt>e</tt> such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * that <tt>o.equals(e)</tt>, if this queue contains one or more such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Returns <tt>true</tt> if this queue contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * (or equivalently, if this queue changed as a result of the call).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param o element to be removed from this queue, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @return <tt>true</tt> if this queue changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (o == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        final E[] items = this.items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            int i = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (k++ >= count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                if (o.equals(items[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    removeAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                i = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
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
     * Returns <tt>true</tt> if this queue contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * More formally, returns <tt>true</tt> if and only if this queue contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param o object to be checked for containment in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @return <tt>true</tt> if this queue contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (o == null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        final E[] items = this.items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            int i = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            while (k++ < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                if (o.equals(items[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                i = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        final E[] items = this.items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            Object[] a = new Object[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            int i = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            while (k < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                a[k++] = items[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                i = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Returns an array containing all of the elements in this queue, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * proper sequence; the runtime type of the returned array is that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * the specified array.  If the queue fits in the specified array, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * is returned therein.  Otherwise, a new array is allocated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * runtime type of the specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * the array immediately following the end of the queue is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * <p>Suppose <tt>x</tt> is a queue known to contain only strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * The following code can be used to dump the queue into a newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * allocated array of <tt>String</tt>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *     String[] y = x.toArray(new String[0]);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * <tt>toArray()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        final E[] items = this.items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            if (a.length < count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                a = (T[])java.lang.reflect.Array.newInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    a.getClass().getComponentType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            int k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            int i = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            while (k < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                a[k++] = (T)items[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                i = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (a.length > count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                a[count] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
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
     * Atomically removes all of the elements from this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        final E[] items = this.items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            int i = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            int k = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            while (k-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                items[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                i = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            putIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            takeIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            notFull.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    public int drainTo(Collection<? super E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        final E[] items = this.items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            int i = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            int max = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            while (n < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                c.add(items[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                items[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                i = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                putIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                takeIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                notFull.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
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
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public int drainTo(Collection<? super E> c, int maxElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (maxElements <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        final E[] items = this.items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            int i = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            int sz = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            int max = (maxElements < count)? maxElements : count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            while (n < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                c.add(items[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                items[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                i = inc(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                count -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                takeIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                notFull.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * Returns an iterator over the elements in this queue in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * The returned <tt>Iterator</tt> is a "weakly consistent" iterator that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * will never throw {@link ConcurrentModificationException},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * and guarantees to traverse elements as they existed upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * construction of the iterator, and may (but is not guaranteed to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * reflect any modifications subsequent to construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @return an iterator over the elements in this queue in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Iterator for ArrayBlockingQueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
         * Index of element to be returned by next,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
         * or a negative number if no such.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        private int nextIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         * nextItem holds on to item fields because once we claim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         * that an element exists in hasNext(), we must return it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
         * the following next() call even if it was in the process of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
         * being removed when hasNext() was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        private E nextItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
         * Index of element returned by most recent call to next.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         * Reset to -1 if this element is deleted by a call to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        private int lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        Itr() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            if (count == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                nextIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                nextIndex = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                nextItem = items[takeIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
             * No sync. We can return true by mistake here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
             * only if this iterator passed across threads,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
             * which we don't support anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            return nextIndex >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         * Checks whether nextIndex is valid; if so setting nextItem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         * Stops iterator when either hits putIndex or sees null item.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        private void checkNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            if (nextIndex == putIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                nextIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                nextItem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                nextItem = items[nextIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                if (nextItem == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    nextIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                if (nextIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                lastRet = nextIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                E x = nextItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                nextIndex = inc(nextIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                checkNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            final ReentrantLock lock = ArrayBlockingQueue.this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                int i = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                if (i == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                int ti = takeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                removeAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                // back up cursor (reset to front if was first element)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                nextIndex = (i == ti) ? takeIndex : i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                checkNext();
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
}