ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractQueue.java
Revision: 1.21
Committed: Sun Oct 19 13:38:29 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE
Changes since 1.20: +1 -1 lines
Log Message:
Changed doc strings for generic params

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