rt/emul/compact/src/main/java/java/util/Queue.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 597 emul/compact/src/main/java/java/util/Queue.java@ee8a922f4268
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@597
     1
/*
jaroslav@597
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@597
     3
 *
jaroslav@597
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@597
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@597
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@597
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@597
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@597
     9
 *
jaroslav@597
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@597
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@597
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@597
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@597
    14
 * accompanied this code).
jaroslav@597
    15
 *
jaroslav@597
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@597
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@597
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@597
    19
 *
jaroslav@597
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@597
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@597
    22
 * questions.
jaroslav@597
    23
 */
jaroslav@597
    24
jaroslav@597
    25
/*
jaroslav@597
    26
 * This file is available under and governed by the GNU General Public
jaroslav@597
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@597
    28
 * However, the following notice accompanied the original version of this
jaroslav@597
    29
 * file:
jaroslav@597
    30
 *
jaroslav@597
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@597
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@597
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@597
    34
 */
jaroslav@597
    35
jaroslav@597
    36
package java.util;
jaroslav@597
    37
jaroslav@597
    38
/**
jaroslav@597
    39
 * A collection designed for holding elements prior to processing.
jaroslav@597
    40
 * Besides basic {@link java.util.Collection Collection} operations,
jaroslav@597
    41
 * queues provide additional insertion, extraction, and inspection
jaroslav@597
    42
 * operations.  Each of these methods exists in two forms: one throws
jaroslav@597
    43
 * an exception if the operation fails, the other returns a special
jaroslav@597
    44
 * value (either <tt>null</tt> or <tt>false</tt>, depending on the
jaroslav@597
    45
 * operation).  The latter form of the insert operation is designed
jaroslav@597
    46
 * specifically for use with capacity-restricted <tt>Queue</tt>
jaroslav@597
    47
 * implementations; in most implementations, insert operations cannot
jaroslav@597
    48
 * fail.
jaroslav@597
    49
 *
jaroslav@597
    50
 * <p>
jaroslav@597
    51
 * <table BORDER CELLPADDING=3 CELLSPACING=1>
jaroslav@597
    52
 *  <tr>
jaroslav@597
    53
 *    <td></td>
jaroslav@597
    54
 *    <td ALIGN=CENTER><em>Throws exception</em></td>
jaroslav@597
    55
 *    <td ALIGN=CENTER><em>Returns special value</em></td>
jaroslav@597
    56
 *  </tr>
jaroslav@597
    57
 *  <tr>
jaroslav@597
    58
 *    <td><b>Insert</b></td>
jaroslav@597
    59
 *    <td>{@link #add add(e)}</td>
jaroslav@597
    60
 *    <td>{@link #offer offer(e)}</td>
jaroslav@597
    61
 *  </tr>
jaroslav@597
    62
 *  <tr>
jaroslav@597
    63
 *    <td><b>Remove</b></td>
jaroslav@597
    64
 *    <td>{@link #remove remove()}</td>
jaroslav@597
    65
 *    <td>{@link #poll poll()}</td>
jaroslav@597
    66
 *  </tr>
jaroslav@597
    67
 *  <tr>
jaroslav@597
    68
 *    <td><b>Examine</b></td>
jaroslav@597
    69
 *    <td>{@link #element element()}</td>
jaroslav@597
    70
 *    <td>{@link #peek peek()}</td>
jaroslav@597
    71
 *  </tr>
jaroslav@597
    72
 * </table>
jaroslav@597
    73
 *
jaroslav@597
    74
 * <p>Queues typically, but do not necessarily, order elements in a
jaroslav@597
    75
 * FIFO (first-in-first-out) manner.  Among the exceptions are
jaroslav@597
    76
 * priority queues, which order elements according to a supplied
jaroslav@597
    77
 * comparator, or the elements' natural ordering, and LIFO queues (or
jaroslav@597
    78
 * stacks) which order the elements LIFO (last-in-first-out).
jaroslav@597
    79
 * Whatever the ordering used, the <em>head</em> of the queue is that
jaroslav@597
    80
 * element which would be removed by a call to {@link #remove() } or
jaroslav@597
    81
 * {@link #poll()}.  In a FIFO queue, all new elements are inserted at
jaroslav@597
    82
 * the <em> tail</em> of the queue. Other kinds of queues may use
jaroslav@597
    83
 * different placement rules.  Every <tt>Queue</tt> implementation
jaroslav@597
    84
 * must specify its ordering properties.
jaroslav@597
    85
 *
jaroslav@597
    86
 * <p>The {@link #offer offer} method inserts an element if possible,
jaroslav@597
    87
 * otherwise returning <tt>false</tt>.  This differs from the {@link
jaroslav@597
    88
 * java.util.Collection#add Collection.add} method, which can fail to
jaroslav@597
    89
 * add an element only by throwing an unchecked exception.  The
jaroslav@597
    90
 * <tt>offer</tt> method is designed for use when failure is a normal,
jaroslav@597
    91
 * rather than exceptional occurrence, for example, in fixed-capacity
jaroslav@597
    92
 * (or &quot;bounded&quot;) queues.
jaroslav@597
    93
 *
jaroslav@597
    94
 * <p>The {@link #remove()} and {@link #poll()} methods remove and
jaroslav@597
    95
 * return the head of the queue.
jaroslav@597
    96
 * Exactly which element is removed from the queue is a
jaroslav@597
    97
 * function of the queue's ordering policy, which differs from
jaroslav@597
    98
 * implementation to implementation. The <tt>remove()</tt> and
jaroslav@597
    99
 * <tt>poll()</tt> methods differ only in their behavior when the
jaroslav@597
   100
 * queue is empty: the <tt>remove()</tt> method throws an exception,
jaroslav@597
   101
 * while the <tt>poll()</tt> method returns <tt>null</tt>.
jaroslav@597
   102
 *
jaroslav@597
   103
 * <p>The {@link #element()} and {@link #peek()} methods return, but do
jaroslav@597
   104
 * not remove, the head of the queue.
jaroslav@597
   105
 *
jaroslav@597
   106
 * <p>The <tt>Queue</tt> interface does not define the <i>blocking queue
jaroslav@597
   107
 * methods</i>, which are common in concurrent programming.  These methods,
jaroslav@597
   108
 * which wait for elements to appear or for space to become available, are
jaroslav@597
   109
 * defined in the {@link java.util.concurrent.BlockingQueue} interface, which
jaroslav@597
   110
 * extends this interface.
jaroslav@597
   111
 *
jaroslav@597
   112
 * <p><tt>Queue</tt> implementations generally do not allow insertion
jaroslav@597
   113
 * of <tt>null</tt> elements, although some implementations, such as
jaroslav@597
   114
 * {@link LinkedList}, do not prohibit insertion of <tt>null</tt>.
jaroslav@597
   115
 * Even in the implementations that permit it, <tt>null</tt> should
jaroslav@597
   116
 * not be inserted into a <tt>Queue</tt>, as <tt>null</tt> is also
jaroslav@597
   117
 * used as a special return value by the <tt>poll</tt> method to
jaroslav@597
   118
 * indicate that the queue contains no elements.
jaroslav@597
   119
 *
jaroslav@597
   120
 * <p><tt>Queue</tt> implementations generally do not define
jaroslav@597
   121
 * element-based versions of methods <tt>equals</tt> and
jaroslav@597
   122
 * <tt>hashCode</tt> but instead inherit the identity based versions
jaroslav@597
   123
 * from class <tt>Object</tt>, because element-based equality is not
jaroslav@597
   124
 * always well-defined for queues with the same elements but different
jaroslav@597
   125
 * ordering properties.
jaroslav@597
   126
 *
jaroslav@597
   127
 *
jaroslav@597
   128
 * <p>This interface is a member of the
jaroslav@597
   129
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@597
   130
 * Java Collections Framework</a>.
jaroslav@597
   131
 *
jaroslav@597
   132
 * @see java.util.Collection
jaroslav@597
   133
 * @see LinkedList
jaroslav@597
   134
 * @see PriorityQueue
jaroslav@597
   135
 * @see java.util.concurrent.LinkedBlockingQueue
jaroslav@597
   136
 * @see java.util.concurrent.BlockingQueue
jaroslav@597
   137
 * @see java.util.concurrent.ArrayBlockingQueue
jaroslav@597
   138
 * @see java.util.concurrent.LinkedBlockingQueue
jaroslav@597
   139
 * @see java.util.concurrent.PriorityBlockingQueue
jaroslav@597
   140
 * @since 1.5
jaroslav@597
   141
 * @author Doug Lea
jaroslav@597
   142
 * @param <E> the type of elements held in this collection
jaroslav@597
   143
 */
jaroslav@597
   144
public interface Queue<E> extends Collection<E> {
jaroslav@597
   145
    /**
jaroslav@597
   146
     * Inserts the specified element into this queue if it is possible to do so
jaroslav@597
   147
     * immediately without violating capacity restrictions, returning
jaroslav@597
   148
     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
jaroslav@597
   149
     * if no space is currently available.
jaroslav@597
   150
     *
jaroslav@597
   151
     * @param e the element to add
jaroslav@597
   152
     * @return <tt>true</tt> (as specified by {@link Collection#add})
jaroslav@597
   153
     * @throws IllegalStateException if the element cannot be added at this
jaroslav@597
   154
     *         time due to capacity restrictions
jaroslav@597
   155
     * @throws ClassCastException if the class of the specified element
jaroslav@597
   156
     *         prevents it from being added to this queue
jaroslav@597
   157
     * @throws NullPointerException if the specified element is null and
jaroslav@597
   158
     *         this queue does not permit null elements
jaroslav@597
   159
     * @throws IllegalArgumentException if some property of this element
jaroslav@597
   160
     *         prevents it from being added to this queue
jaroslav@597
   161
     */
jaroslav@597
   162
    boolean add(E e);
jaroslav@597
   163
jaroslav@597
   164
    /**
jaroslav@597
   165
     * Inserts the specified element into this queue if it is possible to do
jaroslav@597
   166
     * so immediately without violating capacity restrictions.
jaroslav@597
   167
     * When using a capacity-restricted queue, this method is generally
jaroslav@597
   168
     * preferable to {@link #add}, which can fail to insert an element only
jaroslav@597
   169
     * by throwing an exception.
jaroslav@597
   170
     *
jaroslav@597
   171
     * @param e the element to add
jaroslav@597
   172
     * @return <tt>true</tt> if the element was added to this queue, else
jaroslav@597
   173
     *         <tt>false</tt>
jaroslav@597
   174
     * @throws ClassCastException if the class of the specified element
jaroslav@597
   175
     *         prevents it from being added to this queue
jaroslav@597
   176
     * @throws NullPointerException if the specified element is null and
jaroslav@597
   177
     *         this queue does not permit null elements
jaroslav@597
   178
     * @throws IllegalArgumentException if some property of this element
jaroslav@597
   179
     *         prevents it from being added to this queue
jaroslav@597
   180
     */
jaroslav@597
   181
    boolean offer(E e);
jaroslav@597
   182
jaroslav@597
   183
    /**
jaroslav@597
   184
     * Retrieves and removes the head of this queue.  This method differs
jaroslav@597
   185
     * from {@link #poll poll} only in that it throws an exception if this
jaroslav@597
   186
     * queue is empty.
jaroslav@597
   187
     *
jaroslav@597
   188
     * @return the head of this queue
jaroslav@597
   189
     * @throws NoSuchElementException if this queue is empty
jaroslav@597
   190
     */
jaroslav@597
   191
    E remove();
jaroslav@597
   192
jaroslav@597
   193
    /**
jaroslav@597
   194
     * Retrieves and removes the head of this queue,
jaroslav@597
   195
     * or returns <tt>null</tt> if this queue is empty.
jaroslav@597
   196
     *
jaroslav@597
   197
     * @return the head of this queue, or <tt>null</tt> if this queue is empty
jaroslav@597
   198
     */
jaroslav@597
   199
    E poll();
jaroslav@597
   200
jaroslav@597
   201
    /**
jaroslav@597
   202
     * Retrieves, but does not remove, the head of this queue.  This method
jaroslav@597
   203
     * differs from {@link #peek peek} only in that it throws an exception
jaroslav@597
   204
     * if this queue is empty.
jaroslav@597
   205
     *
jaroslav@597
   206
     * @return the head of this queue
jaroslav@597
   207
     * @throws NoSuchElementException if this queue is empty
jaroslav@597
   208
     */
jaroslav@597
   209
    E element();
jaroslav@597
   210
jaroslav@597
   211
    /**
jaroslav@597
   212
     * Retrieves, but does not remove, the head of this queue,
jaroslav@597
   213
     * or returns <tt>null</tt> if this queue is empty.
jaroslav@597
   214
     *
jaroslav@597
   215
     * @return the head of this queue, or <tt>null</tt> if this queue is empty
jaroslav@597
   216
     */
jaroslav@597
   217
    E peek();
jaroslav@597
   218
}