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 and Josh Bloch with assistance from members of 32 * JCP JSR-166 Expert Group and released to the public domain, as explained 33 * at http://creativecommons.org/publicdomain/zero/1.0/ 34 */ 35 36 package java.util; 37 38 // Android-changed: removed link to collections framework docs 39 /** 40 * A linear collection that supports element insertion and removal at 41 * both ends. The name <i>deque</i> is short for "double ended queue" 42 * and is usually pronounced "deck". Most {@code Deque} 43 * implementations place no fixed limits on the number of elements 44 * they may contain, but this interface supports capacity-restricted 45 * deques as well as those with no fixed size limit. 46 * 47 * <p>This interface defines methods to access the elements at both 48 * ends of the deque. Methods are provided to insert, remove, and 49 * examine the element. Each of these methods exists in two forms: 50 * one throws an exception if the operation fails, the other returns a 51 * special value (either {@code null} or {@code false}, depending on 52 * the operation). The latter form of the insert operation is 53 * designed specifically for use with capacity-restricted 54 * {@code Deque} implementations; in most implementations, insert 55 * operations cannot fail. 56 * 57 * <p>The twelve methods described above are summarized in the 58 * following table: 59 * 60 * <table class="striped"> 61 * <caption>Summary of Deque methods</caption> 62 * <thead> 63 * <tr> 64 * <td rowspan="2"></td> 65 * <th scope="col" colspan="2"> First Element (Head)</th> 66 * <th scope="col" colspan="2"> Last Element (Tail)</th> 67 * </tr> 68 * <tr> 69 * <th scope="col" style="font-weight:normal; font-style:italic">Throws exception</th> 70 * <th scope="col" style="font-weight:normal; font-style:italic">Special value</th> 71 * <th scope="col" style="font-weight:normal; font-style:italic">Throws exception</th> 72 * <th scope="col" style="font-weight:normal; font-style:italic">Special value</th> 73 * </tr> 74 * </thead> 75 * <tbody> 76 * <tr> 77 * <th scope="row">Insert</th> 78 * <td>{@link #addFirst(Object) addFirst(e)}</td> 79 * <td>{@link #offerFirst(Object) offerFirst(e)}</td> 80 * <td>{@link #addLast(Object) addLast(e)}</td> 81 * <td>{@link #offerLast(Object) offerLast(e)}</td> 82 * </tr> 83 * <tr> 84 * <th scope="row">Remove</th> 85 * <td>{@link #removeFirst() removeFirst()}</td> 86 * <td>{@link #pollFirst() pollFirst()}</td> 87 * <td>{@link #removeLast() removeLast()}</td> 88 * <td>{@link #pollLast() pollLast()}</td> 89 * </tr> 90 * <tr> 91 * <th scope="row">Examine</th> 92 * <td>{@link #getFirst() getFirst()}</td> 93 * <td>{@link #peekFirst() peekFirst()}</td> 94 * <td>{@link #getLast() getLast()}</td> 95 * <td>{@link #peekLast() peekLast()}</td> 96 * </tr> 97 * </tbody> 98 * </table> 99 * 100 * <p>This interface extends the {@link Queue} interface. When a deque is 101 * used as a queue, FIFO (First-In-First-Out) behavior results. Elements are 102 * added at the end of the deque and removed from the beginning. The methods 103 * inherited from the {@code Queue} interface are precisely equivalent to 104 * {@code Deque} methods as indicated in the following table: 105 * 106 * <table class="striped"> 107 * <caption>Comparison of Queue and Deque methods</caption> 108 * <thead> 109 * <tr> 110 * <th scope="col"> {@code Queue} Method</th> 111 * <th scope="col"> Equivalent {@code Deque} Method</th> 112 * </tr> 113 * </thead> 114 * <tbody> 115 * <tr> 116 * <th scope="row">{@link #add(Object) add(e)}</th> 117 * <td>{@link #addLast(Object) addLast(e)}</td> 118 * </tr> 119 * <tr> 120 * <th scope="row">{@link #offer(Object) offer(e)}</th> 121 * <td>{@link #offerLast(Object) offerLast(e)}</td> 122 * </tr> 123 * <tr> 124 * <th scope="row">{@link #remove() remove()}</th> 125 * <td>{@link #removeFirst() removeFirst()}</td> 126 * </tr> 127 * <tr> 128 * <th scope="row">{@link #poll() poll()}</th> 129 * <td>{@link #pollFirst() pollFirst()}</td> 130 * </tr> 131 * <tr> 132 * <th scope="row">{@link #element() element()}</th> 133 * <td>{@link #getFirst() getFirst()}</td> 134 * </tr> 135 * <tr> 136 * <th scope="row">{@link #peek() peek()}</th> 137 * <td>{@link #peekFirst() peekFirst()}</td> 138 * </tr> 139 * </tbody> 140 * </table> 141 * 142 * <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This 143 * interface should be used in preference to the legacy {@link Stack} class. 144 * When a deque is used as a stack, elements are pushed and popped from the 145 * beginning of the deque. Stack methods are equivalent to {@code Deque} 146 * methods as indicated in the table below: 147 * 148 * <table class="striped"> 149 * <caption>Comparison of Stack and Deque methods</caption> 150 * <thead> 151 * <tr> 152 * <th scope="col"> Stack Method</th> 153 * <th scope="col"> Equivalent {@code Deque} Method</th> 154 * </tr> 155 * </thead> 156 * <tbody> 157 * <tr> 158 * <th scope="row">{@link #push(Object) push(e)}</th> 159 * <td>{@link #addFirst(Object) addFirst(e)}</td> 160 * </tr> 161 * <tr> 162 * <th scope="row">{@link #pop() pop()}</th> 163 * <td>{@link #removeFirst() removeFirst()}</td> 164 * </tr> 165 * <tr> 166 * <th scope="row">{@link #peek() peek()}</th> 167 * <td>{@link #getFirst() getFirst()}</td> 168 * </tr> 169 * </tbody> 170 * </table> 171 * 172 * <p>Note that the {@link #peek peek} method works equally well when 173 * a deque is used as a queue or a stack; in either case, elements are 174 * drawn from the beginning of the deque. 175 * 176 * <p>This interface provides two methods to remove interior 177 * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and 178 * {@link #removeLastOccurrence removeLastOccurrence}. 179 * 180 * <p>Unlike the {@link List} interface, this interface does not 181 * provide support for indexed access to elements. 182 * 183 * <p>While {@code Deque} implementations are not strictly required 184 * to prohibit the insertion of null elements, they are strongly 185 * encouraged to do so. Users of any {@code Deque} implementations 186 * that do allow null elements are strongly encouraged <i>not</i> to 187 * take advantage of the ability to insert nulls. This is so because 188 * {@code null} is used as a special return value by various methods 189 * to indicate that the deque is empty. 190 * 191 * <p>{@code Deque} implementations generally do not define 192 * element-based versions of the {@code equals} and {@code hashCode} 193 * methods, but instead inherit the identity-based versions from class 194 * {@code Object}. 195 * 196 * @author Doug Lea 197 * @author Josh Bloch 198 * @since 1.6 199 * @param <E> the type of elements held in this deque 200 */ 201 public interface Deque<E> extends Queue<E>, SequencedCollection<E> { 202 // Android-changed: fix framework docs link to "Collection#optional-restrictions" 203 // Several occurrences of the link have been fixed throughout. 204 /** 205 * Inserts the specified element at the front of this deque if it is 206 * possible to do so immediately without violating capacity restrictions, 207 * throwing an {@code IllegalStateException} if no space is currently 208 * available. When using a capacity-restricted deque, it is generally 209 * preferable to use method {@link #offerFirst}. 210 * 211 * @param e the element to add 212 * @throws IllegalStateException if the element cannot be added at this 213 * time due to capacity restrictions 214 * @throws ClassCastException if the class of the specified element 215 * prevents it from being added to this deque 216 * @throws NullPointerException if the specified element is null and this 217 * deque does not permit null elements 218 * @throws IllegalArgumentException if some property of the specified 219 * element prevents it from being added to this deque 220 */ addFirst(E e)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 {@code IllegalStateException} if no space is currently 227 * available. When using a capacity-restricted deque, it is generally 228 * preferable to use method {@link #offerLast}. 229 * 230 * <p>This method is equivalent to {@link #add}. 231 * 232 * @param e the element to add 233 * @throws IllegalStateException if the element cannot be added at this 234 * time due to capacity restrictions 235 * @throws ClassCastException if the class of the specified element 236 * prevents it from being added to this deque 237 * @throws NullPointerException if the specified element is null and this 238 * deque does not permit null elements 239 * @throws IllegalArgumentException if some property of the specified 240 * element prevents it from being added to this deque 241 */ addLast(E e)242 void addLast(E e); 243 244 /** 245 * Inserts the specified element at the front of this deque unless it would 246 * violate capacity restrictions. When using a capacity-restricted deque, 247 * this method is generally preferable to the {@link #addFirst} method, 248 * which can fail to insert an element only by throwing an exception. 249 * 250 * @param e the element to add 251 * @return {@code true} if the element was added to this deque, else 252 * {@code false} 253 * @throws ClassCastException if the class of the specified element 254 * prevents it from being added to this deque 255 * @throws NullPointerException if the specified element is null and this 256 * deque does not permit null elements 257 * @throws IllegalArgumentException if some property of the specified 258 * element prevents it from being added to this deque 259 */ offerFirst(E e)260 boolean offerFirst(E e); 261 262 /** 263 * Inserts the specified element at the end of this deque unless it would 264 * violate capacity restrictions. When using a capacity-restricted deque, 265 * this method is generally preferable to the {@link #addLast} method, 266 * which can fail to insert an element only by throwing an exception. 267 * 268 * @param e the element to add 269 * @return {@code true} if the element was added to this deque, else 270 * {@code false} 271 * @throws ClassCastException if the class of the specified element 272 * prevents it from being added to this deque 273 * @throws NullPointerException if the specified element is null and this 274 * deque does not permit null elements 275 * @throws IllegalArgumentException if some property of the specified 276 * element prevents it from being added to this deque 277 */ offerLast(E e)278 boolean offerLast(E e); 279 280 /** 281 * Retrieves and removes the first element of this deque. This method 282 * differs from {@link #pollFirst pollFirst} only in that it throws an 283 * exception if this deque is empty. 284 * 285 * @return the head of this deque 286 * @throws NoSuchElementException if this deque is empty 287 */ removeFirst()288 E removeFirst(); 289 290 /** 291 * Retrieves and removes the last element of this deque. This method 292 * differs from {@link #pollLast pollLast} only in that it throws an 293 * exception if this deque is empty. 294 * 295 * @return the tail of this deque 296 * @throws NoSuchElementException if this deque is empty 297 */ removeLast()298 E removeLast(); 299 300 /** 301 * Retrieves and removes the first element of this deque, 302 * or returns {@code null} if this deque is empty. 303 * 304 * @return the head of this deque, or {@code null} if this deque is empty 305 */ pollFirst()306 E pollFirst(); 307 308 /** 309 * Retrieves and removes the last element of this deque, 310 * or returns {@code null} if this deque is empty. 311 * 312 * @return the tail of this deque, or {@code null} if this deque is empty 313 */ pollLast()314 E pollLast(); 315 316 /** 317 * Retrieves, but does not remove, the first element of this deque. 318 * 319 * This method differs from {@link #peekFirst peekFirst} only in that it 320 * throws an exception if this deque is empty. 321 * 322 * @return the head of this deque 323 * @throws NoSuchElementException if this deque is empty 324 */ getFirst()325 E getFirst(); 326 327 /** 328 * Retrieves, but does not remove, the last element of this deque. 329 * This method differs from {@link #peekLast peekLast} only in that it 330 * throws an exception if this deque is empty. 331 * 332 * @return the tail of this deque 333 * @throws NoSuchElementException if this deque is empty 334 */ getLast()335 E getLast(); 336 337 /** 338 * Retrieves, but does not remove, the first element of this deque, 339 * or returns {@code null} if this deque is empty. 340 * 341 * @return the head of this deque, or {@code null} if this deque is empty 342 */ peekFirst()343 E peekFirst(); 344 345 /** 346 * Retrieves, but does not remove, the last element of this deque, 347 * or returns {@code null} if this deque is empty. 348 * 349 * @return the tail of this deque, or {@code null} if this deque is empty 350 */ peekLast()351 E peekLast(); 352 353 /** 354 * Removes the first occurrence of the specified element from this deque. 355 * If the deque does not contain the element, it is unchanged. 356 * More formally, removes the first element {@code e} such that 357 * {@code Objects.equals(o, e)} (if such an element exists). 358 * Returns {@code true} if this deque contained the specified element 359 * (or equivalently, if this deque changed as a result of the call). 360 * 361 * @param o element to be removed from this deque, if present 362 * @return {@code true} if an element was removed as a result of this call 363 * @throws ClassCastException if the class of the specified element 364 * is incompatible with this deque 365 * ({@linkplain Collection##optional-restrictions optional}) 366 * @throws NullPointerException if the specified element is null and this 367 * deque does not permit null elements 368 * ({@linkplain Collection##optional-restrictions optional}) 369 */ removeFirstOccurrence(Object o)370 boolean removeFirstOccurrence(Object o); 371 372 /** 373 * Removes the last occurrence of the specified element from this deque. 374 * If the deque does not contain the element, it is unchanged. 375 * More formally, removes the last element {@code e} such that 376 * {@code Objects.equals(o, e)} (if such an element exists). 377 * Returns {@code true} if this deque contained the specified element 378 * (or equivalently, if this deque changed as a result of the call). 379 * 380 * @param o element to be removed from this deque, if present 381 * @return {@code true} if an element was removed as a result of this call 382 * @throws ClassCastException if the class of the specified element 383 * is incompatible with this deque 384 * ({@linkplain Collection##optional-restrictions optional}) 385 * @throws NullPointerException if the specified element is null and this 386 * deque does not permit null elements 387 * ({@linkplain Collection##optional-restrictions optional}) 388 */ removeLastOccurrence(Object o)389 boolean removeLastOccurrence(Object o); 390 391 // *** Queue methods *** 392 393 /** 394 * Inserts the specified element into the queue represented by this deque 395 * (in other words, at the tail of this deque) if it is possible to do so 396 * immediately without violating capacity restrictions, returning 397 * {@code true} upon success and throwing an 398 * {@code IllegalStateException} if no space is currently available. 399 * When using a capacity-restricted deque, it is generally preferable to 400 * use {@link #offer(Object) offer}. 401 * 402 * <p>This method is equivalent to {@link #addLast}. 403 * 404 * @param e the element to add 405 * @return {@code true} (as specified by {@link Collection#add}) 406 * @throws IllegalStateException if the element cannot be added at this 407 * time due to capacity restrictions 408 * @throws ClassCastException if the class of the specified element 409 * prevents it from being added to this deque 410 * @throws NullPointerException if the specified element is null and this 411 * deque does not permit null elements 412 * @throws IllegalArgumentException if some property of the specified 413 * element prevents it from being added to this deque 414 */ add(E e)415 boolean add(E e); 416 417 /** 418 * Inserts the specified element into the queue represented by this deque 419 * (in other words, at the tail of this deque) if it is possible to do so 420 * immediately without violating capacity restrictions, returning 421 * {@code true} upon success and {@code false} if no space is currently 422 * available. When using a capacity-restricted deque, this method is 423 * generally preferable to the {@link #add} method, which can fail to 424 * insert an element only by throwing an exception. 425 * 426 * <p>This method is equivalent to {@link #offerLast}. 427 * 428 * @param e the element to add 429 * @return {@code true} if the element was added to this deque, else 430 * {@code false} 431 * @throws ClassCastException if the class of the specified element 432 * prevents it from being added to this deque 433 * @throws NullPointerException if the specified element is null and this 434 * deque does not permit null elements 435 * @throws IllegalArgumentException if some property of the specified 436 * element prevents it from being added to this deque 437 */ offer(E e)438 boolean offer(E e); 439 440 /** 441 * Retrieves and removes the head of the queue represented by this deque 442 * (in other words, the first element of this deque). 443 * This method differs from {@link #poll() poll()} only in that it 444 * throws an exception if this deque is empty. 445 * 446 * <p>This method is equivalent to {@link #removeFirst()}. 447 * 448 * @return the head of the queue represented by this deque 449 * @throws NoSuchElementException if this deque is empty 450 */ remove()451 E remove(); 452 453 /** 454 * Retrieves and removes the head of the queue represented by this deque 455 * (in other words, the first element of this deque), or returns 456 * {@code null} if this deque is empty. 457 * 458 * <p>This method is equivalent to {@link #pollFirst()}. 459 * 460 * @return the first element of this deque, or {@code null} if 461 * this deque is empty 462 */ poll()463 E poll(); 464 465 /** 466 * Retrieves, but does not remove, the head of the queue represented by 467 * this deque (in other words, the first element of this deque). 468 * This method differs from {@link #peek peek} only in that it throws an 469 * exception if this deque is empty. 470 * 471 * <p>This method is equivalent to {@link #getFirst()}. 472 * 473 * @return the head of the queue represented by this deque 474 * @throws NoSuchElementException if this deque is empty 475 */ element()476 E element(); 477 478 /** 479 * Retrieves, but does not remove, the head of the queue represented by 480 * this deque (in other words, the first element of this deque), or 481 * returns {@code null} if this deque is empty. 482 * 483 * <p>This method is equivalent to {@link #peekFirst()}. 484 * 485 * @return the head of the queue represented by this deque, or 486 * {@code null} if this deque is empty 487 */ peek()488 E peek(); 489 490 /** 491 * Adds all of the elements in the specified collection at the end 492 * of this deque, as if by calling {@link #addLast} on each one, 493 * in the order that they are returned by the collection's iterator. 494 * 495 * <p>When using a capacity-restricted deque, it is generally preferable 496 * to call {@link #offer(Object) offer} separately on each element. 497 * 498 * <p>An exception encountered while trying to add an element may result 499 * in only some of the elements having been successfully added when 500 * the associated exception is thrown. 501 * 502 * @param c the elements to be inserted into this deque 503 * @return {@code true} if this deque changed as a result of the call 504 * @throws IllegalStateException if not all the elements can be added at 505 * this time due to insertion restrictions 506 * @throws ClassCastException if the class of an element of the specified 507 * collection prevents it from being added to this deque 508 * @throws NullPointerException if the specified collection contains a 509 * null element and this deque does not permit null elements, 510 * or if the specified collection is null 511 * @throws IllegalArgumentException if some property of an element of the 512 * specified collection prevents it from being added to this deque 513 */ addAll(Collection<? extends E> c)514 boolean addAll(Collection<? extends E> c); 515 516 // *** Stack methods *** 517 518 /** 519 * Pushes an element onto the stack represented by this deque (in other 520 * words, at the head of this deque) if it is possible to do so 521 * immediately without violating capacity restrictions, throwing an 522 * {@code IllegalStateException} if no space is currently available. 523 * 524 * <p>This method is equivalent to {@link #addFirst}. 525 * 526 * @param e the element to push 527 * @throws IllegalStateException if the element cannot be added at this 528 * time due to capacity restrictions 529 * @throws ClassCastException if the class of the specified element 530 * prevents it from being added to this deque 531 * @throws NullPointerException if the specified element is null and this 532 * deque does not permit null elements 533 * @throws IllegalArgumentException if some property of the specified 534 * element prevents it from being added to this deque 535 */ push(E e)536 void push(E e); 537 538 /** 539 * Pops an element from the stack represented by this deque. In other 540 * words, removes and returns the first element of this deque. 541 * 542 * <p>This method is equivalent to {@link #removeFirst()}. 543 * 544 * @return the element at the front of this deque (which is the top 545 * of the stack represented by this deque) 546 * @throws NoSuchElementException if this deque is empty 547 */ pop()548 E pop(); 549 550 551 // *** Collection methods *** 552 553 /** 554 * Removes the first occurrence of the specified element from this deque. 555 * If the deque does not contain the element, it is unchanged. 556 * More formally, removes the first element {@code e} such that 557 * {@code Objects.equals(o, e)} (if such an element exists). 558 * Returns {@code true} if this deque contained the specified element 559 * (or equivalently, if this deque changed as a result of the call). 560 * 561 * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. 562 * 563 * @param o element to be removed from this deque, if present 564 * @return {@code true} if an element was removed as a result of this call 565 * @throws ClassCastException if the class of the specified element 566 * is incompatible with this deque 567 * ({@linkplain Collection##optional-restrictions optional}) 568 * @throws NullPointerException if the specified element is null and this 569 * deque does not permit null elements 570 * ({@linkplain Collection##optional-restrictions optional}) 571 */ remove(Object o)572 boolean remove(Object o); 573 574 /** 575 * Returns {@code true} if this deque contains the specified element. 576 * More formally, returns {@code true} if and only if this deque contains 577 * at least one element {@code e} such that {@code Objects.equals(o, e)}. 578 * 579 * @param o element whose presence in this deque is to be tested 580 * @return {@code true} if this deque contains the specified element 581 * @throws ClassCastException if the class of the specified element 582 * is incompatible with this deque 583 * ({@linkplain Collection##optional-restrictions optional}) 584 * @throws NullPointerException if the specified element is null and this 585 * deque does not permit null elements 586 * ({@linkplain Collection##optional-restrictions optional}) 587 */ contains(Object o)588 boolean contains(Object o); 589 590 /** 591 * Returns the number of elements in this deque. 592 * 593 * @return the number of elements in this deque 594 */ size()595 int size(); 596 597 /** 598 * Returns an iterator over the elements in this deque in proper sequence. 599 * The elements will be returned in order from first (head) to last (tail). 600 * 601 * @return an iterator over the elements in this deque in proper sequence 602 */ iterator()603 Iterator<E> iterator(); 604 605 /** 606 * Returns an iterator over the elements in this deque in reverse 607 * sequential order. The elements will be returned in order from 608 * last (tail) to first (head). 609 * 610 * @return an iterator over the elements in this deque in reverse 611 * sequence 612 */ descendingIterator()613 Iterator<E> descendingIterator(); 614 615 /** 616 * {@inheritDoc} 617 * 618 * @implSpec 619 * The implementation in this interface returns a reverse-ordered Deque 620 * view. The {@code reversed()} method of the view returns a reference 621 * to this Deque. Other operations on the view are implemented via calls to 622 * public methods on this Deque. The exact relationship between calls on the 623 * view and calls on this Deque is unspecified. However, order-sensitive 624 * operations generally delegate to the appropriate method with the opposite 625 * orientation. For example, calling {@code getFirst} on the view results in 626 * a call to {@code getLast} on this Deque. 627 * 628 * @return a reverse-ordered view of this collection, as a {@code Deque} 629 * @since 21 630 */ reversed()631 default Deque<E> reversed() { 632 return ReverseOrderDequeView.of(this); 633 } 634 } 635