ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Queue.java
Revision: 1.2
Committed: Sun May 18 18:10:02 2003 UTC (20 years, 11 months ago) by tim
Branch: MAIN
Changes since 1.1: +83 -57 lines
Log Message:
Copied Queue, AbstractQueue, and PriorityQueue from src/dl,
fixed some type parameterization, and added some basic tests.
Added control for -warnunchecked in user properties file.

File Contents

# Content
1 package java.util;
2
3 /**
4 * A Collection designed for holding elements prior to processing.
5 * Besides basic {@link Collection} operations, queues provide
6 * additional insertion, extraction, and inspection operations.
7 *
8 * <p>Queues typically, but do not necessarily, order elements in a
9 * FIFO (first-in-first-out) manner. Among the exceptions are priority
10 * queues, which order elements according to a supplied comparators, or
11 * the elements natural ordering. Every Queue implementation must specify
12 * its ordering guarantees.
13 *
14 * <p>The {@link #offer(E)} method adds an element if possible, otherwise
15 * returning <tt>false</tt>. This differs from the {@link
16 * Collections#add(Object)} method, which throws an unchecked exception upon
17 * failure. It is designed for use in collections in which failure to
18 * add is a normal, rather than exceptional occurrence, for example,
19 * in fixed-capacity (or &ldquo;bounded&rdquo;) queues.
20 *
21 * <p>The {@link #remove()} and {@link #poll()} methods remove and return an
22 * element in accord with the implementation's ordering policy. For example,
23 * in FIFO queues, they remove and return the oldest element in the queue.
24 * The <tt>remove()</tt> and <tt>poll()</tt> methods differ only in their
25 * behavior when the queue is empty: the <tt>remove()</tt> method throws an
26 * exception, while the <tt>poll()</tt> method returns <tt>null</tt>.
27 *
28 * <p>The {@link #element()} and {@link #peek()} methods return but do
29 * not delete the element that would be obtained by a call to
30 * the <tt>remove</tt> and <tt>poll</tt> methods respectively.
31 *
32 * <p>The <tt>Queue</tt> interface does not define the <i>blocking queue
33 * methods</i>, which are common in concurrent programming. These methods,
34 * which wait for elements to appear or for space to become available, are
35 * defined in the {@link java.util.concurrent.BlockingQueue} interface, which
36 * extends this interface.
37 *
38 * <p><tt>Queue</tt> implementations generally do not allow insertion of
39 * <tt>null</tt> elements. Even in the few implementations that permit it,
40 * it is a bad idea, as <tt>null</tt> is also used as a special return value
41 * by the <tt>poll</tt> method to indicate that the queue contains no
42 * elements.
43 *
44 * <p>This interface is a member of the
45 * <a href="{@docRoot}/../guide/collections/index.html">
46 * Java Collections Framework</a>.
47 *
48 * @see Collection
49 * @see LinkedList
50 * @see PriorityQueue
51 * @see LinkedQueue
52 * @see java.util.concurrent.BlockingQueue
53 * @see java.util.concurrent.ArrayBlockingQueue
54 * @see java.util.concurrent.LinkedBlockingQueue
55 * @see java.util.concurrent.PriorityBlockingQueue
56 */
57 public interface Queue<E> extends Collection<E> {
58 /**
59 * Add the specified element to this queue, if possible.
60 *
61 * @param element the element to add.
62 * @return true if it was possible to add the element to the queue.
63 */
64 public boolean offer(E element);
65
66 /**
67 * Remove and return an element from the queue if one is available.
68 * Exactly which element is removed from the queue is a function
69 * of the queue's ordering policy, which differs from implementation
70 * to implementation. Possible orderings include (but are not limited
71 * to) first-in-first-out (FIFO), element priority, and arbitrary.
72 *
73 * @return an element previously on the queue, or <tt>null</tt> if the
74 * queue is empty.
75 */
76 public E poll();
77
78 /**
79 * Remove and return an element from the queue. This method differs
80 * from the <tt>poll</tt> method in that it throws an exception if the
81 * queue is empty.
82 *
83 * @return an element previously on the queue.
84 * @throws NoSuchElementException if the queue is empty.
85 */
86 public E remove() throws NoSuchElementException;
87
88 /**
89 * Return, but do not remove, an element from the queue, or <tt>null</tt>
90 * if the queue is empty. This method returns the same object reference
91 * that would be returned by by the <tt>poll</tt> method. The two methods
92 * differ in that this method does not remove the element from the queue.
93 *
94 * @return an element on the queue, or <tt>null</tt> if the queue is empty.
95 */
96 public E peek();
97
98 /**
99 * Return, but do not remove, an element from the queue. This method
100 * differs from the <tt>peek</tt> method in that it throws an exception if
101 * the queue is empty.
102 *
103 * @return an element on the queue.
104 * @throws NoSuchElementException if the queue is empty.
105 */
106 public E element() throws NoSuchElementException;
107 }