ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractQueue.java
Revision: 1.26
Committed: Tue Apr 26 19:54:03 2005 UTC (19 years ago) by jsr166
Branch: MAIN
Changes since 1.25: +13 -14 lines
Log Message:
doc fixes

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.24 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 dl 1.2 */
6    
7 tim 1.1 package java.util;
8    
9     /**
10 dl 1.14 * This class provides skeletal implementations of some {@link Queue}
11     * operations. The implementations in this class are appropriate when
12     * the base implementation does <em>not</em> allow <tt>null</tt>
13     * elements. Methods {@link #add add}, {@link #remove remove}, and
14     * {@link #element element} are based on {@link #offer offer}, {@link
15     * #poll poll}, and {@link #peek peek}, respectively but throw
16     * exceptions instead of indicating failure via <tt>false</tt> or
17 dholmes 1.9 * <tt>null</tt> returns.
18 dl 1.14 *
19     * <p> A <tt>Queue</tt> implementation that extends this class must
20     * minimally define a method {@link Queue#offer} which does not permit
21     * insertion of <tt>null</tt> elements, along with methods {@link
22 dl 1.16 * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and a
23     * {@link Collection#iterator} supporting {@link
24 dl 1.18 * Iterator#remove}. Typically, additional methods will be overridden
25 dl 1.16 * as well. If these requirements cannot be met, consider instead
26     * subclassing {@link AbstractCollection}.
27 dl 1.14 *
28 dl 1.25 * <p>This class is a member of the
29     * <a href="{@docRoot}/../guide/collections/index.html">
30     * Java Collections Framework</a>.
31 jsr166 1.26 *
32 dl 1.4 * @since 1.5
33     * @author Doug Lea
34 dl 1.21 * @param <E> the type of elements held in this collection
35 tim 1.1 */
36 tim 1.10 public abstract class AbstractQueue<E>
37     extends AbstractCollection<E>
38 dholmes 1.9 implements Queue<E> {
39 tim 1.5
40 dl 1.14 /**
41     * Constructor for use by subclasses.
42     */
43     protected AbstractQueue() {
44     }
45 tim 1.1
46 dholmes 1.12
47     /**
48 dl 1.14 * Adds the specified element to this queue. This implementation
49     * returns <tt>true</tt> if <tt>offer</tt> succeeds, else
50 jsr166 1.26 * throws an IllegalStateException.
51     *
52 dl 1.14 * @param o the element
53 tim 1.10 * @return <tt>true</tt> (as per the general contract of
54 dl 1.14 * <tt>Collection.add</tt>).
55 dholmes 1.9 *
56 jsr166 1.26 * @throws NullPointerException if the specified element is <tt>null</tt>.
57     * @throws IllegalStateException if the element cannot be added.
58 dholmes 1.9 */
59     public boolean add(E o) {
60     if (offer(o))
61 tim 1.1 return true;
62     else
63     throw new IllegalStateException("Queue full");
64     }
65    
66 dholmes 1.9 /**
67 dl 1.14 * Retrieves and removes the head of this queue.
68     * This implementation returns the result of <tt>poll</tt>
69     * unless the queue is empty.
70 dholmes 1.9 *
71 dl 1.14 * @return the head of this queue.
72     * @throws NoSuchElementException if this queue is empty.
73 dholmes 1.9 */
74 tim 1.8 public E remove() {
75 tim 1.1 E x = poll();
76     if (x != null)
77     return x;
78     else
79     throw new NoSuchElementException();
80     }
81    
82 dl 1.14 /**
83 jsr166 1.26 * Retrieves, but does not remove, the head of this queue.
84 dl 1.14 * This implementation returns the result of <tt>peek</tt>
85     * unless the queue is empty.
86     *
87     * @return the head of this queue.
88     * @throws NoSuchElementException if this queue is empty.
89     */
90 tim 1.8 public E element() {
91 tim 1.1 E x = peek();
92     if (x != null)
93     return x;
94     else
95     throw new NoSuchElementException();
96     }
97    
98 dholmes 1.9 /**
99 jsr166 1.26 * Removes all of the elements from this queue.
100     * The queue will be empty after this call returns.
101 dholmes 1.9 * <p>This implementation repeatedly invokes {@link #poll poll} until it
102     * returns <tt>null</tt>.
103     */
104 tim 1.1 public void clear() {
105     while (poll() != null)
106     ;
107     }
108 dl 1.19
109     /**
110     * Adds all of the elements in the specified collection to this
111     * queue. Attempts to addAll of a queue to itself result in
112     * <tt>IllegalArgumentException</tt>. Further, the behavior of
113     * this operation is undefined if the specified collection is
114     * modified while the operation is in progress.
115     *
116     * <p>This implementation iterates over the specified collection,
117     * and adds each element returned by the iterator to this
118 jsr166 1.26 * queue, in turn. A runtime exception encountered while
119 dl 1.19 * trying to add an element (including, in particular, a
120     * <tt>null</tt> element) may result in only some of the elements
121     * having been successfully added when the associated exception is
122     * thrown.
123     *
124 jsr166 1.26 * @param c collection whose elements are to be added to this queue.
125     * @return <tt>true</tt> if this queue changed as a result of the
126 dl 1.19 * call.
127 dl 1.22 * @throws NullPointerException if the specified collection or
128 jsr166 1.26 * any of its elements are null.
129 dl 1.19 * @throws IllegalArgumentException if c is this queue.
130 jsr166 1.26 *
131 dl 1.19 * @see #add(Object)
132     */
133     public boolean addAll(Collection<? extends E> c) {
134     if (c == null)
135     throw new NullPointerException();
136     if (c == this)
137     throw new IllegalArgumentException();
138 jozart 1.23 boolean modified = false;
139     Iterator<? extends E> e = c.iterator();
140     while (e.hasNext()) {
141     if (add(e.next()))
142     modified = true;
143     }
144     return modified;
145 dl 1.19 }
146    
147 dl 1.14 }