jdk/src/share/classes/java/util/concurrent/DelayQueue.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 60 429af02854d2
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
package java.util.concurrent;
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} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <tt>Delayed</tt> elements, in which an element can only be taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * when its delay has expired.  The <em>head</em> of the queue is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <tt>Delayed</tt> element whose delay expired furthest in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * past.  If no delay has expired there is no head and <tt>poll</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * will return <tt>null</tt>. Expiration occurs when an element's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <tt>getDelay(TimeUnit.NANOSECONDS)</tt> method returns a value less
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * than or equal to zero.  Even though unexpired elements cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * removed using <tt>take</tt> or <tt>poll</tt>, they are otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * treated as normal elements. For example, the <tt>size</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * returns the count of both expired and unexpired elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * This queue does not permit null elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>This class and its iterator implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <em>optional</em> methods of the {@link Collection} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Iterator} interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @param <E> the type of elements held in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public class DelayQueue<E extends Delayed> extends AbstractQueue<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    implements BlockingQueue<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private transient final ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private transient final Condition available = lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final PriorityQueue<E> q = new PriorityQueue<E>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Creates a new <tt>DelayQueue</tt> that is initially empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public DelayQueue() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Creates a <tt>DelayQueue</tt> initially containing the elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * given collection of {@link Delayed} instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param c the collection of elements to initially contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @throws NullPointerException if the specified collection or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *         of its elements are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public DelayQueue(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Inserts the specified element into this delay queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @return <tt>true</tt> (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Inserts the specified element into this delay queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return <tt>true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws NullPointerException if the specified element is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public boolean offer(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            E first = q.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            q.offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (first == null || e.compareTo(first) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                available.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Inserts the specified element into this delay queue. As the queue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * unbounded this method will never block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public void put(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Inserts the specified element into this delay queue. As the queue is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * unbounded this method will never block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param e the element to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param timeout This parameter is ignored as the method never blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param unit This parameter is ignored as the method never blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return <tt>true</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public boolean offer(E e, long timeout, TimeUnit unit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return offer(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Retrieves and removes the head of this queue, or returns <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * if this queue has no elements with an expired delay.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return the head of this queue, or <tt>null</tt> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         queue has no elements with an expired delay
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public E poll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            E first = q.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                E x = q.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                assert x != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                if (q.size() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    available.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Retrieves and removes the head of this queue, waiting if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * until an element with an expired delay is available on this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return the head of this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public E take() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                E first = q.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (first == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    available.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    long delay =  first.getDelay(TimeUnit.NANOSECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    if (delay > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        long tl = available.awaitNanos(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        E x = q.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        assert x != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        if (q.size() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                            available.signalAll(); // wake up other takers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Retrieves and removes the head of this queue, waiting if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * until an element with an expired delay is available on this queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * or the specified wait time expires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return the head of this queue, or <tt>null</tt> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *         specified waiting time elapses before an element with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *         an expired delay becomes available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @throws InterruptedException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        long nanos = unit.toNanos(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        lock.lockInterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                E first = q.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                if (first == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        nanos = available.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    long delay = first.getDelay(TimeUnit.NANOSECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    if (delay > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        if (nanos <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        if (delay > nanos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                            delay = nanos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        long timeLeft = available.awaitNanos(delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                        nanos -= delay - timeLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        E x = q.poll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        assert x != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        if (q.size() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                            available.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Retrieves, but does not remove, the head of this queue, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * returns <tt>null</tt> if this queue is empty.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <tt>poll</tt>, if no expired elements are available in the queue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * this method returns the element that will expire next,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * if one exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return the head of this queue, or <tt>null</tt> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *         queue is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public E peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return q.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return q.size();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public int drainTo(Collection<? super E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                E first = q.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                c.add(q.poll());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                available.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public int drainTo(Collection<? super E> c, int maxElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (c == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (maxElements <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return 0;
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
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            while (n < maxElements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                E first = q.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (first == null || first.getDelay(TimeUnit.NANOSECONDS) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                c.add(q.poll());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                ++n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                available.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
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
     * Atomically removes all of the elements from this delay queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * The queue will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Elements with an unexpired delay are not waited for; they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * simply discarded from the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            q.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Always returns <tt>Integer.MAX_VALUE</tt> because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * a <tt>DelayQueue</tt> is not capacity constrained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @return <tt>Integer.MAX_VALUE</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public int remainingCapacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Returns an array containing all of the elements in this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * The returned array elements are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <p>The returned array will be "safe" in that no references to it are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * maintained by this queue.  (In other words, this method must allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * a new array).  The caller is thus free to modify the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <p>This method acts as bridge between array-based and collection-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return q.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Returns an array containing all of the elements in this queue; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * runtime type of the returned array is that of the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * The returned array elements are in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * If the queue fits in the specified array, it is returned therein.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Otherwise, a new array is allocated with the runtime type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * specified array and the size of this queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <p>If this queue fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * (i.e., the array has more elements than this queue), the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * the array immediately following the end of the queue is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <p>Like the {@link #toArray()} method, this method acts as bridge between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * array-based and collection-based APIs.  Further, this method allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * precise control over the runtime type of the output array, and may,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * under certain circumstances, be used to save allocation costs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * <p>The following code can be used to dump a delay queue into a newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * allocated array of <tt>Delayed</tt>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *     Delayed[] a = q.toArray(new Delayed[0]);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <tt>toArray()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param a the array into which the elements of the queue are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *          same runtime type is allocated for this purpose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @return an array containing all of the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @throws ArrayStoreException if the runtime type of the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *         is not a supertype of the runtime type of every element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *         this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @throws NullPointerException if the specified array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            return q.toArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Removes a single instance of the specified element from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * queue, if it is present, whether or not it has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public boolean remove(Object o) {
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
            return q.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Returns an iterator over all the elements (both expired and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * unexpired) in this queue. The iterator does not return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * elements in any particular order.  The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * <tt>Iterator</tt> is a "weakly consistent" iterator that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * never throw {@link ConcurrentModificationException}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * guarantees to traverse elements as they existed upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * construction of the iterator, and may (but is not guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * to) reflect any modifications subsequent to construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @return an iterator over the elements in this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return new Itr(toArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Snapshot iterator that works off copy of underlying q array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        final Object[] array; // Array of all elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        int cursor;           // index of next element to return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        int lastRet;          // index of last element, or -1 if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        Itr(Object[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            this.array = array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            return cursor < array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (cursor >= array.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            lastRet = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            return (E)array[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            if (lastRet < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            Object x = array[lastRet];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            // Traverse underlying queue to find == element,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            // not just a .equals element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                for (Iterator it = q.iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    if (it.next() == x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                lock.unlock();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
}