1 |
tim |
1.1 |
package java.util; |
2 |
|
|
|
3 |
|
|
/** |
4 |
tim |
1.2 |
* 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 |
tim |
1.1 |
* failure. It is designed for use in collections in which failure to |
18 |
|
|
* add is a normal, rather than exceptional occurrence, for example, |
19 |
tim |
1.2 |
* in fixed-capacity (or “bounded”) queues. |
20 |
tim |
1.1 |
* |
21 |
tim |
1.2 |
* <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 |
tim |
1.1 |
* |
28 |
tim |
1.2 |
* <p>The {@link #element()} and {@link #peek()} methods return but do |
29 |
tim |
1.1 |
* not delete the element that would be obtained by a call to |
30 |
tim |
1.2 |
* the <tt>remove</tt> and <tt>poll</tt> methods respectively. |
31 |
tim |
1.1 |
* |
32 |
tim |
1.2 |
* <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 |
tim |
1.1 |
public interface Queue<E> extends Collection<E> { |
58 |
|
|
/** |
59 |
tim |
1.2 |
* 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 |
tim |
1.1 |
|
66 |
|
|
/** |
67 |
tim |
1.2 |
* 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 |
tim |
1.1 |
public E poll(); |
77 |
|
|
|
78 |
|
|
/** |
79 |
tim |
1.2 |
* 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 |
tim |
1.1 |
public E remove() throws NoSuchElementException; |
87 |
|
|
|
88 |
|
|
/** |
89 |
tim |
1.2 |
* 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 |
tim |
1.1 |
public E peek(); |
97 |
|
|
|
98 |
|
|
/** |
99 |
tim |
1.2 |
* 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 |
tim |
1.1 |
public E element() throws NoSuchElementException; |
107 |
|
|
} |