[cvs] / jsr166 / src / main / java / util / Queue.java Repository:
ViewVC logotype

Annotation of /jsr166/src/main/java/util/Queue.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (view) (download)

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 :     *
14 :     * <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 &ldquo;bounded&rdquo;) queues.
26 : dl 1.4
27 : dl 1.3 *
28 : tim 1.2 * <p>The {@link #remove()} and {@link #poll()} methods remove and return an
29 : dl 1.4 * element in accord with the implementation's ordering policy.
30 :     * Exactly which element is removed from the queue is a function
31 :     * of the queue's ordering policy, which differs from implementation
32 :     * to implementation. Possible orderings include (but are not limited
33 :     * to) first-in-first-out (FIFO), element priority, and arbitrary.
34 : tim 1.2 * The <tt>remove()</tt> and <tt>poll()</tt> methods differ only in their
35 :     * behavior when the queue is empty: the <tt>remove()</tt> method throws an
36 :     * exception, while the <tt>poll()</tt> method returns <tt>null</tt>.
37 : tim 1.1 *
38 : tim 1.2 * <p>The {@link #element()} and {@link #peek()} methods return but do
39 : tim 1.1 * not delete the element that would be obtained by a call to
40 : tim 1.2 * the <tt>remove</tt> and <tt>poll</tt> methods respectively.
41 : tim 1.1 *
42 : tim 1.2 * <p>The <tt>Queue</tt> interface does not define the <i>blocking queue
43 :     * methods</i>, which are common in concurrent programming. These methods,
44 :     * which wait for elements to appear or for space to become available, are
45 :     * defined in the {@link java.util.concurrent.BlockingQueue} interface, which
46 :     * extends this interface.
47 :     *
48 :     * <p><tt>Queue</tt> implementations generally do not allow insertion of
49 :     * <tt>null</tt> elements. Even in the few implementations that permit it,
50 :     * it is a bad idea, as <tt>null</tt> is also used as a special return value
51 :     * by the <tt>poll</tt> method to indicate that the queue contains no
52 :     * elements.
53 :     *
54 :     * <p>This interface is a member of the
55 :     * <a href="{@docRoot}/../guide/collections/index.html">
56 :     * Java Collections Framework</a>.
57 :     *
58 :     * @see Collection
59 :     * @see LinkedList
60 :     * @see PriorityQueue
61 :     * @see LinkedQueue
62 :     * @see java.util.concurrent.BlockingQueue
63 :     * @see java.util.concurrent.ArrayBlockingQueue
64 :     * @see java.util.concurrent.LinkedBlockingQueue
65 :     * @see java.util.concurrent.PriorityBlockingQueue
66 :     */
67 : tim 1.1 public interface Queue<E> extends Collection<E> {
68 :     /**
69 : tim 1.2 * Add the specified element to this queue, if possible.
70 :     *
71 :     * @param element the element to add.
72 :     * @return true if it was possible to add the element to the queue.
73 :     */
74 :     public boolean offer(E element);
75 : tim 1.1
76 :     /**
77 : tim 1.2 * Remove and return an element from the queue if one is available.
78 :     *
79 :     * @return an element previously on the queue, or <tt>null</tt> if the
80 : dl 1.3 * queue is empty.
81 : tim 1.2 */
82 : tim 1.1 public E poll();
83 :    
84 :     /**
85 : tim 1.2 * Remove and return an element from the queue. This method differs
86 :     * from the <tt>poll</tt> method in that it throws an exception if the
87 : dl 1.3 * queue is empty.
88 : tim 1.2 *
89 :     * @return an element previously on the queue.
90 :     * @throws NoSuchElementException if the queue is empty.
91 :     */
92 : tim 1.1 public E remove() throws NoSuchElementException;
93 :    
94 :     /**
95 : tim 1.2 * Return, but do not remove, an element from the queue, or <tt>null</tt>
96 :     * if the queue is empty. This method returns the same object reference
97 :     * that would be returned by by the <tt>poll</tt> method. The two methods
98 :     * differ in that this method does not remove the element from the queue.
99 :     *
100 :     * @return an element on the queue, or <tt>null</tt> if the queue is empty.
101 :     */
102 : tim 1.1 public E peek();
103 :    
104 :     /**
105 : tim 1.2 * Return, but do not remove, an element from the queue. This method
106 :     * differs from the <tt>peek</tt> method in that it throws an exception if
107 :     * the queue is empty.
108 :     *
109 :     * @return an element on the queue.
110 :     * @throws NoSuchElementException if the queue is empty.
111 :     */
112 : tim 1.1 public E element() throws NoSuchElementException;
113 :     }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8