[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.10 - (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 : dholmes 1.8 * A collection designed for holding elements prior to processing.
11 : tim 1.2 * Besides basic {@link Collection} operations, queues provide
12 :     * additional insertion, extraction, and inspection operations.
13 : dholmes 1.8 *
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 : dholmes 1.8 * comparator, or the elements' natural ordering, and LIFO queues (or
18 :     * stacks) which order the elements LIFO (last-in-first-out).
19 :     * Whatever the ordering used, the <em>head</em> of the queue is that element
20 :     * which would be removed by a call to {@link #remove() } or {@link #poll()}.
21 : dholmes 1.10 * Every <tt>Queue</tt> implementation must specify its ordering guarantees.
22 : tim 1.2 *
23 :     * <p>The {@link #offer(E)} method adds an element if possible, otherwise
24 :     * returning <tt>false</tt>. This differs from the {@link
25 :     * Collections#add(Object)} method, which throws an unchecked exception upon
26 : tim 1.1 * failure. It is designed for use in collections in which failure to
27 :     * add is a normal, rather than exceptional occurrence, for example,
28 : dholmes 1.8 * in fixed-capacity (or &quot;bounded&quot;) queues.
29 : tim 1.9 *
30 : dl 1.7 * <p>The {@link #remove()} and {@link #poll()} methods remove and
31 : dholmes 1.8 * return the head of the queue.
32 :     * Exactly which element is removed from the queue is a
33 : dl 1.7 * function of the queue's ordering policy, which differs from
34 : dholmes 1.8 * implementation to implementation. The <tt>remove()</tt> and
35 : dl 1.7 * <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 : dholmes 1.8 * <p>The {@link #element()} and {@link #peek()} methods return, but do
40 : dholmes 1.10 * not remove, the head of the queue.
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 : dl 1.7 * <p><tt>Queue</tt> implementations generally do not allow insertion
49 :     * of <tt>null</tt> elements, although some implementations, such as
50 : brian 1.6 * {@link LinkedList}, do not prohibit insertion of <tt>null</tt>.
51 : dl 1.7 * Even in the implementations that permit it, <tt>null</tt> should
52 :     * not be inserted into a <tt>Queue</tt>, as <tt>null</tt> is also
53 :     * used as a special return value by the <tt>poll</tt> method to
54 :     * indicate that the queue contains no elements.
55 : tim 1.2 *
56 :     * <p>This interface is a member of the
57 :     * <a href="{@docRoot}/../guide/collections/index.html">
58 :     * Java Collections Framework</a>.
59 :     *
60 :     * @see Collection
61 :     * @see LinkedList
62 :     * @see PriorityQueue
63 : brian 1.6 * @see java.util.concurrent.LinkedQueue
64 : tim 1.2 * @see java.util.concurrent.BlockingQueue
65 :     * @see java.util.concurrent.ArrayBlockingQueue
66 :     * @see java.util.concurrent.LinkedBlockingQueue
67 :     * @see java.util.concurrent.PriorityBlockingQueue
68 : dl 1.7 * @since 1.5
69 :     * @author Doug Lea
70 : tim 1.2 */
71 : tim 1.1 public interface Queue<E> extends Collection<E> {
72 : dholmes 1.8
73 : tim 1.1 /**
74 : tim 1.2 * Add the specified element to this queue, if possible.
75 :     *
76 : dholmes 1.10 * @param o the element to add.
77 : tim 1.9 * @return <tt>true</tt> if it was possible to add the element to
78 : dholmes 1.10 * this queue, else <tt>false</tt>
79 : tim 1.2 */
80 : dholmes 1.10 boolean offer(E o);
81 : tim 1.1
82 :     /**
83 : dholmes 1.8 * Retrieve and remove the head of this queue, if it is available.
84 : tim 1.2 *
85 : dholmes 1.8 * @return the head of this queue, or <tt>null</tt> if this
86 : tim 1.9 * queue is empty.
87 : tim 1.2 */
88 : dl 1.7 E poll();
89 : tim 1.1
90 :     /**
91 : tim 1.9 * Retrieve and remove the head of this queue.
92 : dholmes 1.8 * This method differs
93 : dholmes 1.10 * from the <tt>poll</tt> method in that it throws an exception if this
94 : tim 1.9 * queue is empty.
95 : tim 1.2 *
96 : dholmes 1.8 * @return the head of this queue.
97 :     * @throws NoSuchElementException if this queue is empty.
98 : tim 1.2 */
99 : tim 1.9 E remove();
100 : tim 1.1
101 :     /**
102 : dholmes 1.10 * Retrieve, but do not remove, the head of this queue.
103 :     * This method differs from the <tt>poll</tt>
104 :     * method only in that this method does not remove the head element from
105 : dholmes 1.8 * this queue.
106 : tim 1.2 *
107 : dholmes 1.8 * @return the head of this queue, or <tt>null</tt> if this queue is empty.
108 : tim 1.2 */
109 : dl 1.7 E peek();
110 : tim 1.1
111 :     /**
112 : dholmes 1.8 * Retrieve, but do not remove, the head of this queue. This method
113 : tim 1.9 * differs from the <tt>peek</tt> method only in that it throws an
114 : dholmes 1.8 * exception if this queue is empty.
115 : tim 1.2 *
116 : dholmes 1.8 * @return the head of this queue.
117 :     * @throws NoSuchElementException if this queue is empty.
118 : tim 1.2 */
119 : tim 1.9 E element();
120 : tim 1.1 }
121 : dholmes 1.8
122 :    
123 :    
124 :    
125 :    
126 :    
127 :    
128 :    
129 :    
130 :    

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8