jaroslav@597: /* jaroslav@597: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@597: * jaroslav@597: * This code is free software; you can redistribute it and/or modify it jaroslav@597: * under the terms of the GNU General Public License version 2 only, as jaroslav@597: * published by the Free Software Foundation. Oracle designates this jaroslav@597: * particular file as subject to the "Classpath" exception as provided jaroslav@597: * by Oracle in the LICENSE file that accompanied this code. jaroslav@597: * jaroslav@597: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@597: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@597: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@597: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@597: * accompanied this code). jaroslav@597: * jaroslav@597: * You should have received a copy of the GNU General Public License version jaroslav@597: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@597: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@597: * jaroslav@597: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@597: * or visit www.oracle.com if you need additional information or have any jaroslav@597: * questions. jaroslav@597: */ jaroslav@597: jaroslav@597: /* jaroslav@597: * This file is available under and governed by the GNU General Public jaroslav@597: * License version 2 only, as published by the Free Software Foundation. jaroslav@597: * However, the following notice accompanied the original version of this jaroslav@597: * file: jaroslav@597: * jaroslav@597: * Written by Doug Lea with assistance from members of JCP JSR-166 jaroslav@597: * Expert Group and released to the public domain, as explained at jaroslav@597: * http://creativecommons.org/publicdomain/zero/1.0/ jaroslav@597: */ jaroslav@597: jaroslav@597: package java.util; jaroslav@597: jaroslav@597: /** jaroslav@597: * A collection designed for holding elements prior to processing. jaroslav@597: * Besides basic {@link java.util.Collection Collection} operations, jaroslav@597: * queues provide additional insertion, extraction, and inspection jaroslav@597: * operations. Each of these methods exists in two forms: one throws jaroslav@597: * an exception if the operation fails, the other returns a special jaroslav@597: * value (either null or false, depending on the jaroslav@597: * operation). The latter form of the insert operation is designed jaroslav@597: * specifically for use with capacity-restricted Queue jaroslav@597: * implementations; in most implementations, insert operations cannot jaroslav@597: * fail. jaroslav@597: * jaroslav@597: *

jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: * jaroslav@597: *
Throws exceptionReturns special value
Insert{@link #add add(e)}{@link #offer offer(e)}
Remove{@link #remove remove()}{@link #poll poll()}
Examine{@link #element element()}{@link #peek peek()}
jaroslav@597: * jaroslav@597: *

Queues typically, but do not necessarily, order elements in a jaroslav@597: * FIFO (first-in-first-out) manner. Among the exceptions are jaroslav@597: * priority queues, which order elements according to a supplied jaroslav@597: * comparator, or the elements' natural ordering, and LIFO queues (or jaroslav@597: * stacks) which order the elements LIFO (last-in-first-out). jaroslav@597: * Whatever the ordering used, the head of the queue is that jaroslav@597: * element which would be removed by a call to {@link #remove() } or jaroslav@597: * {@link #poll()}. In a FIFO queue, all new elements are inserted at jaroslav@597: * the tail of the queue. Other kinds of queues may use jaroslav@597: * different placement rules. Every Queue implementation jaroslav@597: * must specify its ordering properties. jaroslav@597: * jaroslav@597: *

The {@link #offer offer} method inserts an element if possible, jaroslav@597: * otherwise returning false. This differs from the {@link jaroslav@597: * java.util.Collection#add Collection.add} method, which can fail to jaroslav@597: * add an element only by throwing an unchecked exception. The jaroslav@597: * offer method is designed for use when failure is a normal, jaroslav@597: * rather than exceptional occurrence, for example, in fixed-capacity jaroslav@597: * (or "bounded") queues. jaroslav@597: * jaroslav@597: *

The {@link #remove()} and {@link #poll()} methods remove and jaroslav@597: * return the head of the queue. jaroslav@597: * Exactly which element is removed from the queue is a jaroslav@597: * function of the queue's ordering policy, which differs from jaroslav@597: * implementation to implementation. The remove() and jaroslav@597: * poll() methods differ only in their behavior when the jaroslav@597: * queue is empty: the remove() method throws an exception, jaroslav@597: * while the poll() method returns null. jaroslav@597: * jaroslav@597: *

The {@link #element()} and {@link #peek()} methods return, but do jaroslav@597: * not remove, the head of the queue. jaroslav@597: * jaroslav@597: *

The Queue interface does not define the blocking queue jaroslav@597: * methods, which are common in concurrent programming. These methods, jaroslav@597: * which wait for elements to appear or for space to become available, are jaroslav@597: * defined in the {@link java.util.concurrent.BlockingQueue} interface, which jaroslav@597: * extends this interface. jaroslav@597: * jaroslav@597: *

Queue implementations generally do not allow insertion jaroslav@597: * of null elements, although some implementations, such as jaroslav@597: * {@link LinkedList}, do not prohibit insertion of null. jaroslav@597: * Even in the implementations that permit it, null should jaroslav@597: * not be inserted into a Queue, as null is also jaroslav@597: * used as a special return value by the poll method to jaroslav@597: * indicate that the queue contains no elements. jaroslav@597: * jaroslav@597: *

Queue implementations generally do not define jaroslav@597: * element-based versions of methods equals and jaroslav@597: * hashCode but instead inherit the identity based versions jaroslav@597: * from class Object, because element-based equality is not jaroslav@597: * always well-defined for queues with the same elements but different jaroslav@597: * ordering properties. jaroslav@597: * jaroslav@597: * jaroslav@597: *

This interface is a member of the jaroslav@597: * jaroslav@597: * Java Collections Framework. jaroslav@597: * jaroslav@597: * @see java.util.Collection jaroslav@597: * @see LinkedList jaroslav@597: * @see PriorityQueue jaroslav@597: * @see java.util.concurrent.LinkedBlockingQueue jaroslav@597: * @see java.util.concurrent.BlockingQueue jaroslav@597: * @see java.util.concurrent.ArrayBlockingQueue jaroslav@597: * @see java.util.concurrent.LinkedBlockingQueue jaroslav@597: * @see java.util.concurrent.PriorityBlockingQueue jaroslav@597: * @since 1.5 jaroslav@597: * @author Doug Lea jaroslav@597: * @param the type of elements held in this collection jaroslav@597: */ jaroslav@597: public interface Queue extends Collection { jaroslav@597: /** jaroslav@597: * Inserts the specified element into this queue if it is possible to do so jaroslav@597: * immediately without violating capacity restrictions, returning jaroslav@597: * true upon success and throwing an IllegalStateException jaroslav@597: * if no space is currently available. jaroslav@597: * jaroslav@597: * @param e the element to add jaroslav@597: * @return true (as specified by {@link Collection#add}) jaroslav@597: * @throws IllegalStateException if the element cannot be added at this jaroslav@597: * time due to capacity restrictions jaroslav@597: * @throws ClassCastException if the class of the specified element jaroslav@597: * prevents it from being added to this queue jaroslav@597: * @throws NullPointerException if the specified element is null and jaroslav@597: * this queue does not permit null elements jaroslav@597: * @throws IllegalArgumentException if some property of this element jaroslav@597: * prevents it from being added to this queue jaroslav@597: */ jaroslav@597: boolean add(E e); jaroslav@597: jaroslav@597: /** jaroslav@597: * Inserts the specified element into this queue if it is possible to do jaroslav@597: * so immediately without violating capacity restrictions. jaroslav@597: * When using a capacity-restricted queue, this method is generally jaroslav@597: * preferable to {@link #add}, which can fail to insert an element only jaroslav@597: * by throwing an exception. jaroslav@597: * jaroslav@597: * @param e the element to add jaroslav@597: * @return true if the element was added to this queue, else jaroslav@597: * false jaroslav@597: * @throws ClassCastException if the class of the specified element jaroslav@597: * prevents it from being added to this queue jaroslav@597: * @throws NullPointerException if the specified element is null and jaroslav@597: * this queue does not permit null elements jaroslav@597: * @throws IllegalArgumentException if some property of this element jaroslav@597: * prevents it from being added to this queue jaroslav@597: */ jaroslav@597: boolean offer(E e); jaroslav@597: jaroslav@597: /** jaroslav@597: * Retrieves and removes the head of this queue. This method differs jaroslav@597: * from {@link #poll poll} only in that it throws an exception if this jaroslav@597: * queue is empty. jaroslav@597: * jaroslav@597: * @return the head of this queue jaroslav@597: * @throws NoSuchElementException if this queue is empty jaroslav@597: */ jaroslav@597: E remove(); jaroslav@597: jaroslav@597: /** jaroslav@597: * Retrieves and removes the head of this queue, jaroslav@597: * or returns null if this queue is empty. jaroslav@597: * jaroslav@597: * @return the head of this queue, or null if this queue is empty jaroslav@597: */ jaroslav@597: E poll(); jaroslav@597: jaroslav@597: /** jaroslav@597: * Retrieves, but does not remove, the head of this queue. This method jaroslav@597: * differs from {@link #peek peek} only in that it throws an exception jaroslav@597: * if this queue is empty. jaroslav@597: * jaroslav@597: * @return the head of this queue jaroslav@597: * @throws NoSuchElementException if this queue is empty jaroslav@597: */ jaroslav@597: E element(); jaroslav@597: jaroslav@597: /** jaroslav@597: * Retrieves, but does not remove, the head of this queue, jaroslav@597: * or returns null if this queue is empty. jaroslav@597: * jaroslav@597: * @return the head of this queue, or null if this queue is empty jaroslav@597: */ jaroslav@597: E peek(); jaroslav@597: }