0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * This file is available under and governed by the GNU General Public 0N/A * License version 2 only, as published by the Free Software Foundation. 0N/A * However, the following notice accompanied the original version of this 0N/A * Written by Doug Lea with assistance from members of JCP JSR-166 0N/A * Expert Group and released to the public domain, as explained at 0N/A * A bounded {@linkplain BlockingQueue blocking queue} backed by an 0N/A * array. This queue orders elements FIFO (first-in-first-out). The 0N/A * <em>head</em> of the queue is that element that has been on the 0N/A * queue the longest time. The <em>tail</em> of the queue is that 0N/A * element that has been on the queue the shortest time. New elements 0N/A * are inserted at the tail of the queue, and the queue retrieval 0N/A * operations obtain elements at the head of the queue. 0N/A * <p>This is a classic "bounded buffer", in which a 0N/A * fixed-sized array holds elements inserted by producers and 0N/A * extracted by consumers. Once created, the capacity cannot be 3387N/A * changed. Attempts to {@code put} an element into a full queue 3387N/A * will result in the operation blocking; attempts to {@code take} an 0N/A * element from an empty queue will similarly block. 3387N/A * <p>This class supports an optional fairness policy for ordering 0N/A * waiting producer and consumer threads. By default, this ordering 0N/A * is not guaranteed. However, a queue constructed with fairness set 3387N/A * to {@code true} grants threads access in FIFO order. Fairness 0N/A * generally decreases throughput but reduces variability and avoids 0N/A * <p>This class and its iterator implement all of the 0N/A * <em>optional</em> methods of the {@link Collection} and {@link 0N/A * Iterator} interfaces. 0N/A * <p>This class is a member of the 0N/A * Java Collections Framework</a>. 0N/A * @param <E> the type of elements held in this collection 0N/A * Serialization ID. This class relies on default serialization 0N/A * even for the items array, which is default-serialized, even if 0N/A * it is empty. Otherwise it could not be declared final, which is 3387N/A /** items index for next take, poll, peek or remove */ 3387N/A /** items index for next put, offer, or add */ 3387N/A /** Number of elements in the queue */ 0N/A * Concurrency control uses the classic two-condition algorithm 0N/A * found in any textbook. 0N/A /** Main lock guarding all access */ 0N/A /** Condition for waiting takes */ 0N/A /** Condition for waiting puts */ 0N/A // Internal helper methods 0N/A * Circularly increment i. 3387N/A * Throws NullPointerException if argument is null. 0N/A * Inserts element at current put position, advances, and signals. 0N/A * Call only when holding lock. 0N/A * Extracts element at current take position, advances, and signals. 0N/A * Call only when holding lock. 3387N/A * Deletes item at position i. 3387N/A * Utility for remove and iterator.remove. 0N/A * Call only when holding lock. 0N/A // if removing front item, just advance 0N/A // slide over all others up through putIndex. 3387N/A * Creates an {@code ArrayBlockingQueue} with the given (fixed) 0N/A * capacity and default access policy. 0N/A * @param capacity the capacity of this queue 3387N/A * @throws IllegalArgumentException if {@code capacity < 1} 3387N/A * Creates an {@code ArrayBlockingQueue} with the given (fixed) 0N/A * capacity and the specified access policy. 0N/A * @param capacity the capacity of this queue 3387N/A * @param fair if {@code true} then queue accesses for threads blocked 0N/A * on insertion or removal, are processed in FIFO order; 3387N/A * if {@code false} the access order is unspecified. 3387N/A * @throws IllegalArgumentException if {@code capacity < 1} 3387N/A * Creates an {@code ArrayBlockingQueue} with the given (fixed) 0N/A * capacity, the specified access policy and initially containing the 0N/A * elements of the given collection, 0N/A * added in traversal order of the collection's iterator. 0N/A * @param capacity the capacity of this queue 3387N/A * @param fair if {@code true} then queue accesses for threads blocked 0N/A * on insertion or removal, are processed in FIFO order; 3387N/A * if {@code false} the access order is unspecified. 0N/A * @param c the collection of elements to initially contain 3387N/A * @throws IllegalArgumentException if {@code capacity} is less than 3387N/A * {@code c.size()}, or less than 1. 0N/A * @throws NullPointerException if the specified collection or any 0N/A * of its elements are null 0N/A * Inserts the specified element at the tail of this queue if it is 0N/A * possible to do so immediately without exceeding the queue's capacity, 3387N/A * returning {@code true} upon success and throwing an 3387N/A * {@code IllegalStateException} if this queue is full. 0N/A * @param e the element to add 3387N/A * @return {@code true} (as specified by {@link Collection#add}) 0N/A * @throws IllegalStateException if this queue is full 0N/A * @throws NullPointerException if the specified element is null 0N/A * Inserts the specified element at the tail of this queue if it is 0N/A * possible to do so immediately without exceeding the queue's capacity, 3387N/A * returning {@code true} upon success and {@code false} if this queue 0N/A * is full. This method is generally preferable to method {@link #add}, 0N/A * which can fail to insert an element only by throwing an exception. 0N/A * @throws NullPointerException if the specified element is null 0N/A * Inserts the specified element at the tail of this queue, waiting 0N/A * for space to become available if the queue is full. 0N/A * @throws InterruptedException {@inheritDoc} 0N/A * @throws NullPointerException {@inheritDoc} 0N/A * Inserts the specified element at the tail of this queue, waiting 0N/A * up to the specified wait time for space to become available if 0N/A * the queue is full. 0N/A * @throws InterruptedException {@inheritDoc} 0N/A * @throws NullPointerException {@inheritDoc} 0N/A // this doc comment is overridden to remove the reference to collections 0N/A // greater in size than Integer.MAX_VALUE 0N/A * Returns the number of elements in this queue. 0N/A * @return the number of elements in this queue 0N/A // this doc comment is a modified copy of the inherited doc comment, 0N/A // without the reference to unlimited queues. 0N/A * Returns the number of additional elements that this queue can ideally 0N/A * (in the absence of memory or resource constraints) accept without 0N/A * blocking. This is always equal to the initial capacity of this queue 3387N/A * less the current {@code size} of this queue. 0N/A * <p>Note that you <em>cannot</em> always tell if an attempt to insert 3387N/A * an element will succeed by inspecting {@code remainingCapacity} 0N/A * because it may be the case that another thread is about to 0N/A * insert or remove an element. 0N/A * Removes a single instance of the specified element from this queue, 3387N/A * if it is present. More formally, removes an element {@code e} such 3387N/A * that {@code o.equals(e)}, if this queue contains one or more such 3387N/A * Returns {@code true} if this queue contained the specified element 0N/A * (or equivalently, if this queue changed as a result of the call). 3387N/A * <p>Removal of interior elements in circular array based queues 3387N/A * is an intrinsically slow and disruptive operation, so should 3387N/A * be undertaken only in exceptional circumstances, ideally 3387N/A * only when the queue is known not to be accessible by other 0N/A * @param o element to be removed from this queue, if present 3387N/A * @return {@code true} if this queue changed as a result of the call 3387N/A * Returns {@code true} if this queue contains the specified element. 3387N/A * More formally, returns {@code true} if and only if this queue contains 3387N/A * at least one element {@code e} such that {@code o.equals(e)}. 0N/A * @param o object to be checked for containment in this queue 3387N/A * @return {@code true} if this queue contains the specified element 0N/A * Returns an array containing all of the elements in this queue, in 0N/A * <p>The returned array will be "safe" in that no references to it are 0N/A * maintained by this queue. (In other words, this method must allocate 0N/A * a new array). The caller is thus free to modify the returned array. 0N/A * <p>This method acts as bridge between array-based and collection-based 0N/A * @return an array containing all of the elements in this queue 0N/A * Returns an array containing all of the elements in this queue, in 0N/A * proper sequence; the runtime type of the returned array is that of 0N/A * the specified array. If the queue fits in the specified array, it 0N/A * is returned therein. Otherwise, a new array is allocated with the 0N/A * runtime type of the specified array and the size of this queue. 0N/A * <p>If this queue fits in the specified array with room to spare 0N/A * (i.e., the array has more elements than this queue), the element in 0N/A * the array immediately following the end of the queue is set to 0N/A * <p>Like the {@link #toArray()} method, this method acts as bridge between 0N/A * array-based and collection-based APIs. Further, this method allows 0N/A * precise control over the runtime type of the output array, and may, 0N/A * under certain circumstances, be used to save allocation costs. 3387N/A * <p>Suppose {@code x} is a queue known to contain only strings. 0N/A * The following code can be used to dump the queue into a newly 3387N/A * allocated array of {@code String}: 0N/A * String[] y = x.toArray(new String[0]);</pre> 3387N/A * Note that {@code toArray(new Object[0])} is identical in function to 0N/A * @param a the array into which the elements of the queue are to 0N/A * be stored, if it is big enough; otherwise, a new array of the 0N/A * same runtime type is allocated for this purpose 0N/A * @return an array containing all of the elements in this queue 0N/A * @throws ArrayStoreException if the runtime type of the specified array 0N/A * is not a supertype of the runtime type of every element in 0N/A * @throws NullPointerException if the specified array is null 0N/A * Atomically removes all of the elements from this queue. 0N/A * The queue will be empty after this call returns. 0N/A * @throws UnsupportedOperationException {@inheritDoc} 0N/A * @throws ClassCastException {@inheritDoc} 0N/A * @throws NullPointerException {@inheritDoc} 0N/A * @throws IllegalArgumentException {@inheritDoc} 0N/A * @throws UnsupportedOperationException {@inheritDoc} 0N/A * @throws ClassCastException {@inheritDoc} 0N/A * @throws NullPointerException {@inheritDoc} 0N/A * @throws IllegalArgumentException {@inheritDoc} 0N/A * Returns an iterator over the elements in this queue in proper sequence. 3387N/A * The elements will be returned in order from first (head) to last (tail). 3387N/A * <p>The returned {@code Iterator} is a "weakly consistent" iterator that 3387N/A * will never throw {@link java.util.ConcurrentModificationException 3387N/A * ConcurrentModificationException}, 0N/A * and guarantees to traverse elements as they existed upon 0N/A * construction of the iterator, and may (but is not guaranteed to) 0N/A * reflect any modifications subsequent to construction. 0N/A * @return an iterator over the elements in this queue in proper sequence 3387N/A * Iterator for ArrayBlockingQueue. To maintain weak consistency 3387N/A * with respect to puts and takes, we (1) read ahead one slot, so 3387N/A * as to not report hasNext true but then not have an element to 3387N/A * return -- however we later recheck this slot to use the most 3387N/A * current value; (2) ensure that each array slot is traversed at 3387N/A * most once (by tracking "remaining" elements); (3) skip over 3387N/A * null slots, which can occur if takes race ahead of iterators. 3387N/A * However, for circular array-based queues, we cannot rely on any 3387N/A * well established definition of what it means to be weakly 3387N/A * consistent with respect to interior removes since these may 3387N/A * require slot overwrites in the process of sliding elements to 3387N/A * cover gaps. So we settle for resiliency, operating on 3387N/A * established apparent nexts, which may miss some elements that 3387N/A * have moved between calls to next. 3387N/A private int lastRet;
// Index of last element returned, or -1 if none 3387N/A // only remove if item still at index