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 BORDER CELLPADDING=3 CELLSPACING=1> 61 * <caption>Summary of Deque methods</caption> 62 * <tr> 63 * <td></td> 64 * <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td> 65 * <td ALIGN=CENTER COLSPAN = 2> <b>Last Element (Tail)</b></td> 66 * </tr> 67 * <tr> 68 * <td></td> 69 * <td ALIGN=CENTER><em>Throws exception</em></td> 70 * <td ALIGN=CENTER><em>Special value</em></td> 71 * <td ALIGN=CENTER><em>Throws exception</em></td> 72 * <td ALIGN=CENTER><em>Special value</em></td> 73 * </tr> 74 * <tr> 75 * <td><b>Insert</b></td> 76 * <td>{@link Deque#addFirst addFirst(e)}</td> 77 * <td>{@link Deque#offerFirst offerFirst(e)}</td> 78 * <td>{@link Deque#addLast addLast(e)}</td> 79 * <td>{@link Deque#offerLast offerLast(e)}</td> 80 * </tr> 81 * <tr> 82 * <td><b>Remove</b></td> 83 * <td>{@link Deque#removeFirst removeFirst()}</td> 84 * <td>{@link Deque#pollFirst pollFirst()}</td> 85 * <td>{@link Deque#removeLast removeLast()}</td> 86 * <td>{@link Deque#pollLast pollLast()}</td> 87 * </tr> 88 * <tr> 89 * <td><b>Examine</b></td> 90 * <td>{@link Deque#getFirst getFirst()}</td> 91 * <td>{@link Deque#peekFirst peekFirst()}</td> 92 * <td>{@link Deque#getLast getLast()}</td> 93 * <td>{@link Deque#peekLast peekLast()}</td> 94 * </tr> 95 * </table> 96 * 97 * <p>This interface extends the {@link Queue} interface. When a deque is 98 * used as a queue, FIFO (First-In-First-Out) behavior results. Elements are 99 * added at the end of the deque and removed from the beginning. The methods 100 * inherited from the {@code Queue} interface are precisely equivalent to 101 * {@code Deque} methods as indicated in the following table: 102 * 103 * <table BORDER CELLPADDING=3 CELLSPACING=1> 104 * <caption>Comparison of Queue and Deque methods</caption> 105 * <tr> 106 * <td ALIGN=CENTER> <b>{@code Queue} Method</b></td> 107 * <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td> 108 * </tr> 109 * <tr> 110 * <td>{@link java.util.Queue#add add(e)}</td> 111 * <td>{@link #addLast addLast(e)}</td> 112 * </tr> 113 * <tr> 114 * <td>{@link java.util.Queue#offer offer(e)}</td> 115 * <td>{@link #offerLast offerLast(e)}</td> 116 * </tr> 117 * <tr> 118 * <td>{@link java.util.Queue#remove remove()}</td> 119 * <td>{@link #removeFirst removeFirst()}</td> 120 * </tr> 121 * <tr> 122 * <td>{@link java.util.Queue#poll poll()}</td> 123 * <td>{@link #pollFirst pollFirst()}</td> 124 * </tr> 125 * <tr> 126 * <td>{@link java.util.Queue#element element()}</td> 127 * <td>{@link #getFirst getFirst()}</td> 128 * </tr> 129 * <tr> 130 * <td>{@link java.util.Queue#peek peek()}</td> 131 * <td>{@link #peek peekFirst()}</td> 132 * </tr> 133 * </table> 134 * 135 * <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This 136 * interface should be used in preference to the legacy {@link Stack} class. 137 * When a deque is used as a stack, elements are pushed and popped from the 138 * beginning of the deque. Stack methods are precisely equivalent to 139 * {@code Deque} methods as indicated in the table below: 140 * 141 * <table BORDER CELLPADDING=3 CELLSPACING=1> 142 * <caption>Comparison of Stack and Deque methods</caption> 143 * <tr> 144 * <td ALIGN=CENTER> <b>Stack Method</b></td> 145 * <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td> 146 * </tr> 147 * <tr> 148 * <td>{@link #push push(e)}</td> 149 * <td>{@link #addFirst addFirst(e)}</td> 150 * </tr> 151 * <tr> 152 * <td>{@link #pop pop()}</td> 153 * <td>{@link #removeFirst removeFirst()}</td> 154 * </tr> 155 * <tr> 156 * <td>{@link #peek peek()}</td> 157 * <td>{@link #peekFirst peekFirst()}</td> 158 * </tr> 159 * </table> 160 * 161 * <p>Note that the {@link #peek peek} method works equally well when 162 * a deque is used as a queue or a stack; in either case, elements are 163 * drawn from the beginning of the deque. 164 * 165 * <p>This interface provides two methods to remove interior 166 * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and 167 * {@link #removeLastOccurrence removeLastOccurrence}. 168 * 169 * <p>Unlike the {@link List} interface, this interface does not 170 * provide support for indexed access to elements. 171 * 172 * <p>While {@code Deque} implementations are not strictly required 173 * to prohibit the insertion of null elements, they are strongly 174 * encouraged to do so. Users of any {@code Deque} implementations 175 * that do allow null elements are strongly encouraged <i>not</i> to 176 * take advantage of the ability to insert nulls. This is so because 177 * {@code null} is used as a special return value by various methods 178 * to indicated that the deque is empty. 179 * 180 * <p>{@code Deque} implementations generally do not define 181 * element-based versions of the {@code equals} and {@code hashCode} 182 * methods, but instead inherit the identity-based versions from class 183 * {@code Object}. 184 * 185 * @author Doug Lea 186 * @author Josh Bloch 187 * @since 1.6 188 * @param <E> the type of elements held in this deque 189 */ 190 public interface Deque<E> extends Queue<E> { 191 // Android-changed: fix framework docs link to "Collection#optional-restrictions" 192 // Several occurrences of the link have been fixed throughout. 193 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 */ addFirst(E e)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 */ addLast(E e)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 */ offerFirst(E e)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 */ offerLast(E e)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 */ removeFirst()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 */ removeLast()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 */ pollFirst()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 */ pollLast()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 */ getFirst()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 */ getLast()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 */ peekFirst()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 */ peekLast()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="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="Collection.html#optional-restrictions">optional</a>) 359 */ removeFirstOccurrence(Object o)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="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="Collection.html#optional-restrictions">optional</a>) 378 */ removeLastOccurrence(Object o)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 */ add(E e)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 */ offer(E e)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 throws an 434 * 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 */ remove()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 */ poll()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 */ element()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 */ peek()478 E peek(); 479 480 481 // *** Stack methods *** 482 483 /** 484 * Pushes an element onto the stack represented by this deque (in other 485 * words, at the head of this deque) if it is possible to do so 486 * immediately without violating capacity restrictions, throwing an 487 * {@code IllegalStateException} if no space is currently available. 488 * 489 * <p>This method is equivalent to {@link #addFirst}. 490 * 491 * @param e the element to push 492 * @throws IllegalStateException if the element cannot be added at this 493 * time due to capacity restrictions 494 * @throws ClassCastException if the class of the specified element 495 * prevents it from being added to this deque 496 * @throws NullPointerException if the specified element is null and this 497 * deque does not permit null elements 498 * @throws IllegalArgumentException if some property of the specified 499 * element prevents it from being added to this deque 500 */ push(E e)501 void push(E e); 502 503 /** 504 * Pops an element from the stack represented by this deque. In other 505 * words, removes and returns the first element of this deque. 506 * 507 * <p>This method is equivalent to {@link #removeFirst()}. 508 * 509 * @return the element at the front of this deque (which is the top 510 * of the stack represented by this deque) 511 * @throws NoSuchElementException if this deque is empty 512 */ pop()513 E pop(); 514 515 516 // *** Collection methods *** 517 518 /** 519 * Removes the first occurrence of the specified element from this deque. 520 * If the deque does not contain the element, it is unchanged. 521 * More formally, removes the first element {@code e} such that 522 * {@code Objects.equals(o, e)} (if such an element exists). 523 * Returns {@code true} if this deque contained the specified element 524 * (or equivalently, if this deque changed as a result of the call). 525 * 526 * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}. 527 * 528 * @param o element to be removed from this deque, if present 529 * @return {@code true} if an element was removed as a result of this call 530 * @throws ClassCastException if the class of the specified element 531 * is incompatible with this deque 532 * (<a href="Collection.html#optional-restrictions">optional</a>) 533 * @throws NullPointerException if the specified element is null and this 534 * deque does not permit null elements 535 * (<a href="Collection.html#optional-restrictions">optional</a>) 536 */ remove(Object o)537 boolean remove(Object o); 538 539 /** 540 * Returns {@code true} if this deque contains the specified element. 541 * More formally, returns {@code true} if and only if this deque contains 542 * at least one element {@code e} such that {@code Objects.equals(o, e)}. 543 * 544 * @param o element whose presence in this deque is to be tested 545 * @return {@code true} if this deque contains the specified element 546 * @throws ClassCastException if the class of the specified element 547 * is incompatible with this deque 548 * (<a href="Collection.html#optional-restrictions">optional</a>) 549 * @throws NullPointerException if the specified element is null and this 550 * deque does not permit null elements 551 * (<a href="Collection.html#optional-restrictions">optional</a>) 552 */ contains(Object o)553 boolean contains(Object o); 554 555 /** 556 * Returns the number of elements in this deque. 557 * 558 * @return the number of elements in this deque 559 */ size()560 int size(); 561 562 /** 563 * Returns an iterator over the elements in this deque in proper sequence. 564 * The elements will be returned in order from first (head) to last (tail). 565 * 566 * @return an iterator over the elements in this deque in proper sequence 567 */ iterator()568 Iterator<E> iterator(); 569 570 /** 571 * Returns an iterator over the elements in this deque in reverse 572 * sequential order. The elements will be returned in order from 573 * last (tail) to first (head). 574 * 575 * @return an iterator over the elements in this deque in reverse 576 * sequence 577 */ descendingIterator()578 Iterator<E> descendingIterator(); 579 580 } 581