| Queue.java |
1 /*
2 * %W% %E%
3 *
4 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6 */
7
8 package java.util;
9
10 /**
11 * A collection designed for holding elements prior to processing.
12 * Besides basic {@link java.util.Collection Collection} operations,
13 * queues provide additional insertion, extraction, and inspection
14 * operations. Each of these methods exists in two forms: one throws
15 * an exception if the operation fails, the other returns a special
16 * value (either <tt>null</tt> or <tt>false</tt>, depending on the
17 * operation). The latter form of the insert operation is designed
18 * specifically for use with capacity-restricted <tt>Queue</tt>
19 * implementations; in most implementations, insert operations cannot
20 * fail.
21 *
22 * <p>
23 * <table BORDER CELLPADDING=3 CELLSPACING=1>
24 * <tr>
25 * <td></td>
26 * <td ALIGN=CENTER><em>Throws exception</em></td>
27 * <td ALIGN=CENTER><em>Returns special value</em></td>
28 * </tr>
29 * <tr>
30 * <td><b>Insert</b></td>
31 * <td>{@link #add add(e)}</td>
32 * <td>{@link #offer offer(e)}</td>
33 * </tr>
34 * <tr>
35 * <td><b>Remove</b></td>
36 * <td>{@link #remove remove()}</td>
37 * <td>{@link #poll poll()}</td>
38 * </tr>
39 * <tr>
40 * <td><b>Examine</b></td>
41 * <td>{@link #element element()}</td>
42 * <td>{@link #peek peek()}</td>
43 * </tr>
44 * </table>
45 *
46 * <p>Queues typically, but do not necessarily, order elements in a
47 * FIFO (first-in-first-out) manner. Among the exceptions are
48 * priority queues, which order elements according to a supplied
49 * comparator, or the elements' natural ordering, and LIFO queues (or
50 * stacks) which order the elements LIFO (last-in-first-out).
51 * Whatever the ordering used, the <em>head</em> of the queue is that
52 * element which would be removed by a call to {@link #remove() } or
53 * {@link #poll()}. In a FIFO queue, all new elements are inserted at
54 * the <em> tail</em> of the queue. Other kinds of queues may use
55 * different placement rules. Every <tt>Queue</tt> implementation
56 * must specify its ordering properties.
57 *
58 * <p>The {@link #offer offer} method inserts an element if possible,
59 * otherwise returning <tt>false</tt>. This differs from the {@link
60 * java.util.Collection#add Collection.add} method, which can fail to
61 * add an element only by throwing an unchecked exception. The
62 * <tt>offer</tt> method is designed for use when failure is a normal,
63 * rather than exceptional occurrence, for example, in fixed-capacity
64 * (or "bounded") queues.
65 *
66 * <p>The {@link #remove()} and {@link #poll()} methods remove and
67 * return the head of the queue.
68 * Exactly which element is removed from the queue is a
69 * function of the queue's ordering policy, which differs from
70 * implementation to implementation. The <tt>remove()</tt> and
71 * <tt>poll()</tt> methods differ only in their behavior when the
72 * queue is empty: the <tt>remove()</tt> method throws an exception,
73 * while the <tt>poll()</tt> method returns <tt>null</tt>.
74 *
75 * <p>The {@link #element()} and {@link #peek()} methods return, but do
76 * not remove, the head of the queue.
77 *
78 * <p>The <tt>Queue</tt> interface does not define the <i>blocking queue
79 * methods</i>, which are common in concurrent programming. These methods,
80 * which wait for elements to appear or for space to become available, are
81 * defined in the {@link java.util.concurrent.BlockingQueue} interface, which
82 * extends this interface.
83 *
84 * <p><tt>Queue</tt> implementations generally do not allow insertion
85 * of <tt>null</tt> elements, although some implementations, such as
86 * {@link LinkedList}, do not prohibit insertion of <tt>null</tt>.
87 * Even in the implementations that permit it, <tt>null</tt> should
88 * not be inserted into a <tt>Queue</tt>, as <tt>null</tt> is also
89 * used as a special return value by the <tt>poll</tt> method to
90 * indicate that the queue contains no elements.
91 *
92 * <p><tt>Queue</tt> implementations generally do not define
93 * element-based versions of methods <tt>equals</tt> and
94 * <tt>hashCode</tt> but instead inherit the identity based versions
95 * from class <tt>Object</tt>, because element-based equality is not
96 * always well-defined for queues with the same elements but different
97 * ordering properties.
98 *
99 *
100 * <p>This interface is a member of the
101 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
102 * Java Collections Framework</a>.
103 *
104 * @see java.util.Collection
105 * @see LinkedList
106 * @see PriorityQueue
107 * @see java.util.concurrent.LinkedBlockingQueue
108 * @see java.util.concurrent.BlockingQueue
109 * @see java.util.concurrent.ArrayBlockingQueue
110 * @see java.util.concurrent.LinkedBlockingQueue
111 * @see java.util.concurrent.PriorityBlockingQueue
112 * @since 1.5
113 * @author Doug Lea
114 * @param <E> the type of elements held in this collection
115 */
116 public interface Queue<E> extends Collection<E> {
117 /**
118 * Inserts the specified element into this queue if it is possible to do so
119 * immediately without violating capacity restrictions, returning
120 * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
121 * if no space is currently available.
122 *
123 * @param e the element to add
124 * @return <tt>true</tt> (as specified by {@link Collection#add})
125 * @throws IllegalStateException if the element cannot be added at this
126 * time due to capacity restrictions
127 * @throws ClassCastException if the class of the specified element
128 * prevents it from being added to this queue
129 * @throws NullPointerException if the specified element is null and
130 * this queue does not permit null elements
131 * @throws IllegalArgumentException if some property of this element
132 * prevents it from being added to this queue
133 */
134 boolean add(E e);
135
136 /**
137 * Inserts the specified element into this queue if it is possible to do
138 * so immediately without violating capacity restrictions.
139 * When using a capacity-restricted queue, this method is generally
140 * preferable to {@link #add}, which can fail to insert an element only
141 * by throwing an exception.
142 *
143 * @param e the element to add
144 * @return <tt>true</tt> if the element was added to this queue, else
145 * <tt>false</tt>
146 * @throws ClassCastException if the class of the specified element
147 * prevents it from being added to this queue
148 * @throws NullPointerException if the specified element is null and
149 * this queue does not permit null elements
150 * @throws IllegalArgumentException if some property of this element
151 * prevents it from being added to this queue
152 */
153 boolean offer(E e);
154
155 /**
156 * Retrieves and removes the head of this queue. This method differs
157 * from {@link #poll poll} only in that it throws an exception if this
158 * queue is empty.
159 *
160 * @return the head of this queue
161 * @throws NoSuchElementException if this queue is empty
162 */
163 E remove();
164
165 /**
166 * Retrieves and removes the head of this queue,
167 * or returns <tt>null</tt> if this queue is empty.
168 *
169 * @return the head of this queue, or <tt>null</tt> if this queue is empty
170 */
171 E poll();
172
173 /**
174 * Retrieves, but does not remove, the head of this queue. This method
175 * differs from {@link #peek peek} only in that it throws an exception
176 * if this queue is empty.
177 *
178 * @return the head of this queue
179 * @throws NoSuchElementException if this queue is empty
180 */
181 E element();
182
183 /**
184 * Retrieves, but does not remove, the head of this queue,
185 * or returns <tt>null</tt> if this queue is empty.
186 *
187 * @return the head of this queue, or <tt>null</tt> if this queue is empty
188 */
189 E peek();
190 }
191