ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractQueue.java
Revision: 1.31
Committed: Mon Jul 18 19:14:17 2005 UTC (18 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.30: +5 -4 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 jsr166 1.31 import java.util.*; // for javadoc (till 6280605 is fixed)
9 tim 1.1
10     /**
11 dl 1.14 * This class provides skeletal implementations of some {@link Queue}
12     * operations. The implementations in this class are appropriate when
13     * the base implementation does <em>not</em> allow <tt>null</tt>
14     * elements. Methods {@link #add add}, {@link #remove remove}, and
15     * {@link #element element} are based on {@link #offer offer}, {@link
16     * #poll poll}, and {@link #peek peek}, respectively but throw
17     * exceptions instead of indicating failure via <tt>false</tt> or
18 dholmes 1.9 * <tt>null</tt> returns.
19 dl 1.14 *
20     * <p> A <tt>Queue</tt> implementation that extends this class must
21     * minimally define a method {@link Queue#offer} which does not permit
22     * insertion of <tt>null</tt> elements, along with methods {@link
23 dl 1.16 * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and a
24     * {@link Collection#iterator} supporting {@link
25 dl 1.18 * Iterator#remove}. Typically, additional methods will be overridden
26 dl 1.16 * as well. If these requirements cannot be met, consider instead
27     * subclassing {@link AbstractCollection}.
28 dl 1.14 *
29 dl 1.25 * <p>This class is a member of the
30     * <a href="{@docRoot}/../guide/collections/index.html">
31     * Java Collections Framework</a>.
32 jsr166 1.26 *
33 dl 1.4 * @since 1.5
34     * @author Doug Lea
35 dl 1.21 * @param <E> the type of elements held in this collection
36 tim 1.1 */
37 tim 1.10 public abstract class AbstractQueue<E>
38     extends AbstractCollection<E>
39 dholmes 1.9 implements Queue<E> {
40 tim 1.5
41 dl 1.14 /**
42     * Constructor for use by subclasses.
43     */
44     protected AbstractQueue() {
45     }
46 tim 1.1
47 dholmes 1.12 /**
48 jsr166 1.28 * Inserts the specified element into this queue if it is possible to do so
49     * immediately without violating capacity restrictions, returning
50     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
51     * if no space is currently available.
52     *
53     * <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
54     * else throws an <tt>IllegalStateException</tt>.
55     *
56     * @param e the element to add
57 jsr166 1.31 * @return <tt>true</tt> (as specified by {@link Collection#add})
58 jsr166 1.28 * @throws IllegalStateException if the element cannot be added at this
59     * time due to capacity restrictions
60 jsr166 1.29 * @throws ClassCastException if the class of the specified element
61     * prevents it from being added to this queue
62 jsr166 1.28 * @throws NullPointerException if the specified element is null and
63     * this queue not permit null elements
64     * @throws IllegalArgumentException if some property of this element
65     * prevents it from being added to this queue
66 dholmes 1.9 */
67 jsr166 1.27 public boolean add(E e) {
68     if (offer(e))
69 tim 1.1 return true;
70     else
71     throw new IllegalStateException("Queue full");
72     }
73    
74 dholmes 1.9 /**
75 jsr166 1.28 * Retrieves and removes the head of this queue. This method differs
76 jsr166 1.31 * from {@link #poll poll} only in that it throws an exception if this
77     * queue is empty.
78 jsr166 1.28 *
79     * <p>This implementation returns the result of <tt>poll</tt>
80 dl 1.14 * unless the queue is empty.
81 dholmes 1.9 *
82 jsr166 1.28 * @return the head of this queue
83     * @throws NoSuchElementException if this queue is empty
84 dholmes 1.9 */
85 tim 1.8 public E remove() {
86 tim 1.1 E x = poll();
87     if (x != null)
88     return x;
89     else
90     throw new NoSuchElementException();
91     }
92    
93 dl 1.14 /**
94 jsr166 1.28 * Retrieves, but does not remove, the head of this queue. This method
95 jsr166 1.31 * differs from {@link #peek peek} only in that it throws an exception if
96 jsr166 1.28 * this queue is empty.
97     *
98     * <p>This implementation returns the result of <tt>peek</tt>
99 dl 1.14 * unless the queue is empty.
100     *
101 jsr166 1.28 * @return the head of this queue
102     * @throws NoSuchElementException if this queue is empty
103 dl 1.14 */
104 tim 1.8 public E element() {
105 tim 1.1 E x = peek();
106     if (x != null)
107     return x;
108     else
109     throw new NoSuchElementException();
110     }
111    
112 dholmes 1.9 /**
113 jsr166 1.26 * Removes all of the elements from this queue.
114     * The queue will be empty after this call returns.
115 jsr166 1.28 *
116 dholmes 1.9 * <p>This implementation repeatedly invokes {@link #poll poll} until it
117     * returns <tt>null</tt>.
118     */
119 tim 1.1 public void clear() {
120     while (poll() != null)
121     ;
122     }
123 dl 1.19
124     /**
125     * Adds all of the elements in the specified collection to this
126     * queue. Attempts to addAll of a queue to itself result in
127     * <tt>IllegalArgumentException</tt>. Further, the behavior of
128     * this operation is undefined if the specified collection is
129     * modified while the operation is in progress.
130     *
131     * <p>This implementation iterates over the specified collection,
132     * and adds each element returned by the iterator to this
133 jsr166 1.26 * queue, in turn. A runtime exception encountered while
134 dl 1.19 * trying to add an element (including, in particular, a
135     * <tt>null</tt> element) may result in only some of the elements
136     * having been successfully added when the associated exception is
137     * thrown.
138     *
139 jsr166 1.30 * @param c collection containing elements to be added to this queue
140 jsr166 1.28 * @return <tt>true</tt> if this queue changed as a result of the call
141 jsr166 1.29 * @throws ClassCastException if the class of an element of the specified
142     * collection prevents it from being added to this queue
143 jsr166 1.28 * @throws NullPointerException if the specified collection contains a
144     * null element and this queue does not permit null elements,
145     * or if the specified collection is null
146     * @throws IllegalArgumentException if some property of an element of the
147 jsr166 1.29 * specified collection prevents it from being added to this
148     * queue, or if the specified collection is this queue
149 jsr166 1.28 * @throws IllegalStateException if not all the elements can be added at
150     * this time due to insertion restrictions
151 dl 1.19 * @see #add(Object)
152     */
153     public boolean addAll(Collection<? extends E> c) {
154     if (c == null)
155     throw new NullPointerException();
156     if (c == this)
157     throw new IllegalArgumentException();
158 jozart 1.23 boolean modified = false;
159     Iterator<? extends E> e = c.iterator();
160     while (e.hasNext()) {
161     if (add(e.next()))
162     modified = true;
163     }
164     return modified;
165 dl 1.19 }
166    
167 dl 1.14 }