rt/emul/compact/src/main/java/java/util/concurrent/BlockingDeque.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 10:46:31 +0100
branchjdk7-b147
changeset 1890 212417b74b72
permissions -rw-r--r--
Bringing in all concurrent package from JDK7-b147
jaroslav@1890
     1
/*
jaroslav@1890
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1890
     3
 *
jaroslav@1890
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1890
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1890
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1890
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1890
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1890
     9
 *
jaroslav@1890
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1890
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1890
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1890
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1890
    14
 * accompanied this code).
jaroslav@1890
    15
 *
jaroslav@1890
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@1890
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1890
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1890
    19
 *
jaroslav@1890
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1890
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1890
    22
 * questions.
jaroslav@1890
    23
 */
jaroslav@1890
    24
jaroslav@1890
    25
/*
jaroslav@1890
    26
 * This file is available under and governed by the GNU General Public
jaroslav@1890
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@1890
    28
 * However, the following notice accompanied the original version of this
jaroslav@1890
    29
 * file:
jaroslav@1890
    30
 *
jaroslav@1890
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@1890
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@1890
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1890
    34
 */
jaroslav@1890
    35
jaroslav@1890
    36
package java.util.concurrent;
jaroslav@1890
    37
import java.util.*;
jaroslav@1890
    38
jaroslav@1890
    39
/**
jaroslav@1890
    40
 * A {@link Deque} that additionally supports blocking operations that wait
jaroslav@1890
    41
 * for the deque to become non-empty when retrieving an element, and wait for
jaroslav@1890
    42
 * space to become available in the deque when storing an element.
jaroslav@1890
    43
 *
jaroslav@1890
    44
 * <p><tt>BlockingDeque</tt> methods come in four forms, with different ways
jaroslav@1890
    45
 * of handling operations that cannot be satisfied immediately, but may be
jaroslav@1890
    46
 * satisfied at some point in the future:
jaroslav@1890
    47
 * one throws an exception, the second returns a special value (either
jaroslav@1890
    48
 * <tt>null</tt> or <tt>false</tt>, depending on the operation), the third
jaroslav@1890
    49
 * blocks the current thread indefinitely until the operation can succeed,
jaroslav@1890
    50
 * and the fourth blocks for only a given maximum time limit before giving
jaroslav@1890
    51
 * up.  These methods are summarized in the following table:
jaroslav@1890
    52
 *
jaroslav@1890
    53
 * <p>
jaroslav@1890
    54
 * <table BORDER CELLPADDING=3 CELLSPACING=1>
jaroslav@1890
    55
 *  <tr>
jaroslav@1890
    56
 *    <td ALIGN=CENTER COLSPAN = 5> <b>First Element (Head)</b></td>
jaroslav@1890
    57
 *  </tr>
jaroslav@1890
    58
 *  <tr>
jaroslav@1890
    59
 *    <td></td>
jaroslav@1890
    60
 *    <td ALIGN=CENTER><em>Throws exception</em></td>
jaroslav@1890
    61
 *    <td ALIGN=CENTER><em>Special value</em></td>
jaroslav@1890
    62
 *    <td ALIGN=CENTER><em>Blocks</em></td>
jaroslav@1890
    63
 *    <td ALIGN=CENTER><em>Times out</em></td>
jaroslav@1890
    64
 *  </tr>
jaroslav@1890
    65
 *  <tr>
jaroslav@1890
    66
 *    <td><b>Insert</b></td>
jaroslav@1890
    67
 *    <td>{@link #addFirst addFirst(e)}</td>
jaroslav@1890
    68
 *    <td>{@link #offerFirst(Object) offerFirst(e)}</td>
jaroslav@1890
    69
 *    <td>{@link #putFirst putFirst(e)}</td>
jaroslav@1890
    70
 *    <td>{@link #offerFirst(Object, long, TimeUnit) offerFirst(e, time, unit)}</td>
jaroslav@1890
    71
 *  </tr>
jaroslav@1890
    72
 *  <tr>
jaroslav@1890
    73
 *    <td><b>Remove</b></td>
jaroslav@1890
    74
 *    <td>{@link #removeFirst removeFirst()}</td>
jaroslav@1890
    75
 *    <td>{@link #pollFirst pollFirst()}</td>
jaroslav@1890
    76
 *    <td>{@link #takeFirst takeFirst()}</td>
jaroslav@1890
    77
 *    <td>{@link #pollFirst(long, TimeUnit) pollFirst(time, unit)}</td>
jaroslav@1890
    78
 *  </tr>
jaroslav@1890
    79
 *  <tr>
jaroslav@1890
    80
 *    <td><b>Examine</b></td>
jaroslav@1890
    81
 *    <td>{@link #getFirst getFirst()}</td>
jaroslav@1890
    82
 *    <td>{@link #peekFirst peekFirst()}</td>
jaroslav@1890
    83
 *    <td><em>not applicable</em></td>
jaroslav@1890
    84
 *    <td><em>not applicable</em></td>
jaroslav@1890
    85
 *  </tr>
jaroslav@1890
    86
 *  <tr>
jaroslav@1890
    87
 *    <td ALIGN=CENTER COLSPAN = 5> <b>Last Element (Tail)</b></td>
jaroslav@1890
    88
 *  </tr>
jaroslav@1890
    89
 *  <tr>
jaroslav@1890
    90
 *    <td></td>
jaroslav@1890
    91
 *    <td ALIGN=CENTER><em>Throws exception</em></td>
jaroslav@1890
    92
 *    <td ALIGN=CENTER><em>Special value</em></td>
jaroslav@1890
    93
 *    <td ALIGN=CENTER><em>Blocks</em></td>
jaroslav@1890
    94
 *    <td ALIGN=CENTER><em>Times out</em></td>
jaroslav@1890
    95
 *  </tr>
jaroslav@1890
    96
 *  <tr>
jaroslav@1890
    97
 *    <td><b>Insert</b></td>
jaroslav@1890
    98
 *    <td>{@link #addLast addLast(e)}</td>
jaroslav@1890
    99
 *    <td>{@link #offerLast(Object) offerLast(e)}</td>
jaroslav@1890
   100
 *    <td>{@link #putLast putLast(e)}</td>
jaroslav@1890
   101
 *    <td>{@link #offerLast(Object, long, TimeUnit) offerLast(e, time, unit)}</td>
jaroslav@1890
   102
 *  </tr>
jaroslav@1890
   103
 *  <tr>
jaroslav@1890
   104
 *    <td><b>Remove</b></td>
jaroslav@1890
   105
 *    <td>{@link #removeLast() removeLast()}</td>
jaroslav@1890
   106
 *    <td>{@link #pollLast() pollLast()}</td>
jaroslav@1890
   107
 *    <td>{@link #takeLast takeLast()}</td>
jaroslav@1890
   108
 *    <td>{@link #pollLast(long, TimeUnit) pollLast(time, unit)}</td>
jaroslav@1890
   109
 *  </tr>
jaroslav@1890
   110
 *  <tr>
jaroslav@1890
   111
 *    <td><b>Examine</b></td>
jaroslav@1890
   112
 *    <td>{@link #getLast getLast()}</td>
jaroslav@1890
   113
 *    <td>{@link #peekLast peekLast()}</td>
jaroslav@1890
   114
 *    <td><em>not applicable</em></td>
jaroslav@1890
   115
 *    <td><em>not applicable</em></td>
jaroslav@1890
   116
 *  </tr>
jaroslav@1890
   117
 * </table>
jaroslav@1890
   118
 *
jaroslav@1890
   119
 * <p>Like any {@link BlockingQueue}, a <tt>BlockingDeque</tt> is thread safe,
jaroslav@1890
   120
 * does not permit null elements, and may (or may not) be
jaroslav@1890
   121
 * capacity-constrained.
jaroslav@1890
   122
 *
jaroslav@1890
   123
 * <p>A <tt>BlockingDeque</tt> implementation may be used directly as a FIFO
jaroslav@1890
   124
 * <tt>BlockingQueue</tt>. The methods inherited from the
jaroslav@1890
   125
 * <tt>BlockingQueue</tt> interface are precisely equivalent to
jaroslav@1890
   126
 * <tt>BlockingDeque</tt> methods as indicated in the following table:
jaroslav@1890
   127
 *
jaroslav@1890
   128
 * <p>
jaroslav@1890
   129
 * <table BORDER CELLPADDING=3 CELLSPACING=1>
jaroslav@1890
   130
 *  <tr>
jaroslav@1890
   131
 *    <td ALIGN=CENTER> <b><tt>BlockingQueue</tt> Method</b></td>
jaroslav@1890
   132
 *    <td ALIGN=CENTER> <b>Equivalent <tt>BlockingDeque</tt> Method</b></td>
jaroslav@1890
   133
 *  </tr>
jaroslav@1890
   134
 *  <tr>
jaroslav@1890
   135
 *    <td ALIGN=CENTER COLSPAN = 2> <b>Insert</b></td>
jaroslav@1890
   136
 *  </tr>
jaroslav@1890
   137
 *  <tr>
jaroslav@1890
   138
 *    <td>{@link #add(Object) add(e)}</td>
jaroslav@1890
   139
 *    <td>{@link #addLast(Object) addLast(e)}</td>
jaroslav@1890
   140
 *  </tr>
jaroslav@1890
   141
 *  <tr>
jaroslav@1890
   142
 *    <td>{@link #offer(Object) offer(e)}</td>
jaroslav@1890
   143
 *    <td>{@link #offerLast(Object) offerLast(e)}</td>
jaroslav@1890
   144
 *  </tr>
jaroslav@1890
   145
 *  <tr>
jaroslav@1890
   146
 *    <td>{@link #put(Object) put(e)}</td>
jaroslav@1890
   147
 *    <td>{@link #putLast(Object) putLast(e)}</td>
jaroslav@1890
   148
 *  </tr>
jaroslav@1890
   149
 *  <tr>
jaroslav@1890
   150
 *    <td>{@link #offer(Object, long, TimeUnit) offer(e, time, unit)}</td>
jaroslav@1890
   151
 *    <td>{@link #offerLast(Object, long, TimeUnit) offerLast(e, time, unit)}</td>
jaroslav@1890
   152
 *  </tr>
jaroslav@1890
   153
 *  <tr>
jaroslav@1890
   154
 *    <td ALIGN=CENTER COLSPAN = 2> <b>Remove</b></td>
jaroslav@1890
   155
 *  </tr>
jaroslav@1890
   156
 *  <tr>
jaroslav@1890
   157
 *    <td>{@link #remove() remove()}</td>
jaroslav@1890
   158
 *    <td>{@link #removeFirst() removeFirst()}</td>
jaroslav@1890
   159
 *  </tr>
jaroslav@1890
   160
 *  <tr>
jaroslav@1890
   161
 *    <td>{@link #poll() poll()}</td>
jaroslav@1890
   162
 *    <td>{@link #pollFirst() pollFirst()}</td>
jaroslav@1890
   163
 *  </tr>
jaroslav@1890
   164
 *  <tr>
jaroslav@1890
   165
 *    <td>{@link #take() take()}</td>
jaroslav@1890
   166
 *    <td>{@link #takeFirst() takeFirst()}</td>
jaroslav@1890
   167
 *  </tr>
jaroslav@1890
   168
 *  <tr>
jaroslav@1890
   169
 *    <td>{@link #poll(long, TimeUnit) poll(time, unit)}</td>
jaroslav@1890
   170
 *    <td>{@link #pollFirst(long, TimeUnit) pollFirst(time, unit)}</td>
jaroslav@1890
   171
 *  </tr>
jaroslav@1890
   172
 *  <tr>
jaroslav@1890
   173
 *    <td ALIGN=CENTER COLSPAN = 2> <b>Examine</b></td>
jaroslav@1890
   174
 *  </tr>
jaroslav@1890
   175
 *  <tr>
jaroslav@1890
   176
 *    <td>{@link #element() element()}</td>
jaroslav@1890
   177
 *    <td>{@link #getFirst() getFirst()}</td>
jaroslav@1890
   178
 *  </tr>
jaroslav@1890
   179
 *  <tr>
jaroslav@1890
   180
 *    <td>{@link #peek() peek()}</td>
jaroslav@1890
   181
 *    <td>{@link #peekFirst() peekFirst()}</td>
jaroslav@1890
   182
 *  </tr>
jaroslav@1890
   183
 * </table>
jaroslav@1890
   184
 *
jaroslav@1890
   185
 * <p>Memory consistency effects: As with other concurrent
jaroslav@1890
   186
 * collections, actions in a thread prior to placing an object into a
jaroslav@1890
   187
 * {@code BlockingDeque}
jaroslav@1890
   188
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
jaroslav@1890
   189
 * actions subsequent to the access or removal of that element from
jaroslav@1890
   190
 * the {@code BlockingDeque} in another thread.
jaroslav@1890
   191
 *
jaroslav@1890
   192
 * <p>This interface is a member of the
jaroslav@1890
   193
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@1890
   194
 * Java Collections Framework</a>.
jaroslav@1890
   195
 *
jaroslav@1890
   196
 * @since 1.6
jaroslav@1890
   197
 * @author Doug Lea
jaroslav@1890
   198
 * @param <E> the type of elements held in this collection
jaroslav@1890
   199
 */
jaroslav@1890
   200
public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> {
jaroslav@1890
   201
    /*
jaroslav@1890
   202
     * We have "diamond" multiple interface inheritance here, and that
jaroslav@1890
   203
     * introduces ambiguities.  Methods might end up with different
jaroslav@1890
   204
     * specs depending on the branch chosen by javadoc.  Thus a lot of
jaroslav@1890
   205
     * methods specs here are copied from superinterfaces.
jaroslav@1890
   206
     */
jaroslav@1890
   207
jaroslav@1890
   208
    /**
jaroslav@1890
   209
     * Inserts the specified element at the front of this deque if it is
jaroslav@1890
   210
     * possible to do so immediately without violating capacity restrictions,
jaroslav@1890
   211
     * throwing an <tt>IllegalStateException</tt> if no space is currently
jaroslav@1890
   212
     * available.  When using a capacity-restricted deque, it is generally
jaroslav@1890
   213
     * preferable to use {@link #offerFirst(Object) offerFirst}.
jaroslav@1890
   214
     *
jaroslav@1890
   215
     * @param e the element to add
jaroslav@1890
   216
     * @throws IllegalStateException {@inheritDoc}
jaroslav@1890
   217
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   218
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   219
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   220
     */
jaroslav@1890
   221
    void addFirst(E e);
jaroslav@1890
   222
jaroslav@1890
   223
    /**
jaroslav@1890
   224
     * Inserts the specified element at the end of this deque if it is
jaroslav@1890
   225
     * possible to do so immediately without violating capacity restrictions,
jaroslav@1890
   226
     * throwing an <tt>IllegalStateException</tt> if no space is currently
jaroslav@1890
   227
     * available.  When using a capacity-restricted deque, it is generally
jaroslav@1890
   228
     * preferable to use {@link #offerLast(Object) offerLast}.
jaroslav@1890
   229
     *
jaroslav@1890
   230
     * @param e the element to add
jaroslav@1890
   231
     * @throws IllegalStateException {@inheritDoc}
jaroslav@1890
   232
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   233
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   234
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   235
     */
jaroslav@1890
   236
    void addLast(E e);
jaroslav@1890
   237
jaroslav@1890
   238
    /**
jaroslav@1890
   239
     * Inserts the specified element at the front of this deque if it is
jaroslav@1890
   240
     * possible to do so immediately without violating capacity restrictions,
jaroslav@1890
   241
     * returning <tt>true</tt> upon success and <tt>false</tt> if no space is
jaroslav@1890
   242
     * currently available.
jaroslav@1890
   243
     * When using a capacity-restricted deque, this method is generally
jaroslav@1890
   244
     * preferable to the {@link #addFirst(Object) addFirst} method, which can
jaroslav@1890
   245
     * fail to insert an element only by throwing an exception.
jaroslav@1890
   246
     *
jaroslav@1890
   247
     * @param e the element to add
jaroslav@1890
   248
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   249
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   250
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   251
     */
jaroslav@1890
   252
    boolean offerFirst(E e);
jaroslav@1890
   253
jaroslav@1890
   254
    /**
jaroslav@1890
   255
     * Inserts the specified element at the end of this deque if it is
jaroslav@1890
   256
     * possible to do so immediately without violating capacity restrictions,
jaroslav@1890
   257
     * returning <tt>true</tt> upon success and <tt>false</tt> if no space is
jaroslav@1890
   258
     * currently available.
jaroslav@1890
   259
     * When using a capacity-restricted deque, this method is generally
jaroslav@1890
   260
     * preferable to the {@link #addLast(Object) addLast} method, which can
jaroslav@1890
   261
     * fail to insert an element only by throwing an exception.
jaroslav@1890
   262
     *
jaroslav@1890
   263
     * @param e the element to add
jaroslav@1890
   264
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   265
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   266
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   267
     */
jaroslav@1890
   268
    boolean offerLast(E e);
jaroslav@1890
   269
jaroslav@1890
   270
    /**
jaroslav@1890
   271
     * Inserts the specified element at the front of this deque,
jaroslav@1890
   272
     * waiting if necessary for space to become available.
jaroslav@1890
   273
     *
jaroslav@1890
   274
     * @param e the element to add
jaroslav@1890
   275
     * @throws InterruptedException if interrupted while waiting
jaroslav@1890
   276
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   277
     *         prevents it from being added to this deque
jaroslav@1890
   278
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   279
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
   280
     *         element prevents it from being added to this deque
jaroslav@1890
   281
     */
jaroslav@1890
   282
    void putFirst(E e) throws InterruptedException;
jaroslav@1890
   283
jaroslav@1890
   284
    /**
jaroslav@1890
   285
     * Inserts the specified element at the end of this deque,
jaroslav@1890
   286
     * waiting if necessary for space to become available.
jaroslav@1890
   287
     *
jaroslav@1890
   288
     * @param e the element to add
jaroslav@1890
   289
     * @throws InterruptedException if interrupted while waiting
jaroslav@1890
   290
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   291
     *         prevents it from being added to this deque
jaroslav@1890
   292
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   293
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
   294
     *         element prevents it from being added to this deque
jaroslav@1890
   295
     */
jaroslav@1890
   296
    void putLast(E e) throws InterruptedException;
jaroslav@1890
   297
jaroslav@1890
   298
    /**
jaroslav@1890
   299
     * Inserts the specified element at the front of this deque,
jaroslav@1890
   300
     * waiting up to the specified wait time if necessary for space to
jaroslav@1890
   301
     * become available.
jaroslav@1890
   302
     *
jaroslav@1890
   303
     * @param e the element to add
jaroslav@1890
   304
     * @param timeout how long to wait before giving up, in units of
jaroslav@1890
   305
     *        <tt>unit</tt>
jaroslav@1890
   306
     * @param unit a <tt>TimeUnit</tt> determining how to interpret the
jaroslav@1890
   307
     *        <tt>timeout</tt> parameter
jaroslav@1890
   308
     * @return <tt>true</tt> if successful, or <tt>false</tt> if
jaroslav@1890
   309
     *         the specified waiting time elapses before space is available
jaroslav@1890
   310
     * @throws InterruptedException if interrupted while waiting
jaroslav@1890
   311
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   312
     *         prevents it from being added to this deque
jaroslav@1890
   313
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   314
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
   315
     *         element prevents it from being added to this deque
jaroslav@1890
   316
     */
jaroslav@1890
   317
    boolean offerFirst(E e, long timeout, TimeUnit unit)
jaroslav@1890
   318
        throws InterruptedException;
jaroslav@1890
   319
jaroslav@1890
   320
    /**
jaroslav@1890
   321
     * Inserts the specified element at the end of this deque,
jaroslav@1890
   322
     * waiting up to the specified wait time if necessary for space to
jaroslav@1890
   323
     * become available.
jaroslav@1890
   324
     *
jaroslav@1890
   325
     * @param e the element to add
jaroslav@1890
   326
     * @param timeout how long to wait before giving up, in units of
jaroslav@1890
   327
     *        <tt>unit</tt>
jaroslav@1890
   328
     * @param unit a <tt>TimeUnit</tt> determining how to interpret the
jaroslav@1890
   329
     *        <tt>timeout</tt> parameter
jaroslav@1890
   330
     * @return <tt>true</tt> if successful, or <tt>false</tt> if
jaroslav@1890
   331
     *         the specified waiting time elapses before space is available
jaroslav@1890
   332
     * @throws InterruptedException if interrupted while waiting
jaroslav@1890
   333
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   334
     *         prevents it from being added to this deque
jaroslav@1890
   335
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   336
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
   337
     *         element prevents it from being added to this deque
jaroslav@1890
   338
     */
jaroslav@1890
   339
    boolean offerLast(E e, long timeout, TimeUnit unit)
jaroslav@1890
   340
        throws InterruptedException;
jaroslav@1890
   341
jaroslav@1890
   342
    /**
jaroslav@1890
   343
     * Retrieves and removes the first element of this deque, waiting
jaroslav@1890
   344
     * if necessary until an element becomes available.
jaroslav@1890
   345
     *
jaroslav@1890
   346
     * @return the head of this deque
jaroslav@1890
   347
     * @throws InterruptedException if interrupted while waiting
jaroslav@1890
   348
     */
jaroslav@1890
   349
    E takeFirst() throws InterruptedException;
jaroslav@1890
   350
jaroslav@1890
   351
    /**
jaroslav@1890
   352
     * Retrieves and removes the last element of this deque, waiting
jaroslav@1890
   353
     * if necessary until an element becomes available.
jaroslav@1890
   354
     *
jaroslav@1890
   355
     * @return the tail of this deque
jaroslav@1890
   356
     * @throws InterruptedException if interrupted while waiting
jaroslav@1890
   357
     */
jaroslav@1890
   358
    E takeLast() throws InterruptedException;
jaroslav@1890
   359
jaroslav@1890
   360
    /**
jaroslav@1890
   361
     * Retrieves and removes the first element of this deque, waiting
jaroslav@1890
   362
     * up to the specified wait time if necessary for an element to
jaroslav@1890
   363
     * become available.
jaroslav@1890
   364
     *
jaroslav@1890
   365
     * @param timeout how long to wait before giving up, in units of
jaroslav@1890
   366
     *        <tt>unit</tt>
jaroslav@1890
   367
     * @param unit a <tt>TimeUnit</tt> determining how to interpret the
jaroslav@1890
   368
     *        <tt>timeout</tt> parameter
jaroslav@1890
   369
     * @return the head of this deque, or <tt>null</tt> if the specified
jaroslav@1890
   370
     *         waiting time elapses before an element is available
jaroslav@1890
   371
     * @throws InterruptedException if interrupted while waiting
jaroslav@1890
   372
     */
jaroslav@1890
   373
    E pollFirst(long timeout, TimeUnit unit)
jaroslav@1890
   374
        throws InterruptedException;
jaroslav@1890
   375
jaroslav@1890
   376
    /**
jaroslav@1890
   377
     * Retrieves and removes the last element of this deque, waiting
jaroslav@1890
   378
     * up to the specified wait time if necessary for an element to
jaroslav@1890
   379
     * become available.
jaroslav@1890
   380
     *
jaroslav@1890
   381
     * @param timeout how long to wait before giving up, in units of
jaroslav@1890
   382
     *        <tt>unit</tt>
jaroslav@1890
   383
     * @param unit a <tt>TimeUnit</tt> determining how to interpret the
jaroslav@1890
   384
     *        <tt>timeout</tt> parameter
jaroslav@1890
   385
     * @return the tail of this deque, or <tt>null</tt> if the specified
jaroslav@1890
   386
     *         waiting time elapses before an element is available
jaroslav@1890
   387
     * @throws InterruptedException if interrupted while waiting
jaroslav@1890
   388
     */
jaroslav@1890
   389
    E pollLast(long timeout, TimeUnit unit)
jaroslav@1890
   390
        throws InterruptedException;
jaroslav@1890
   391
jaroslav@1890
   392
    /**
jaroslav@1890
   393
     * Removes the first occurrence of the specified element from this deque.
jaroslav@1890
   394
     * If the deque does not contain the element, it is unchanged.
jaroslav@1890
   395
     * More formally, removes the first element <tt>e</tt> such that
jaroslav@1890
   396
     * <tt>o.equals(e)</tt> (if such an element exists).
jaroslav@1890
   397
     * Returns <tt>true</tt> if this deque contained the specified element
jaroslav@1890
   398
     * (or equivalently, if this deque changed as a result of the call).
jaroslav@1890
   399
     *
jaroslav@1890
   400
     * @param o element to be removed from this deque, if present
jaroslav@1890
   401
     * @return <tt>true</tt> if an element was removed as a result of this call
jaroslav@1890
   402
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   403
     *         is incompatible with this deque
jaroslav@1890
   404
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
jaroslav@1890
   405
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   406
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
jaroslav@1890
   407
     */
jaroslav@1890
   408
    boolean removeFirstOccurrence(Object o);
jaroslav@1890
   409
jaroslav@1890
   410
    /**
jaroslav@1890
   411
     * Removes the last occurrence of the specified element from this deque.
jaroslav@1890
   412
     * If the deque does not contain the element, it is unchanged.
jaroslav@1890
   413
     * More formally, removes the last element <tt>e</tt> such that
jaroslav@1890
   414
     * <tt>o.equals(e)</tt> (if such an element exists).
jaroslav@1890
   415
     * Returns <tt>true</tt> if this deque contained the specified element
jaroslav@1890
   416
     * (or equivalently, if this deque changed as a result of the call).
jaroslav@1890
   417
     *
jaroslav@1890
   418
     * @param o element to be removed from this deque, if present
jaroslav@1890
   419
     * @return <tt>true</tt> if an element was removed as a result of this call
jaroslav@1890
   420
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   421
     *         is incompatible with this deque
jaroslav@1890
   422
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
jaroslav@1890
   423
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   424
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
jaroslav@1890
   425
     */
jaroslav@1890
   426
    boolean removeLastOccurrence(Object o);
jaroslav@1890
   427
jaroslav@1890
   428
    // *** BlockingQueue methods ***
jaroslav@1890
   429
jaroslav@1890
   430
    /**
jaroslav@1890
   431
     * Inserts the specified element into the queue represented by this deque
jaroslav@1890
   432
     * (in other words, at the tail of this deque) if it is possible to do so
jaroslav@1890
   433
     * immediately without violating capacity restrictions, returning
jaroslav@1890
   434
     * <tt>true</tt> upon success and throwing an
jaroslav@1890
   435
     * <tt>IllegalStateException</tt> if no space is currently available.
jaroslav@1890
   436
     * When using a capacity-restricted deque, it is generally preferable to
jaroslav@1890
   437
     * use {@link #offer(Object) offer}.
jaroslav@1890
   438
     *
jaroslav@1890
   439
     * <p>This method is equivalent to {@link #addLast(Object) addLast}.
jaroslav@1890
   440
     *
jaroslav@1890
   441
     * @param e the element to add
jaroslav@1890
   442
     * @throws IllegalStateException {@inheritDoc}
jaroslav@1890
   443
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   444
     *         prevents it from being added to this deque
jaroslav@1890
   445
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   446
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
   447
     *         element prevents it from being added to this deque
jaroslav@1890
   448
     */
jaroslav@1890
   449
    boolean add(E e);
jaroslav@1890
   450
jaroslav@1890
   451
    /**
jaroslav@1890
   452
     * Inserts the specified element into the queue represented by this deque
jaroslav@1890
   453
     * (in other words, at the tail of this deque) if it is possible to do so
jaroslav@1890
   454
     * immediately without violating capacity restrictions, returning
jaroslav@1890
   455
     * <tt>true</tt> upon success and <tt>false</tt> if no space is currently
jaroslav@1890
   456
     * available.  When using a capacity-restricted deque, this method is
jaroslav@1890
   457
     * generally preferable to the {@link #add} method, which can fail to
jaroslav@1890
   458
     * insert an element only by throwing an exception.
jaroslav@1890
   459
     *
jaroslav@1890
   460
     * <p>This method is equivalent to {@link #offerLast(Object) offerLast}.
jaroslav@1890
   461
     *
jaroslav@1890
   462
     * @param e the element to add
jaroslav@1890
   463
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   464
     *         prevents it from being added to this deque
jaroslav@1890
   465
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   466
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
   467
     *         element prevents it from being added to this deque
jaroslav@1890
   468
     */
jaroslav@1890
   469
    boolean offer(E e);
jaroslav@1890
   470
jaroslav@1890
   471
    /**
jaroslav@1890
   472
     * Inserts the specified element into the queue represented by this deque
jaroslav@1890
   473
     * (in other words, at the tail of this deque), waiting if necessary for
jaroslav@1890
   474
     * space to become available.
jaroslav@1890
   475
     *
jaroslav@1890
   476
     * <p>This method is equivalent to {@link #putLast(Object) putLast}.
jaroslav@1890
   477
     *
jaroslav@1890
   478
     * @param e the element to add
jaroslav@1890
   479
     * @throws InterruptedException {@inheritDoc}
jaroslav@1890
   480
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   481
     *         prevents it from being added to this deque
jaroslav@1890
   482
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   483
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
   484
     *         element prevents it from being added to this deque
jaroslav@1890
   485
     */
jaroslav@1890
   486
    void put(E e) throws InterruptedException;
jaroslav@1890
   487
jaroslav@1890
   488
    /**
jaroslav@1890
   489
     * Inserts the specified element into the queue represented by this deque
jaroslav@1890
   490
     * (in other words, at the tail of this deque), waiting up to the
jaroslav@1890
   491
     * specified wait time if necessary for space to become available.
jaroslav@1890
   492
     *
jaroslav@1890
   493
     * <p>This method is equivalent to
jaroslav@1890
   494
     * {@link #offerLast(Object,long,TimeUnit) offerLast}.
jaroslav@1890
   495
     *
jaroslav@1890
   496
     * @param e the element to add
jaroslav@1890
   497
     * @return <tt>true</tt> if the element was added to this deque, else
jaroslav@1890
   498
     *         <tt>false</tt>
jaroslav@1890
   499
     * @throws InterruptedException {@inheritDoc}
jaroslav@1890
   500
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   501
     *         prevents it from being added to this deque
jaroslav@1890
   502
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   503
     * @throws IllegalArgumentException if some property of the specified
jaroslav@1890
   504
     *         element prevents it from being added to this deque
jaroslav@1890
   505
     */
jaroslav@1890
   506
    boolean offer(E e, long timeout, TimeUnit unit)
jaroslav@1890
   507
        throws InterruptedException;
jaroslav@1890
   508
jaroslav@1890
   509
    /**
jaroslav@1890
   510
     * Retrieves and removes the head of the queue represented by this deque
jaroslav@1890
   511
     * (in other words, the first element of this deque).
jaroslav@1890
   512
     * This method differs from {@link #poll poll} only in that it
jaroslav@1890
   513
     * throws an exception if this deque is empty.
jaroslav@1890
   514
     *
jaroslav@1890
   515
     * <p>This method is equivalent to {@link #removeFirst() removeFirst}.
jaroslav@1890
   516
     *
jaroslav@1890
   517
     * @return the head of the queue represented by this deque
jaroslav@1890
   518
     * @throws NoSuchElementException if this deque is empty
jaroslav@1890
   519
     */
jaroslav@1890
   520
    E remove();
jaroslav@1890
   521
jaroslav@1890
   522
    /**
jaroslav@1890
   523
     * Retrieves and removes the head of the queue represented by this deque
jaroslav@1890
   524
     * (in other words, the first element of this deque), or returns
jaroslav@1890
   525
     * <tt>null</tt> if this deque is empty.
jaroslav@1890
   526
     *
jaroslav@1890
   527
     * <p>This method is equivalent to {@link #pollFirst()}.
jaroslav@1890
   528
     *
jaroslav@1890
   529
     * @return the head of this deque, or <tt>null</tt> if this deque is empty
jaroslav@1890
   530
     */
jaroslav@1890
   531
    E poll();
jaroslav@1890
   532
jaroslav@1890
   533
    /**
jaroslav@1890
   534
     * Retrieves and removes the head of the queue represented by this deque
jaroslav@1890
   535
     * (in other words, the first element of this deque), waiting if
jaroslav@1890
   536
     * necessary until an element becomes available.
jaroslav@1890
   537
     *
jaroslav@1890
   538
     * <p>This method is equivalent to {@link #takeFirst() takeFirst}.
jaroslav@1890
   539
     *
jaroslav@1890
   540
     * @return the head of this deque
jaroslav@1890
   541
     * @throws InterruptedException if interrupted while waiting
jaroslav@1890
   542
     */
jaroslav@1890
   543
    E take() throws InterruptedException;
jaroslav@1890
   544
jaroslav@1890
   545
    /**
jaroslav@1890
   546
     * Retrieves and removes the head of the queue represented by this deque
jaroslav@1890
   547
     * (in other words, the first element of this deque), waiting up to the
jaroslav@1890
   548
     * specified wait time if necessary for an element to become available.
jaroslav@1890
   549
     *
jaroslav@1890
   550
     * <p>This method is equivalent to
jaroslav@1890
   551
     * {@link #pollFirst(long,TimeUnit) pollFirst}.
jaroslav@1890
   552
     *
jaroslav@1890
   553
     * @return the head of this deque, or <tt>null</tt> if the
jaroslav@1890
   554
     *         specified waiting time elapses before an element is available
jaroslav@1890
   555
     * @throws InterruptedException if interrupted while waiting
jaroslav@1890
   556
     */
jaroslav@1890
   557
    E poll(long timeout, TimeUnit unit)
jaroslav@1890
   558
        throws InterruptedException;
jaroslav@1890
   559
jaroslav@1890
   560
    /**
jaroslav@1890
   561
     * Retrieves, but does not remove, the head of the queue represented by
jaroslav@1890
   562
     * this deque (in other words, the first element of this deque).
jaroslav@1890
   563
     * This method differs from {@link #peek peek} only in that it throws an
jaroslav@1890
   564
     * exception if this deque is empty.
jaroslav@1890
   565
     *
jaroslav@1890
   566
     * <p>This method is equivalent to {@link #getFirst() getFirst}.
jaroslav@1890
   567
     *
jaroslav@1890
   568
     * @return the head of this deque
jaroslav@1890
   569
     * @throws NoSuchElementException if this deque is empty
jaroslav@1890
   570
     */
jaroslav@1890
   571
    E element();
jaroslav@1890
   572
jaroslav@1890
   573
    /**
jaroslav@1890
   574
     * Retrieves, but does not remove, the head of the queue represented by
jaroslav@1890
   575
     * this deque (in other words, the first element of this deque), or
jaroslav@1890
   576
     * returns <tt>null</tt> if this deque is empty.
jaroslav@1890
   577
     *
jaroslav@1890
   578
     * <p>This method is equivalent to {@link #peekFirst() peekFirst}.
jaroslav@1890
   579
     *
jaroslav@1890
   580
     * @return the head of this deque, or <tt>null</tt> if this deque is empty
jaroslav@1890
   581
     */
jaroslav@1890
   582
    E peek();
jaroslav@1890
   583
jaroslav@1890
   584
    /**
jaroslav@1890
   585
     * Removes the first occurrence of the specified element from this deque.
jaroslav@1890
   586
     * If the deque does not contain the element, it is unchanged.
jaroslav@1890
   587
     * More formally, removes the first element <tt>e</tt> such that
jaroslav@1890
   588
     * <tt>o.equals(e)</tt> (if such an element exists).
jaroslav@1890
   589
     * Returns <tt>true</tt> if this deque contained the specified element
jaroslav@1890
   590
     * (or equivalently, if this deque changed as a result of the call).
jaroslav@1890
   591
     *
jaroslav@1890
   592
     * <p>This method is equivalent to
jaroslav@1890
   593
     * {@link #removeFirstOccurrence(Object) removeFirstOccurrence}.
jaroslav@1890
   594
     *
jaroslav@1890
   595
     * @param o element to be removed from this deque, if present
jaroslav@1890
   596
     * @return <tt>true</tt> if this deque changed as a result of the call
jaroslav@1890
   597
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   598
     *         is incompatible with this deque
jaroslav@1890
   599
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
jaroslav@1890
   600
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   601
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
jaroslav@1890
   602
     */
jaroslav@1890
   603
    boolean remove(Object o);
jaroslav@1890
   604
jaroslav@1890
   605
    /**
jaroslav@1890
   606
     * Returns <tt>true</tt> if this deque contains the specified element.
jaroslav@1890
   607
     * More formally, returns <tt>true</tt> if and only if this deque contains
jaroslav@1890
   608
     * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
jaroslav@1890
   609
     *
jaroslav@1890
   610
     * @param o object to be checked for containment in this deque
jaroslav@1890
   611
     * @return <tt>true</tt> if this deque contains the specified element
jaroslav@1890
   612
     * @throws ClassCastException if the class of the specified element
jaroslav@1890
   613
     *         is incompatible with this deque
jaroslav@1890
   614
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
jaroslav@1890
   615
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   616
     *         (<a href="../Collection.html#optional-restrictions">optional</a>)
jaroslav@1890
   617
     */
jaroslav@1890
   618
    public boolean contains(Object o);
jaroslav@1890
   619
jaroslav@1890
   620
    /**
jaroslav@1890
   621
     * Returns the number of elements in this deque.
jaroslav@1890
   622
     *
jaroslav@1890
   623
     * @return the number of elements in this deque
jaroslav@1890
   624
     */
jaroslav@1890
   625
    public int size();
jaroslav@1890
   626
jaroslav@1890
   627
    /**
jaroslav@1890
   628
     * Returns an iterator over the elements in this deque in proper sequence.
jaroslav@1890
   629
     * The elements will be returned in order from first (head) to last (tail).
jaroslav@1890
   630
     *
jaroslav@1890
   631
     * @return an iterator over the elements in this deque in proper sequence
jaroslav@1890
   632
     */
jaroslav@1890
   633
    Iterator<E> iterator();
jaroslav@1890
   634
jaroslav@1890
   635
    // *** Stack methods ***
jaroslav@1890
   636
jaroslav@1890
   637
    /**
jaroslav@1890
   638
     * Pushes an element onto the stack represented by this deque.  In other
jaroslav@1890
   639
     * words, inserts the element at the front of this deque unless it would
jaroslav@1890
   640
     * violate capacity restrictions.
jaroslav@1890
   641
     *
jaroslav@1890
   642
     * <p>This method is equivalent to {@link #addFirst(Object) addFirst}.
jaroslav@1890
   643
     *
jaroslav@1890
   644
     * @throws IllegalStateException {@inheritDoc}
jaroslav@1890
   645
     * @throws ClassCastException {@inheritDoc}
jaroslav@1890
   646
     * @throws NullPointerException if the specified element is null
jaroslav@1890
   647
     * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890
   648
     */
jaroslav@1890
   649
    void push(E e);
jaroslav@1890
   650
}