1 |
|
package java.util; |
2 |
|
|
3 |
|
/** |
4 |
< |
* Queues are Collections supporting additional basic insertion, |
5 |
< |
* extraction, and inspection operations. |
6 |
< |
* |
7 |
< |
* <p> Queues typically, but do not necessarily order elements in a |
8 |
< |
* FIFO (first-in-first-out) manner. Among the exceptions are priority |
9 |
< |
* queues, that order elements in accord with supplied |
10 |
< |
* Comparators. Every Queue implementation must specify its ordering |
11 |
< |
* guarantees, |
12 |
< |
* |
13 |
< |
* <p> The <tt>offer</tt> method adds an element if possible, |
14 |
< |
* otherwise returning <tt>false</tt>. This differs from the |
15 |
< |
* Collections.add method, that throws an unchecked exception upon |
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 queues. |
19 |
> |
* in fixed-capacity (or “bounded”) queues. |
20 |
|
* |
21 |
< |
* <p> The <tt>remove</tt> and <tt>poll</tt> methods delete and return |
22 |
< |
* an element in accord with the implementation's ordering policies -- |
23 |
< |
* for example, in FIFO queues, it will return the oldest element. |
24 |
< |
* The <tt>remove</tt> and <tt>poll</tt> differ only in their behavior |
25 |
< |
* when the queue is empty: <tt>poll</tt> returns <tt>null</tt> while |
26 |
< |
* <tt>remove</tt> throws an exception. These are designed for usage |
26 |
< |
* contexts in which emptiness is considered to be normal versus |
27 |
< |
* exceptional. |
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 <tt>element</tt> and <tt>peek</tt> methods return but do |
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 |
< |
* <tt>remove</tt> and <tt>poll</tt> respectively. |
30 |
> |
* the <tt>remove</tt> and <tt>poll</tt> methods respectively. |
31 |
|
* |
32 |
< |
* <p> The Queue interface does not define blocking queue methods |
33 |
< |
* (i.e., those that wait for elements to appear and/or for space to |
34 |
< |
* be available) that are common in concurrent programming. These are |
35 |
< |
* defined in the extended java.util.concurrent.BlockingQueue |
36 |
< |
* interface. |
37 |
< |
* |
38 |
< |
* <p> Queue implementations generally do not allow insertion of |
39 |
< |
* <tt>null</tt>. Even in those that allow it, it is a very bad idea |
40 |
< |
* to do so, since <tt>null</tt> is also used as a sentinel by |
41 |
< |
* <tt>poll</tt> to indicate that no elements exist. |
42 |
< |
**/ |
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> { |
45 |
– |
|
58 |
|
/** |
59 |
< |
* Add the given object to this queue if possible. |
60 |
< |
* @param x the object to add |
61 |
< |
* @return true if successful |
62 |
< |
**/ |
63 |
< |
public boolean offer(E x); |
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 |
< |
* Delete and return an object from the queue if one is available. |
68 |
< |
* @return the object, or null if the queue is empty. |
69 |
< |
**/ |
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 |
< |
* Delete and return the element produced by poll, if the queue is |
80 |
< |
* not empty. |
81 |
< |
* @return an element |
82 |
< |
* @throws NoSuchElementException if empty |
83 |
< |
**/ |
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 delete the element that will be returned by |
90 |
< |
* the next call to poll. |
91 |
< |
* @return an element, or null if empty |
92 |
< |
**/ |
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 delete the element that will be returned by |
100 |
< |
* the next call to poll, if the queue is not empty. |
101 |
< |
* @return an element |
102 |
< |
* @throws NoSuchElementException if empty |
103 |
< |
**/ |
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 |
|
} |