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