ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractQueue.java
Revision: 1.23
Committed: Sun Nov 16 15:57:39 2003 UTC (20 years, 6 months ago) by jozart
Branch: MAIN
CVS Tags: JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.22: +7 -7 lines
Log Message:
Removed tabs from addAll method.

File Contents

# Content
1 /*
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 package java.util;
8
9 /**
10 * 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 * <tt>null</tt> returns.
18 *
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 * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and a
23 * {@link Collection#iterator} supporting {@link
24 * Iterator#remove}. Typically, additional methods will be overridden
25 * as well. If these requirements cannot be met, consider instead
26 * subclassing {@link AbstractCollection}.
27 *
28 * @since 1.5
29 * @author Doug Lea
30 * @param <E> the type of elements held in this collection
31 */
32 public abstract class AbstractQueue<E>
33 extends AbstractCollection<E>
34 implements Queue<E> {
35
36 /**
37 * Constructor for use by subclasses.
38 */
39 protected AbstractQueue() {
40 }
41
42
43 /**
44 * 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 *
48 * @param o the element
49 * @return <tt>true</tt> (as per the general contract of
50 * <tt>Collection.add</tt>).
51 *
52 * @throws NullPointerException if the specified element is <tt>null</tt>
53 * @throws IllegalStateException if element cannot be added
54 */
55 public boolean add(E o) {
56 if (offer(o))
57 return true;
58 else
59 throw new IllegalStateException("Queue full");
60 }
61
62 /**
63 * 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 *
67 * @return the head of this queue.
68 * @throws NoSuchElementException if this queue is empty.
69 */
70 public E remove() {
71 E x = poll();
72 if (x != null)
73 return x;
74 else
75 throw new NoSuchElementException();
76 }
77
78
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 public E element() {
88 E x = peek();
89 if (x != null)
90 return x;
91 else
92 throw new NoSuchElementException();
93 }
94
95 /**
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 public void clear() {
102 while (poll() != null)
103 ;
104 }
105
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 }