AbstractQueue.java revision ed4f365789d43b1961657195df223a19bf4ef20f
1/* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25/* 26 * This file is available under and governed by the GNU General Public 27 * License version 2 only, as published by the Free Software Foundation. 28 * However, the following notice accompanied the original version of this 29 * file: 30 * Written by Doug Lea with assistance from members of JCP JSR-166 31 * Expert Group and released to the public domain, as explained at 32 * http://creativecommons.org/publicdomain/zero/1.0/ 33 */ 34 35package java.util; 36 37// BEGIN android-note 38// removed link to collections framework docs 39// END android-note 40 41/** 42 * This class provides skeletal implementations of some {@link Queue} 43 * operations. The implementations in this class are appropriate when 44 * the base implementation does <em>not</em> allow {@code null} 45 * elements. Methods {@link #add add}, {@link #remove remove}, and 46 * {@link #element element} are based on {@link #offer offer}, {@link 47 * #poll poll}, and {@link #peek peek}, respectively, but throw 48 * exceptions instead of indicating failure via {@code false} or 49 * {@code null} returns. 50 * 51 * <p>A {@code Queue} implementation that extends this class must 52 * minimally define a method {@link Queue#offer} which does not permit 53 * insertion of {@code null} elements, along with methods {@link 54 * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and 55 * {@link Collection#iterator}. Typically, additional methods will be 56 * overridden as well. If these requirements cannot be met, consider 57 * instead subclassing {@link AbstractCollection}. 58 * 59 * @since 1.5 60 * @author Doug Lea 61 * @param <E> the type of elements held in this collection 62 */ 63public abstract class AbstractQueue<E> 64 extends AbstractCollection<E> 65 implements Queue<E> { 66 67 /** 68 * Constructor for use by subclasses. 69 */ 70 protected AbstractQueue() { 71 } 72 73 /** 74 * Inserts the specified element into this queue if it is possible to do so 75 * immediately without violating capacity restrictions, returning 76 * {@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 add 83 * @return {@code true} (as specified by {@link Collection#add}) 84 * @throws IllegalStateException if the element cannot be added at this 85 * time due to capacity restrictions 86 * @throws ClassCastException if the class of the specified element 87 * prevents it from being added to this queue 88 * @throws NullPointerException if the specified element is null and 89 * this queue does not permit null elements 90 * @throws IllegalArgumentException if some property of this element 91 * prevents it from being added to this queue 92 */ 93 public boolean add(E e) { 94 if (offer(e)) 95 return true; 96 else 97 throw new IllegalStateException("Queue full"); 98 } 99 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 {@code poll} 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 {@code peek} 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 {@code null}. 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 * {@code IllegalArgumentException}. 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 * {@code null} 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 {@code true} 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 for (E e : c) 186 if (add(e)) 187 modified = true; 188 return modified; 189 } 190 191} 192