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