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