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 * 31 * Written by Doug Lea with assistance from members of JCP JSR-166 32 * Expert Group and released to the public domain, as explained at 33 * http://creativecommons.org/publicdomain/zero/1.0/ 34 */ 35 36 package java.util; 37 38 // BEGIN android-note 39 // removed link to collections framework docs 40 // END android-note 41 42 /** 43 * A collection designed for holding elements prior to processing. 44 * Besides basic {@link Collection} operations, queues provide 45 * additional insertion, extraction, and inspection operations. 46 * Each of these methods exists in two forms: one throws an exception 47 * if the operation fails, the other returns a special value (either 48 * {@code null} or {@code false}, depending on the operation). The 49 * latter form of the insert operation is designed specifically for 50 * use with capacity-restricted {@code Queue} implementations; in most 51 * implementations, insert operations cannot fail. 52 * 53 * <table class="striped"> 54 * <caption>Summary of Queue methods</caption> 55 * <thead> 56 * <tr> 57 * <td></td> 58 * <th scope="col" style="font-weight:normal; font-style:italic">Throws exception</th> 59 * <th scope="col" style="font-weight:normal; font-style:italic">Returns special value</th> 60 * </tr> 61 * </thead> 62 * <tbody> 63 * <tr> 64 * <th scope="row">Insert</th> 65 * <td>{@link #add(Object) add(e)}</td> 66 * <td>{@link #offer(Object) offer(e)}</td> 67 * </tr> 68 * <tr> 69 * <th scope="row">Remove</th> 70 * <td>{@link #remove() remove()}</td> 71 * <td>{@link #poll() poll()}</td> 72 * </tr> 73 * <tr> 74 * <th scope="row">Examine</th> 75 * <td>{@link #element() element()}</td> 76 * <td>{@link #peek() peek()}</td> 77 * </tr> 78 * </tbody> 79 * </table> 80 * 81 * <p>Queues typically, but do not necessarily, order elements in a 82 * FIFO (first-in-first-out) manner. Among the exceptions are 83 * priority queues, which order elements according to a supplied 84 * comparator, or the elements' natural ordering, and LIFO queues (or 85 * stacks) which order the elements LIFO (last-in-first-out). 86 * Whatever the ordering used, the <em>head</em> of the queue is that 87 * element which would be removed by a call to {@link #remove()} or 88 * {@link #poll()}. In a FIFO queue, all new elements are inserted at 89 * the <em>tail</em> of the queue. Other kinds of queues may use 90 * different placement rules. Every {@code Queue} implementation 91 * must specify its ordering properties. 92 * 93 * <p>The {@link #offer offer} method inserts an element if possible, 94 * otherwise returning {@code false}. This differs from the {@link 95 * java.util.Collection#add Collection.add} method, which can fail to 96 * add an element only by throwing an unchecked exception. The 97 * {@code offer} method is designed for use when failure is a normal, 98 * rather than exceptional occurrence, for example, in fixed-capacity 99 * (or "bounded") queues. 100 * 101 * <p>The {@link #remove()} and {@link #poll()} methods remove and 102 * return the head of the queue. 103 * Exactly which element is removed from the queue is a 104 * function of the queue's ordering policy, which differs from 105 * implementation to implementation. The {@code remove()} and 106 * {@code poll()} methods differ only in their behavior when the 107 * queue is empty: the {@code remove()} method throws an exception, 108 * while the {@code poll()} method returns {@code null}. 109 * 110 * <p>The {@link #element()} and {@link #peek()} methods return, but do 111 * not remove, the head of the queue. 112 * 113 * <p>The {@code Queue} interface does not define the <i>blocking queue 114 * methods</i>, which are common in concurrent programming. These methods, 115 * which wait for elements to appear or for space to become available, are 116 * defined in the {@link java.util.concurrent.BlockingQueue} interface, which 117 * extends this interface. 118 * 119 * <p>{@code Queue} implementations generally do not allow insertion 120 * of {@code null} elements, although some implementations, such as 121 * {@link LinkedList}, do not prohibit insertion of {@code null}. 122 * Even in the implementations that permit it, {@code null} should 123 * not be inserted into a {@code Queue}, as {@code null} is also 124 * used as a special return value by the {@code poll} method to 125 * indicate that the queue contains no elements. 126 * 127 * <p>{@code Queue} implementations generally do not define 128 * element-based versions of methods {@code equals} and 129 * {@code hashCode} but instead inherit the identity based versions 130 * from class {@code Object}, because element-based equality is not 131 * always well-defined for queues with the same elements but different 132 * ordering properties. 133 * 134 * @since 1.5 135 * @author Doug Lea 136 * @param <E> the type of elements held in this queue 137 */ 138 public interface Queue<E> extends Collection<E> { 139 /** 140 * Inserts the specified element into this queue if it is possible to do so 141 * immediately without violating capacity restrictions, returning 142 * {@code true} upon success and throwing an {@code IllegalStateException} 143 * if no space is currently available. 144 * 145 * @param e the element to add 146 * @return {@code true} (as specified by {@link Collection#add}) 147 * @throws IllegalStateException if the element cannot be added at this 148 * time due to capacity restrictions 149 * @throws ClassCastException if the class of the specified element 150 * prevents it from being added to this queue 151 * @throws NullPointerException if the specified element is null and 152 * this queue does not permit null elements 153 * @throws IllegalArgumentException if some property of this element 154 * prevents it from being added to this queue 155 */ add(E e)156 boolean add(E e); 157 158 /** 159 * Inserts the specified element into this queue if it is possible to do 160 * so immediately without violating capacity restrictions. 161 * When using a capacity-restricted queue, this method is generally 162 * preferable to {@link #add}, which can fail to insert an element only 163 * by throwing an exception. 164 * 165 * @param e the element to add 166 * @return {@code true} if the element was added to this queue, else 167 * {@code false} 168 * @throws ClassCastException if the class of the specified element 169 * prevents it from being added to this queue 170 * @throws NullPointerException if the specified element is null and 171 * this queue does not permit null elements 172 * @throws IllegalArgumentException if some property of this element 173 * prevents it from being added to this queue 174 */ offer(E e)175 boolean offer(E e); 176 177 /** 178 * Retrieves and removes the head of this queue. This method differs 179 * from {@link #poll() poll()} only in that it throws an exception if 180 * this queue is empty. 181 * 182 * @return the head of this queue 183 * @throws NoSuchElementException if this queue is empty 184 */ remove()185 E remove(); 186 187 /** 188 * Retrieves and removes the head of this queue, 189 * or returns {@code null} if this queue is empty. 190 * 191 * @return the head of this queue, or {@code null} if this queue is empty 192 */ poll()193 E poll(); 194 195 /** 196 * Retrieves, but does not remove, the head of this queue. This method 197 * differs from {@link #peek peek} only in that it throws an exception 198 * if this queue is empty. 199 * 200 * @return the head of this queue 201 * @throws NoSuchElementException if this queue is empty 202 */ element()203 E element(); 204 205 /** 206 * Retrieves, but does not remove, the head of this queue, 207 * or returns {@code null} if this queue is empty. 208 * 209 * @return the head of this queue, or {@code null} if this queue is empty 210 */ peek()211 E peek(); 212 } 213