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