ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractQueue.java
Revision: 1.19
Committed: Sun Oct 5 22:59:21 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.18: +39 -0 lines
Log Message:
addAll(self) throws exception; javadoc touchups

File Contents

# User Rev Content
1 dl 1.2 /*
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 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.4 * @since 1.5
29     * @author Doug Lea
30 tim 1.1 */
31 tim 1.10 public abstract class AbstractQueue<E>
32     extends AbstractCollection<E>
33 dholmes 1.9 implements Queue<E> {
34 tim 1.5
35 dl 1.14 /**
36     * Constructor for use by subclasses.
37     */
38     protected AbstractQueue() {
39     }
40 tim 1.1
41 dholmes 1.12
42     /**
43 dl 1.14 * Adds the specified element to this queue. This implementation
44     * returns <tt>true</tt> if <tt>offer</tt> succeeds, else
45     * throws an IllegalStateException.
46 dl 1.17 *
47 dl 1.14 * @param o the element
48 tim 1.10 * @return <tt>true</tt> (as per the general contract of
49 dl 1.14 * <tt>Collection.add</tt>).
50 dholmes 1.9 *
51 dholmes 1.11 * @throws NullPointerException if the specified element is <tt>null</tt>
52 dl 1.14 * @throws IllegalStateException if element cannot be added
53 dholmes 1.9 */
54     public boolean add(E o) {
55     if (offer(o))
56 tim 1.1 return true;
57     else
58     throw new IllegalStateException("Queue full");
59     }
60    
61 dholmes 1.9 /**
62 dl 1.14 * Retrieves and removes the head of this queue.
63     * This implementation returns the result of <tt>poll</tt>
64     * unless the queue is empty.
65 dholmes 1.9 *
66 dl 1.14 * @return the head of this queue.
67     * @throws NoSuchElementException if this queue is empty.
68 dholmes 1.9 */
69 tim 1.8 public E remove() {
70 tim 1.1 E x = poll();
71     if (x != null)
72     return x;
73     else
74     throw new NoSuchElementException();
75     }
76    
77 dl 1.14
78     /**
79     * Retrieves, but does not remove, the head of this queue.
80     * This implementation returns the result of <tt>peek</tt>
81     * unless the queue is empty.
82     *
83     * @return the head of this queue.
84     * @throws NoSuchElementException if this queue is empty.
85     */
86 tim 1.8 public E element() {
87 tim 1.1 E x = peek();
88     if (x != null)
89     return x;
90     else
91     throw new NoSuchElementException();
92     }
93    
94 dholmes 1.9 /**
95     * Removes all of the elements from this collection.
96     * The collection will be empty after this call returns.
97     * <p>This implementation repeatedly invokes {@link #poll poll} until it
98     * returns <tt>null</tt>.
99     */
100 tim 1.1 public void clear() {
101     while (poll() != null)
102     ;
103     }
104 dl 1.19
105     /**
106     * Adds all of the elements in the specified collection to this
107     * queue. Attempts to addAll of a queue to itself result in
108     * <tt>IllegalArgumentException</tt>. Further, the behavior of
109     * this operation is undefined if the specified collection is
110     * modified while the operation is in progress.
111     *
112     * <p>This implementation iterates over the specified collection,
113     * and adds each element returned by the iterator to this
114     * collection, in turn. A runtime exception encountered while
115     * trying to add an element (including, in particular, a
116     * <tt>null</tt> element) may result in only some of the elements
117     * having been successfully added when the associated exception is
118     * thrown.
119     *
120     * @param c collection whose elements are to be added to this collection.
121     * @return <tt>true</tt> if this collection changed as a result of the
122     * call.
123     * @throws NullPointerException if the specified collection, or
124     * any of its elements are null.
125     * @throws IllegalArgumentException if c is this queue.
126     *
127     * @see #add(Object)
128     */
129     public boolean addAll(Collection<? extends E> c) {
130     if (c == null)
131     throw new NullPointerException();
132     if (c == this)
133     throw new IllegalArgumentException();
134     boolean modified = false;
135     Iterator<? extends E> e = c.iterator();
136     while (e.hasNext()) {
137     if (add(e.next()))
138     modified = true;
139     }
140     return modified;
141     }
142    
143 dl 1.14 }