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