jaroslav@597: /* jaroslav@597: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@597: * jaroslav@597: * This code is free software; you can redistribute it and/or modify it jaroslav@597: * under the terms of the GNU General Public License version 2 only, as jaroslav@597: * published by the Free Software Foundation. Oracle designates this jaroslav@597: * particular file as subject to the "Classpath" exception as provided jaroslav@597: * by Oracle in the LICENSE file that accompanied this code. jaroslav@597: * jaroslav@597: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@597: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@597: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@597: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@597: * accompanied this code). jaroslav@597: * jaroslav@597: * You should have received a copy of the GNU General Public License version jaroslav@597: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@597: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@597: * jaroslav@597: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@597: * or visit www.oracle.com if you need additional information or have any jaroslav@597: * questions. jaroslav@597: */ jaroslav@597: jaroslav@597: /* jaroslav@597: * This file is available under and governed by the GNU General Public jaroslav@597: * License version 2 only, as published by the Free Software Foundation. jaroslav@597: * However, the following notice accompanied the original version of this jaroslav@597: * file: jaroslav@597: * jaroslav@597: * Written by Doug Lea with assistance from members of JCP JSR-166 jaroslav@597: * Expert Group and released to the public domain, as explained at jaroslav@597: * http://creativecommons.org/publicdomain/zero/1.0/ jaroslav@597: */ jaroslav@597: jaroslav@597: package java.util; jaroslav@597: jaroslav@597: /** jaroslav@597: * This class provides skeletal implementations of some {@link Queue} jaroslav@597: * operations. The implementations in this class are appropriate when jaroslav@597: * the base implementation does not allow null jaroslav@597: * elements. Methods {@link #add add}, {@link #remove remove}, and jaroslav@597: * {@link #element element} are based on {@link #offer offer}, {@link jaroslav@597: * #poll poll}, and {@link #peek peek}, respectively, but throw jaroslav@597: * exceptions instead of indicating failure via false or jaroslav@597: * null returns. jaroslav@597: * jaroslav@597: *

A Queue implementation that extends this class must jaroslav@597: * minimally define a method {@link Queue#offer} which does not permit jaroslav@597: * insertion of null elements, along with methods {@link jaroslav@597: * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and jaroslav@597: * {@link Collection#iterator}. Typically, additional methods will be jaroslav@597: * overridden as well. If these requirements cannot be met, consider jaroslav@597: * instead subclassing {@link AbstractCollection}. jaroslav@597: * jaroslav@597: *

This class is a member of the jaroslav@597: * jaroslav@597: * Java Collections Framework. jaroslav@597: * jaroslav@597: * @since 1.5 jaroslav@597: * @author Doug Lea jaroslav@597: * @param the type of elements held in this collection jaroslav@597: */ jaroslav@597: public abstract class AbstractQueue jaroslav@597: extends AbstractCollection jaroslav@597: implements Queue { jaroslav@597: jaroslav@597: /** jaroslav@597: * Constructor for use by subclasses. jaroslav@597: */ jaroslav@597: protected AbstractQueue() { jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Inserts the specified element into this queue if it is possible to do so jaroslav@597: * immediately without violating capacity restrictions, returning jaroslav@597: * true upon success and throwing an IllegalStateException jaroslav@597: * if no space is currently available. jaroslav@597: * jaroslav@597: *

This implementation returns true if offer succeeds, jaroslav@597: * else throws an IllegalStateException. jaroslav@597: * jaroslav@597: * @param e the element to add jaroslav@597: * @return true (as specified by {@link Collection#add}) jaroslav@597: * @throws IllegalStateException if the element cannot be added at this jaroslav@597: * time due to capacity restrictions jaroslav@597: * @throws ClassCastException if the class of the specified element jaroslav@597: * prevents it from being added to this queue jaroslav@597: * @throws NullPointerException if the specified element is null and jaroslav@597: * this queue does not permit null elements jaroslav@597: * @throws IllegalArgumentException if some property of this element jaroslav@597: * prevents it from being added to this queue jaroslav@597: */ jaroslav@597: public boolean add(E e) { jaroslav@597: if (offer(e)) jaroslav@597: return true; jaroslav@597: else jaroslav@597: throw new IllegalStateException("Queue full"); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Retrieves and removes the head of this queue. This method differs jaroslav@597: * from {@link #poll poll} only in that it throws an exception if this jaroslav@597: * queue is empty. jaroslav@597: * jaroslav@597: *

This implementation returns the result of poll jaroslav@597: * unless the queue is empty. jaroslav@597: * jaroslav@597: * @return the head of this queue jaroslav@597: * @throws NoSuchElementException if this queue is empty jaroslav@597: */ jaroslav@597: public E remove() { jaroslav@597: E x = poll(); jaroslav@597: if (x != null) jaroslav@597: return x; jaroslav@597: else jaroslav@597: throw new NoSuchElementException(); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Retrieves, but does not remove, the head of this queue. This method jaroslav@597: * differs from {@link #peek peek} only in that it throws an exception if jaroslav@597: * this queue is empty. jaroslav@597: * jaroslav@597: *

This implementation returns the result of peek jaroslav@597: * unless the queue is empty. jaroslav@597: * jaroslav@597: * @return the head of this queue jaroslav@597: * @throws NoSuchElementException if this queue is empty jaroslav@597: */ jaroslav@597: public E element() { jaroslav@597: E x = peek(); jaroslav@597: if (x != null) jaroslav@597: return x; jaroslav@597: else jaroslav@597: throw new NoSuchElementException(); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Removes all of the elements from this queue. jaroslav@597: * The queue will be empty after this call returns. jaroslav@597: * jaroslav@597: *

This implementation repeatedly invokes {@link #poll poll} until it jaroslav@597: * returns null. jaroslav@597: */ jaroslav@597: public void clear() { jaroslav@597: while (poll() != null) jaroslav@597: ; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Adds all of the elements in the specified collection to this jaroslav@597: * queue. Attempts to addAll of a queue to itself result in jaroslav@597: * IllegalArgumentException. Further, the behavior of jaroslav@597: * this operation is undefined if the specified collection is jaroslav@597: * modified while the operation is in progress. jaroslav@597: * jaroslav@597: *

This implementation iterates over the specified collection, jaroslav@597: * and adds each element returned by the iterator to this jaroslav@597: * queue, in turn. A runtime exception encountered while jaroslav@597: * trying to add an element (including, in particular, a jaroslav@597: * null element) may result in only some of the elements jaroslav@597: * having been successfully added when the associated exception is jaroslav@597: * thrown. jaroslav@597: * jaroslav@597: * @param c collection containing elements to be added to this queue jaroslav@597: * @return true if this queue changed as a result of the call jaroslav@597: * @throws ClassCastException if the class of an element of the specified jaroslav@597: * collection prevents it from being added to this queue jaroslav@597: * @throws NullPointerException if the specified collection contains a jaroslav@597: * null element and this queue does not permit null elements, jaroslav@597: * or if the specified collection is null jaroslav@597: * @throws IllegalArgumentException if some property of an element of the jaroslav@597: * specified collection prevents it from being added to this jaroslav@597: * queue, or if the specified collection is this queue jaroslav@597: * @throws IllegalStateException if not all the elements can be added at jaroslav@597: * this time due to insertion restrictions jaroslav@597: * @see #add(Object) jaroslav@597: */ jaroslav@597: public boolean addAll(Collection c) { jaroslav@597: if (c == null) jaroslav@597: throw new NullPointerException(); jaroslav@597: if (c == this) jaroslav@597: throw new IllegalArgumentException(); jaroslav@597: boolean modified = false; jaroslav@597: for (E e : c) jaroslav@597: if (add(e)) jaroslav@597: modified = true; jaroslav@597: return modified; jaroslav@597: } jaroslav@597: jaroslav@597: }