Path: blob/master/src/java.base/share/classes/java/util/AbstractQueue.java
41152 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* This file is available under and governed by the GNU General Public26* License version 2 only, as published by the Free Software Foundation.27* However, the following notice accompanied the original version of this28* file:29*30* Written by Doug Lea with assistance from members of JCP JSR-16631* Expert Group and released to the public domain, as explained at32* http://creativecommons.org/publicdomain/zero/1.0/33*/3435package java.util;3637/**38* This class provides skeletal implementations of some {@link Queue}39* operations. The implementations in this class are appropriate when40* the base implementation does <em>not</em> allow {@code null}41* elements. Methods {@link #add add}, {@link #remove remove}, and42* {@link #element element} are based on {@link #offer offer}, {@link43* #poll poll}, and {@link #peek peek}, respectively, but throw44* exceptions instead of indicating failure via {@code false} or45* {@code null} returns.46*47* <p>A {@code Queue} implementation that extends this class must48* minimally define a method {@link Queue#offer} which does not permit49* insertion of {@code null} elements, along with methods {@link50* Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and51* {@link Collection#iterator}. Typically, additional methods will be52* overridden as well. If these requirements cannot be met, consider53* instead subclassing {@link AbstractCollection}.54*55* <p>This class is a member of the56* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">57* Java Collections Framework</a>.58*59* @since 1.560* @author Doug Lea61* @param <E> the type of elements held in this queue62*/63public abstract class AbstractQueue<E>64extends AbstractCollection<E>65implements Queue<E> {6667/**68* Constructor for use by subclasses.69*/70protected AbstractQueue() {71}7273/**74* Inserts the specified element into this queue if it is possible to do so75* immediately without violating capacity restrictions, returning76* {@code true} upon success and throwing an {@code IllegalStateException}77* if no space is currently available.78*79* <p>This implementation returns {@code true} if {@code offer} succeeds,80* else throws an {@code IllegalStateException}.81*82* @param e the element to add83* @return {@code true} (as specified by {@link Collection#add})84* @throws IllegalStateException if the element cannot be added at this85* time due to capacity restrictions86* @throws ClassCastException if the class of the specified element87* prevents it from being added to this queue88* @throws NullPointerException if the specified element is null and89* this queue does not permit null elements90* @throws IllegalArgumentException if some property of this element91* prevents it from being added to this queue92*/93public boolean add(E e) {94if (offer(e))95return true;96else97throw new IllegalStateException("Queue full");98}99100/**101* Retrieves and removes the head of this queue. This method differs102* from {@link #poll poll} only in that it throws an exception if this103* queue is empty.104*105* <p>This implementation returns the result of {@code poll}106* unless the queue is empty.107*108* @return the head of this queue109* @throws NoSuchElementException if this queue is empty110*/111public E remove() {112E x = poll();113if (x != null)114return x;115else116throw new NoSuchElementException();117}118119/**120* Retrieves, but does not remove, the head of this queue. This method121* differs from {@link #peek peek} only in that it throws an exception if122* this queue is empty.123*124* <p>This implementation returns the result of {@code peek}125* unless the queue is empty.126*127* @return the head of this queue128* @throws NoSuchElementException if this queue is empty129*/130public E element() {131E x = peek();132if (x != null)133return x;134else135throw new NoSuchElementException();136}137138/**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 it143* returns {@code null}.144*/145public void clear() {146while (poll() != null)147;148}149150/**151* Adds all of the elements in the specified collection to this152* queue. Attempts to addAll of a queue to itself result in153* {@code IllegalArgumentException}. Further, the behavior of154* this operation is undefined if the specified collection is155* 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 this159* queue, in turn. A runtime exception encountered while160* trying to add an element (including, in particular, a161* {@code null} element) may result in only some of the elements162* having been successfully added when the associated exception is163* thrown.164*165* @param c collection containing elements to be added to this queue166* @return {@code true} if this queue changed as a result of the call167* @throws ClassCastException if the class of an element of the specified168* collection prevents it from being added to this queue169* @throws NullPointerException if the specified collection contains a170* null element and this queue does not permit null elements,171* or if the specified collection is null172* @throws IllegalArgumentException if some property of an element of the173* specified collection prevents it from being added to this174* queue, or if the specified collection is this queue175* @throws IllegalStateException if not all the elements can be added at176* this time due to insertion restrictions177* @see #add(Object)178*/179public boolean addAll(Collection<? extends E> c) {180if (c == null)181throw new NullPointerException();182if (c == this)183throw new IllegalArgumentException();184boolean modified = false;185for (E e : c)186if (add(e))187modified = true;188return modified;189}190191}192193194