ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractQueue.java
Revision: 1.32
Committed: Tue Feb 7 20:54:24 2006 UTC (18 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.31: +0 -1 lines
Log Message:
6378729: Remove workaround for 6280605

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 jsr166 1.28 * Inserts the specified element into this queue if it is possible to do so
48     * immediately without violating capacity restrictions, returning
49     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
50     * if no space is currently available.
51     *
52     * <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
53     * else throws an <tt>IllegalStateException</tt>.
54     *
55     * @param e the element to add
56 jsr166 1.31 * @return <tt>true</tt> (as specified by {@link Collection#add})
57 jsr166 1.28 * @throws IllegalStateException if the element cannot be added at this
58     * time due to capacity restrictions
59 jsr166 1.29 * @throws ClassCastException if the class of the specified element
60     * prevents it from being added to this queue
61 jsr166 1.28 * @throws NullPointerException if the specified element is null and
62     * this queue not permit null elements
63     * @throws IllegalArgumentException if some property of this element
64     * prevents it from being added to this queue
65 dholmes 1.9 */
66 jsr166 1.27 public boolean add(E e) {
67     if (offer(e))
68 tim 1.1 return true;
69     else
70     throw new IllegalStateException("Queue full");
71     }
72    
73 dholmes 1.9 /**
74 jsr166 1.28 * Retrieves and removes the head of this queue. This method differs
75 jsr166 1.31 * from {@link #poll poll} only in that it throws an exception if this
76     * queue is empty.
77 jsr166 1.28 *
78     * <p>This implementation returns the result of <tt>poll</tt>
79 dl 1.14 * unless the queue is empty.
80 dholmes 1.9 *
81 jsr166 1.28 * @return the head of this queue
82     * @throws NoSuchElementException if this queue is empty
83 dholmes 1.9 */
84 tim 1.8 public E remove() {
85 tim 1.1 E x = poll();
86     if (x != null)
87     return x;
88     else
89     throw new NoSuchElementException();
90     }
91    
92 dl 1.14 /**
93 jsr166 1.28 * Retrieves, but does not remove, the head of this queue. This method
94 jsr166 1.31 * differs from {@link #peek peek} only in that it throws an exception if
95 jsr166 1.28 * this queue is empty.
96     *
97     * <p>This implementation returns the result of <tt>peek</tt>
98 dl 1.14 * unless the queue is empty.
99     *
100 jsr166 1.28 * @return the head of this queue
101     * @throws NoSuchElementException if this queue is empty
102 dl 1.14 */
103 tim 1.8 public E element() {
104 tim 1.1 E x = peek();
105     if (x != null)
106     return x;
107     else
108     throw new NoSuchElementException();
109     }
110    
111 dholmes 1.9 /**
112 jsr166 1.26 * Removes all of the elements from this queue.
113     * The queue will be empty after this call returns.
114 jsr166 1.28 *
115 dholmes 1.9 * <p>This implementation repeatedly invokes {@link #poll poll} until it
116     * returns <tt>null</tt>.
117     */
118 tim 1.1 public void clear() {
119     while (poll() != null)
120     ;
121     }
122 dl 1.19
123     /**
124     * Adds all of the elements in the specified collection to this
125     * queue. Attempts to addAll of a queue to itself result in
126     * <tt>IllegalArgumentException</tt>. Further, the behavior of
127     * this operation is undefined if the specified collection is
128     * modified while the operation is in progress.
129     *
130     * <p>This implementation iterates over the specified collection,
131     * and adds each element returned by the iterator to this
132 jsr166 1.26 * queue, in turn. A runtime exception encountered while
133 dl 1.19 * trying to add an element (including, in particular, a
134     * <tt>null</tt> element) may result in only some of the elements
135     * having been successfully added when the associated exception is
136     * thrown.
137     *
138 jsr166 1.30 * @param c collection containing elements to be added to this queue
139 jsr166 1.28 * @return <tt>true</tt> if this queue changed as a result of the call
140 jsr166 1.29 * @throws ClassCastException if the class of an element of the specified
141     * collection prevents it from being added to this queue
142 jsr166 1.28 * @throws NullPointerException if the specified collection contains a
143     * null element and this queue does not permit null elements,
144     * or if the specified collection is null
145     * @throws IllegalArgumentException if some property of an element of the
146 jsr166 1.29 * specified collection prevents it from being added to this
147     * queue, or if the specified collection is this queue
148 jsr166 1.28 * @throws IllegalStateException if not all the elements can be added at
149     * this time due to insertion restrictions
150 dl 1.19 * @see #add(Object)
151     */
152     public boolean addAll(Collection<? extends E> c) {
153     if (c == null)
154     throw new NullPointerException();
155     if (c == this)
156     throw new IllegalArgumentException();
157 jozart 1.23 boolean modified = false;
158     Iterator<? extends E> e = c.iterator();
159     while (e.hasNext()) {
160     if (add(e.next()))
161     modified = true;
162     }
163     return modified;
164 dl 1.19 }
165    
166 dl 1.14 }