001 /*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
003 *
004 * This code is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU General Public License version 2 only, as
006 * published by the Free Software Foundation. Sun designates this
007 * particular file as subject to the "Classpath" exception as provided
008 * by Sun in the LICENSE file that accompanied this code.
009 *
010 * This code is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013 * version 2 for more details (a copy is included in the LICENSE file that
014 * accompanied this code).
015 *
016 * You should have received a copy of the GNU General Public License version
017 * 2 along with this work; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
019 *
020 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
021 * CA 95054 USA or visit www.sun.com if you need additional information or
022 * have any questions.
023 */
024
025 /*
026 * This file is available under and governed by the GNU General Public
027 * License version 2 only, as published by the Free Software Foundation.
028 * However, the following notice accompanied the original version of this
029 * file:
030 *
031 * Written by Doug Lea with assistance from members of JCP JSR-166
032 * Expert Group and released to the public domain, as explained at
033 * http://creativecommons.org/licenses/publicdomain
034 */
035
036 package java.util;
037
038 /**
039 * This class provides skeletal implementations of some {@link Queue}
040 * operations. The implementations in this class are appropriate when
041 * the base implementation does <em>not</em> allow <tt>null</tt>
042 * elements. Methods {@link #add add}, {@link #remove remove}, and
043 * {@link #element element} are based on {@link #offer offer}, {@link
044 * #poll poll}, and {@link #peek peek}, respectively, but throw
045 * exceptions instead of indicating failure via <tt>false</tt> or
046 * <tt>null</tt> returns.
047 *
048 * <p>A <tt>Queue</tt> implementation that extends this class must
049 * minimally define a method {@link Queue#offer} which does not permit
050 * insertion of <tt>null</tt> elements, along with methods {@link
051 * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and
052 * {@link Collection#iterator}. Typically, additional methods will be
053 * overridden as well. If these requirements cannot be met, consider
054 * instead subclassing {@link AbstractCollection}.
055 *
056 * <p>This class is a member of the
057 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
058 * Java Collections Framework</a>.
059 *
060 * @since 1.5
061 * @author Doug Lea
062 * @param <E> the type of elements held in this collection
063 */
064 public abstract class AbstractQueue<E> extends AbstractCollection<E>
065 implements Queue<E> {
066
067 /**
068 * Constructor for use by subclasses.
069 */
070 protected AbstractQueue() {
071 }
072
073 /**
074 * Inserts the specified element into this queue if it is possible to do so
075 * immediately without violating capacity restrictions, returning
076 * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
077 * if no space is currently available.
078 *
079 * <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
080 * else throws an <tt>IllegalStateException</tt>.
081 *
082 * @param e the element to add
083 * @return <tt>true</tt> (as specified by {@link Collection#add})
084 * @throws IllegalStateException if the element cannot be added at this
085 * time due to capacity restrictions
086 * @throws ClassCastException if the class of the specified element
087 * prevents it from being added to this queue
088 * @throws NullPointerException if the specified element is null and
089 * this queue does not permit null elements
090 * @throws IllegalArgumentException if some property of this element
091 * prevents it from being added to this queue
092 */
093 public boolean add(E e) {
094 if (offer(e))
095 return true;
096 else
097 throw new IllegalStateException("Queue full");
098 }
099
100 /**
101 * Retrieves and removes the head of this queue. This method differs
102 * from {@link #poll poll} only in that it throws an exception if this
103 * queue is empty.
104 *
105 * <p>This implementation returns the result of <tt>poll</tt>
106 * unless the queue is empty.
107 *
108 * @return the head of this queue
109 * @throws NoSuchElementException if this queue is empty
110 */
111 public E remove() {
112 E x = poll();
113 if (x != null)
114 return x;
115 else
116 throw new NoSuchElementException();
117 }
118
119 /**
120 * Retrieves, but does not remove, the head of this queue. This method
121 * differs from {@link #peek peek} only in that it throws an exception if
122 * this queue is empty.
123 *
124 * <p>This implementation returns the result of <tt>peek</tt>
125 * unless the queue is empty.
126 *
127 * @return the head of this queue
128 * @throws NoSuchElementException if this queue is empty
129 */
130 public E element() {
131 E x = peek();
132 if (x != null)
133 return x;
134 else
135 throw new NoSuchElementException();
136 }
137
138 /**
139 * Removes all of the elements from this queue.
140 * The queue will be empty after this call returns.
141 *
142 * <p>This implementation repeatedly invokes {@link #poll poll} until it
143 * returns <tt>null</tt>.
144 */
145 public void clear() {
146 while (poll() != null)
147 ;
148 }
149
150 /**
151 * Adds all of the elements in the specified collection to this
152 * queue. Attempts to addAll of a queue to itself result in
153 * <tt>IllegalArgumentException</tt>. Further, the behavior of
154 * this operation is undefined if the specified collection is
155 * modified while the operation is in progress.
156 *
157 * <p>This implementation iterates over the specified collection,
158 * and adds each element returned by the iterator to this
159 * queue, in turn. A runtime exception encountered while
160 * trying to add an element (including, in particular, a
161 * <tt>null</tt> element) may result in only some of the elements
162 * having been successfully added when the associated exception is
163 * thrown.
164 *
165 * @param c collection containing elements to be added to this queue
166 * @return <tt>true</tt> if this queue changed as a result of the call
167 * @throws ClassCastException if the class of an element of the specified
168 * collection prevents it from being added to this queue
169 * @throws NullPointerException if the specified collection contains a
170 * null element and this queue does not permit null elements,
171 * or if the specified collection is null
172 * @throws IllegalArgumentException if some property of an element of the
173 * specified collection prevents it from being added to this
174 * queue, or if the specified collection is this queue
175 * @throws IllegalStateException if not all the elements can be added at
176 * this time due to insertion restrictions
177 * @see #add(Object)
178 */
179 public boolean addAll(Collection<? extends E> c) {
180 if (c == null)
181 throw new NullPointerException();
182 if (c == this )
183 throw new IllegalArgumentException();
184 boolean modified = false;
185 Iterator<? extends E> e = c.iterator();
186 while (e.hasNext()) {
187 if (add(e.next()))
188 modified = true;
189 }
190 return modified;
191 }
192
193 }
|