• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  Oracle designates this
7  * particular file as subject to the "Classpath" exception as provided
8  * by Oracle in the LICENSE file that accompanied this code.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /*
26  * This file is available under and governed by the GNU General Public
27  * License version 2 only, as published by the Free Software Foundation.
28  * However, the following notice accompanied the original version of this
29  * file:
30  * Written by Doug Lea with assistance from members of JCP JSR-166
31  * Expert Group and released to the public domain, as explained at
32  * http://creativecommons.org/publicdomain/zero/1.0/
33  */
34 
35 package java.util;
36 
37 // BEGIN android-note
38 // removed link to collections framework docs
39 // END android-note
40 
41 /**
42  * A collection designed for holding elements prior to processing.
43  * Besides basic {@link java.util.Collection Collection} operations,
44  * queues provide additional insertion, extraction, and inspection
45  * operations.  Each of these methods exists in two forms: one throws
46  * an exception if the operation fails, the other returns a special
47  * value (either {@code null} or {@code false}, depending on the
48  * operation).  The latter form of the insert operation is designed
49  * specifically for use with capacity-restricted {@code Queue}
50  * implementations; in most implementations, insert operations cannot
51  * fail.
52  *
53  * <table BORDER CELLPADDING=3 CELLSPACING=1>
54  * <caption>Summary of Queue methods</caption>
55  *  <tr>
56  *    <td></td>
57  *    <td ALIGN=CENTER><em>Throws exception</em></td>
58  *    <td ALIGN=CENTER><em>Returns special value</em></td>
59  *  </tr>
60  *  <tr>
61  *    <td><b>Insert</b></td>
62  *    <td>{@link Queue#add add(e)}</td>
63  *    <td>{@link Queue#offer offer(e)}</td>
64  *  </tr>
65  *  <tr>
66  *    <td><b>Remove</b></td>
67  *    <td>{@link Queue#remove remove()}</td>
68  *    <td>{@link Queue#poll poll()}</td>
69  *  </tr>
70  *  <tr>
71  *    <td><b>Examine</b></td>
72  *    <td>{@link Queue#element element()}</td>
73  *    <td>{@link Queue#peek peek()}</td>
74  *  </tr>
75  * </table>
76  *
77  * <p>Queues typically, but do not necessarily, order elements in a
78  * FIFO (first-in-first-out) manner.  Among the exceptions are
79  * priority queues, which order elements according to a supplied
80  * comparator, or the elements' natural ordering, and LIFO queues (or
81  * stacks) which order the elements LIFO (last-in-first-out).
82  * Whatever the ordering used, the <em>head</em> of the queue is that
83  * element which would be removed by a call to {@link #remove() } or
84  * {@link #poll()}.  In a FIFO queue, all new elements are inserted at
85  * the <em>tail</em> of the queue. Other kinds of queues may use
86  * different placement rules.  Every {@code Queue} implementation
87  * must specify its ordering properties.
88  *
89  * <p>The {@link #offer offer} method inserts an element if possible,
90  * otherwise returning {@code false}.  This differs from the {@link
91  * java.util.Collection#add Collection.add} method, which can fail to
92  * add an element only by throwing an unchecked exception.  The
93  * {@code offer} method is designed for use when failure is a normal,
94  * rather than exceptional occurrence, for example, in fixed-capacity
95  * (or &quot;bounded&quot;) queues.
96  *
97  * <p>The {@link #remove()} and {@link #poll()} methods remove and
98  * return the head of the queue.
99  * Exactly which element is removed from the queue is a
100  * function of the queue's ordering policy, which differs from
101  * implementation to implementation. The {@code remove()} and
102  * {@code poll()} methods differ only in their behavior when the
103  * queue is empty: the {@code remove()} method throws an exception,
104  * while the {@code poll()} method returns {@code null}.
105  *
106  * <p>The {@link #element()} and {@link #peek()} methods return, but do
107  * not remove, the head of the queue.
108  *
109  * <p>The {@code Queue} interface does not define the <i>blocking queue
110  * methods</i>, which are common in concurrent programming.  These methods,
111  * which wait for elements to appear or for space to become available, are
112  * defined in the {@link java.util.concurrent.BlockingQueue} interface, which
113  * extends this interface.
114  *
115  * <p>{@code Queue} implementations generally do not allow insertion
116  * of {@code null} elements, although some implementations, such as
117  * {@link LinkedList}, do not prohibit insertion of {@code null}.
118  * Even in the implementations that permit it, {@code null} should
119  * not be inserted into a {@code Queue}, as {@code null} is also
120  * used as a special return value by the {@code poll} method to
121  * indicate that the queue contains no elements.
122  *
123  * <p>{@code Queue} implementations generally do not define
124  * element-based versions of methods {@code equals} and
125  * {@code hashCode} but instead inherit the identity based versions
126  * from class {@code Object}, because element-based equality is not
127  * always well-defined for queues with the same elements but different
128  * ordering properties.
129  *
130  * @since 1.5
131  * @author Doug Lea
132  * @param <E> the type of elements held in this queue
133  */
134 public interface Queue<E> extends Collection<E> {
135     /**
136      * Inserts the specified element into this queue if it is possible to do so
137      * immediately without violating capacity restrictions, returning
138      * {@code true} upon success and throwing an {@code IllegalStateException}
139      * if no space is currently available.
140      *
141      * @param e the element to add
142      * @return {@code true} (as specified by {@link Collection#add})
143      * @throws IllegalStateException if the element cannot be added at this
144      *         time due to capacity restrictions
145      * @throws ClassCastException if the class of the specified element
146      *         prevents it from being added to this queue
147      * @throws NullPointerException if the specified element is null and
148      *         this queue does not permit null elements
149      * @throws IllegalArgumentException if some property of this element
150      *         prevents it from being added to this queue
151      */
add(E e)152     boolean add(E e);
153 
154     /**
155      * Inserts the specified element into this queue if it is possible to do
156      * so immediately without violating capacity restrictions.
157      * When using a capacity-restricted queue, this method is generally
158      * preferable to {@link #add}, which can fail to insert an element only
159      * by throwing an exception.
160      *
161      * @param e the element to add
162      * @return {@code true} if the element was added to this queue, else
163      *         {@code false}
164      * @throws ClassCastException if the class of the specified element
165      *         prevents it from being added to this queue
166      * @throws NullPointerException if the specified element is null and
167      *         this queue does not permit null elements
168      * @throws IllegalArgumentException if some property of this element
169      *         prevents it from being added to this queue
170      */
offer(E e)171     boolean offer(E e);
172 
173     /**
174      * Retrieves and removes the head of this queue.  This method differs
175      * from {@link #poll poll} only in that it throws an exception if this
176      * queue is empty.
177      *
178      * @return the head of this queue
179      * @throws NoSuchElementException if this queue is empty
180      */
remove()181     E remove();
182 
183     /**
184      * Retrieves and removes the head of this queue,
185      * or returns {@code null} if this queue is empty.
186      *
187      * @return the head of this queue, or {@code null} if this queue is empty
188      */
poll()189     E poll();
190 
191     /**
192      * Retrieves, but does not remove, the head of this queue.  This method
193      * differs from {@link #peek peek} only in that it throws an exception
194      * if this queue is empty.
195      *
196      * @return the head of this queue
197      * @throws NoSuchElementException if this queue is empty
198      */
element()199     E element();
200 
201     /**
202      * Retrieves, but does not remove, the head of this queue,
203      * or returns {@code null} if this queue is empty.
204      *
205      * @return the head of this queue, or {@code null} if this queue is empty
206      */
peek()207     E peek();
208 }
209