1 |
|
/* |
2 |
|
* Written by Josh Bloch of Google Inc. and released to the public domain, |
3 |
< |
* as explained at http://creativecommons.org/licenses/publicdomain. |
3 |
> |
* as explained at http://creativecommons.org/publicdomain/zero/1.0/. |
4 |
|
*/ |
5 |
|
|
6 |
|
package jsr166x; // XXX This belongs in java.util!!! XXX |
13 |
|
* usage. They are not thread-safe; in the absence of external |
14 |
|
* synchronization, they do not support concurrent access by multiple threads. |
15 |
|
* Null elements are prohibited. This class is likely to be faster than |
16 |
< |
* {@link Stack} when used as as a stack, and faster than {@link LinkedList} |
16 |
> |
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
17 |
|
* when used as a queue. |
18 |
|
* |
19 |
< |
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
19 |
> |
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
20 |
|
* Exceptions include {@link #remove(Object) remove}, {@link |
21 |
|
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
22 |
|
* removeLastOccurrence}, {@link #contains contains }, {@link #iterator |
23 |
|
* iterator.remove()}, and the bulk operations, all of which run in linear |
24 |
|
* time. |
25 |
|
* |
26 |
< |
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
26 |
> |
* <p>The iterators returned by this class's {@code iterator} method are |
27 |
|
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
28 |
|
* is created, in any way except through the iterator's own remove method, the |
29 |
|
* iterator will generally throw a {@link ConcurrentModificationException}. |
34 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
35 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
36 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
37 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
37 |
> |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
38 |
|
* Therefore, it would be wrong to write a program that depended on this |
39 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
40 |
|
* should be used only to detect bugs.</i> |
86 |
|
// ****** Array allocation and resizing utilities ****** |
87 |
|
|
88 |
|
/** |
89 |
< |
* Allocate empty array to hold the given number of elements. |
89 |
> |
* Allocates empty array to hold the given number of elements. |
90 |
|
* |
91 |
< |
* @param numElements the number of elements to hold. |
91 |
> |
* @param numElements the number of elements to hold |
92 |
|
*/ |
93 |
|
private void allocateElements(int numElements) { |
94 |
|
int initialCapacity = MIN_INITIAL_CAPACITY; |
110 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
< |
* Double the capacity of this deque. Call only when full, i.e., |
113 |
> |
* Doubles the capacity of this deque. Call only when full, i.e., |
114 |
|
* when head and tail have wrapped around to become equal. |
115 |
|
*/ |
116 |
|
private void doubleCapacity() { |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* Copy the elements from our element array into the specified array, |
133 |
> |
* Copies the elements from our element array into the specified array, |
134 |
|
* in order (from first to last element in the deque). It is assumed |
135 |
|
* that the array is large enough to hold all elements in the deque. |
136 |
|
* |
188 |
|
* Inserts the specified element to the front this deque. |
189 |
|
* |
190 |
|
* @param e the element to insert |
191 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
191 |
> |
* @throws NullPointerException if {@code e} is null |
192 |
|
*/ |
193 |
|
public void addFirst(E e) { |
194 |
|
if (e == null) |
204 |
|
* {@link #push}. |
205 |
|
* |
206 |
|
* @param e the element to insert |
207 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
207 |
> |
* @throws NullPointerException if {@code e} is null |
208 |
|
*/ |
209 |
|
public void addLast(E e) { |
210 |
|
if (e == null) |
216 |
|
|
217 |
|
/** |
218 |
|
* Retrieves and removes the first element of this deque, or |
219 |
< |
* <tt>null</tt> if this deque is empty. |
219 |
> |
* {@code null} if this deque is empty. |
220 |
|
* |
221 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
221 |
> |
* @return the first element of this deque, or {@code null} if |
222 |
|
* this deque is empty |
223 |
|
*/ |
224 |
|
public E pollFirst() { |
233 |
|
|
234 |
|
/** |
235 |
|
* Retrieves and removes the last element of this deque, or |
236 |
< |
* <tt>null</tt> if this deque is empty. |
236 |
> |
* {@code null} if this deque is empty. |
237 |
|
* |
238 |
< |
* @return the last element of this deque, or <tt>null</tt> if |
238 |
> |
* @return the last element of this deque, or {@code null} if |
239 |
|
* this deque is empty |
240 |
|
*/ |
241 |
|
public E pollLast() { |
252 |
|
* Inserts the specified element to the front this deque. |
253 |
|
* |
254 |
|
* @param e the element to insert |
255 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
256 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
255 |
> |
* @return {@code true} (as per the spec for {@link Deque#offerFirst}) |
256 |
> |
* @throws NullPointerException if {@code e} is null |
257 |
|
*/ |
258 |
|
public boolean offerFirst(E e) { |
259 |
|
addFirst(e); |
264 |
|
* Inserts the specified element to the end this deque. |
265 |
|
* |
266 |
|
* @param e the element to insert |
267 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
268 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
267 |
> |
* @return {@code true} (as per the spec for {@link Deque#offerLast}) |
268 |
> |
* @throws NullPointerException if {@code e} is null |
269 |
|
*/ |
270 |
|
public boolean offerLast(E e) { |
271 |
|
addLast(e); |
274 |
|
|
275 |
|
/** |
276 |
|
* Retrieves and removes the first element of this deque. This method |
277 |
< |
* differs from the <tt>pollFirst</tt> method in that it throws an |
277 |
> |
* differs from the {@code pollFirst} method in that it throws an |
278 |
|
* exception if this deque is empty. |
279 |
|
* |
280 |
|
* @return the first element of this deque |
289 |
|
|
290 |
|
/** |
291 |
|
* Retrieves and removes the last element of this deque. This method |
292 |
< |
* differs from the <tt>pollLast</tt> method in that it throws an |
292 |
> |
* differs from the {@code pollLast} method in that it throws an |
293 |
|
* exception if this deque is empty. |
294 |
|
* |
295 |
|
* @return the last element of this deque |
304 |
|
|
305 |
|
/** |
306 |
|
* Retrieves, but does not remove, the first element of this deque, |
307 |
< |
* returning <tt>null</tt> if this deque is empty. |
307 |
> |
* returning {@code null} if this deque is empty. |
308 |
|
* |
309 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
309 |
> |
* @return the first element of this deque, or {@code null} if |
310 |
|
* this deque is empty |
311 |
|
*/ |
312 |
|
public E peekFirst() { |
315 |
|
|
316 |
|
/** |
317 |
|
* Retrieves, but does not remove, the last element of this deque, |
318 |
< |
* returning <tt>null</tt> if this deque is empty. |
318 |
> |
* returning {@code null} if this deque is empty. |
319 |
|
* |
320 |
< |
* @return the last element of this deque, or <tt>null</tt> if this deque |
320 |
> |
* @return the last element of this deque, or {@code null} if this deque |
321 |
|
* is empty |
322 |
|
*/ |
323 |
|
public E peekLast() { |
326 |
|
|
327 |
|
/** |
328 |
|
* Retrieves, but does not remove, the first element of this |
329 |
< |
* deque. This method differs from the <tt>peek</tt> method only |
329 |
> |
* deque. This method differs from the {@code peek} method only |
330 |
|
* in that it throws an exception if this deque is empty. |
331 |
|
* |
332 |
|
* @return the first element of this deque |
341 |
|
|
342 |
|
/** |
343 |
|
* Retrieves, but does not remove, the last element of this |
344 |
< |
* deque. This method differs from the <tt>peek</tt> method only |
344 |
> |
* deque. This method differs from the {@code peek} method only |
345 |
|
* in that it throws an exception if this deque is empty. |
346 |
|
* |
347 |
|
* @return the last element of this deque |
360 |
|
* does not contain the element, it is unchanged. |
361 |
|
* |
362 |
|
* @param e element to be removed from this deque, if present |
363 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
363 |
> |
* @return {@code true} if the deque contained the specified element |
364 |
|
*/ |
365 |
|
public boolean removeFirstOccurrence(Object e) { |
366 |
|
if (e == null) |
384 |
|
* does not contain the element, it is unchanged. |
385 |
|
* |
386 |
|
* @param e element to be removed from this deque, if present |
387 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
387 |
> |
* @return {@code true} if the deque contained the specified element |
388 |
|
*/ |
389 |
|
public boolean removeLastOccurrence(Object e) { |
390 |
|
if (e == null) |
410 |
|
* <p>This method is equivalent to {@link #offerLast}. |
411 |
|
* |
412 |
|
* @param e the element to insert |
413 |
< |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
414 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
413 |
> |
* @return {@code true} (as per the spec for {@link Queue#offer}) |
414 |
> |
* @throws NullPointerException if {@code e} is null |
415 |
|
*/ |
416 |
|
public boolean offer(E e) { |
417 |
|
return offerLast(e); |
423 |
|
* <p>This method is equivalent to {@link #addLast}. |
424 |
|
* |
425 |
|
* @param e the element to insert |
426 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
427 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
426 |
> |
* @return {@code true} (as per the spec for {@link Collection#add}) |
427 |
> |
* @throws NullPointerException if {@code e} is null |
428 |
|
*/ |
429 |
|
public boolean add(E e) { |
430 |
|
addLast(e); |
433 |
|
|
434 |
|
/** |
435 |
|
* Retrieves and removes the head of the queue represented by |
436 |
< |
* this deque, or <tt>null</tt> if this deque is empty. In other words, |
437 |
< |
* retrieves and removes the first element of this deque, or <tt>null</tt> |
436 |
> |
* this deque, or {@code null} if this deque is empty. In other words, |
437 |
> |
* retrieves and removes the first element of this deque, or {@code null} |
438 |
|
* if this deque is empty. |
439 |
|
* |
440 |
|
* <p>This method is equivalent to {@link #pollFirst}. |
441 |
|
* |
442 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
442 |
> |
* @return the first element of this deque, or {@code null} if |
443 |
|
* this deque is empty |
444 |
|
*/ |
445 |
|
public E poll() { |
448 |
|
|
449 |
|
/** |
450 |
|
* Retrieves and removes the head of the queue represented by this deque. |
451 |
< |
* This method differs from the <tt>poll</tt> method in that it throws an |
451 |
> |
* This method differs from the {@code poll} method in that it throws an |
452 |
|
* exception if this deque is empty. |
453 |
|
* |
454 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
462 |
|
|
463 |
|
/** |
464 |
|
* Retrieves, but does not remove, the head of the queue represented by |
465 |
< |
* this deque, returning <tt>null</tt> if this deque is empty. |
465 |
> |
* this deque, returning {@code null} if this deque is empty. |
466 |
|
* |
467 |
< |
* <p>This method is equivalent to {@link #peekFirst} |
467 |
> |
* <p>This method is equivalent to {@link #peekFirst}. |
468 |
|
* |
469 |
|
* @return the head of the queue represented by this deque, or |
470 |
< |
* <tt>null</tt> if this deque is empty |
470 |
> |
* {@code null} if this deque is empty |
471 |
|
*/ |
472 |
|
public E peek() { |
473 |
|
return peekFirst(); |
475 |
|
|
476 |
|
/** |
477 |
|
* Retrieves, but does not remove, the head of the queue represented by |
478 |
< |
* this deque. This method differs from the <tt>peek</tt> method only in |
478 |
> |
* this deque. This method differs from the {@code peek} method only in |
479 |
|
* that it throws an exception if this deque is empty. |
480 |
|
* |
481 |
< |
* <p>This method is equivalent to {@link #getFirst} |
481 |
> |
* <p>This method is equivalent to {@link #getFirst}. |
482 |
|
* |
483 |
|
* @return the head of the queue represented by this deque |
484 |
|
* @throws NoSuchElementException if this deque is empty |
496 |
|
* <p>This method is equivalent to {@link #addFirst}. |
497 |
|
* |
498 |
|
* @param e the element to push |
499 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
499 |
> |
* @throws NullPointerException if {@code e} is null |
500 |
|
*/ |
501 |
|
public void push(E e) { |
502 |
|
addFirst(e); |
504 |
|
|
505 |
|
/** |
506 |
|
* Pops an element from the stack represented by this deque. In other |
507 |
< |
* words, removes and returns the the first element of this deque. |
507 |
> |
* words, removes and returns the first element of this deque. |
508 |
|
* |
509 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
510 |
|
* |
517 |
|
} |
518 |
|
|
519 |
|
/** |
520 |
< |
* Remove the element at the specified position in the elements array, |
520 |
> |
* Removes the element at the specified position in the elements array, |
521 |
|
* adjusting head, tail, and size as necessary. This can result in |
522 |
|
* motion of elements backwards or forwards in the array. |
523 |
|
* |
555 |
|
} |
556 |
|
|
557 |
|
/** |
558 |
< |
* Returns <tt>true</tt> if this collection contains no elements.<p> |
558 |
> |
* Returns {@code true} if this collection contains no elements. |
559 |
|
* |
560 |
< |
* @return <tt>true</tt> if this collection contains no elements. |
560 |
> |
* @return {@code true} if this collection contains no elements |
561 |
|
*/ |
562 |
|
public boolean isEmpty() { |
563 |
|
return head == tail; |
569 |
|
* order that elements would be dequeued (via successive calls to |
570 |
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
571 |
|
* |
572 |
< |
* @return an <tt>Iterator</tt> over the elements in this deque |
572 |
> |
* @return an {@code Iterator} over the elements in this deque |
573 |
|
*/ |
574 |
|
public Iterator<E> iterator() { |
575 |
|
return new DeqIterator(); |
621 |
|
} |
622 |
|
|
623 |
|
/** |
624 |
< |
* Returns <tt>true</tt> if this deque contains the specified |
625 |
< |
* element. More formally, returns <tt>true</tt> if and only if this |
626 |
< |
* deque contains at least one element <tt>e</tt> such that |
627 |
< |
* <tt>e.equals(o)</tt>. |
624 |
> |
* Returns {@code true} if this deque contains the specified |
625 |
> |
* element. More formally, returns {@code true} if and only if this |
626 |
> |
* deque contains at least one element {@code e} such that |
627 |
> |
* {@code e.equals(o)}. |
628 |
|
* |
629 |
|
* @param o object to be checked for containment in this deque |
630 |
< |
* @return <tt>true</tt> if this deque contains the specified element |
630 |
> |
* @return {@code true} if this deque contains the specified element |
631 |
|
*/ |
632 |
|
public boolean contains(Object o) { |
633 |
|
if (o == null) |
648 |
|
* This method is equivalent to {@link #removeFirstOccurrence}. |
649 |
|
* |
650 |
|
* @param e element to be removed from this deque, if present |
651 |
< |
* @return <tt>true</tt> if this deque contained the specified element |
651 |
> |
* @return {@code true} if this deque contained the specified element |
652 |
|
*/ |
653 |
|
public boolean remove(Object e) { |
654 |
|
return removeFirstOccurrence(e); |
667 |
|
do { |
668 |
|
elements[i] = null; |
669 |
|
i = (i + 1) & mask; |
670 |
< |
} while(i != t); |
670 |
> |
} while (i != t); |
671 |
|
} |
672 |
|
} |
673 |
|
|
691 |
|
* |
692 |
|
* <p>If the deque fits in the specified array with room to spare (i.e., |
693 |
|
* the array has more elements than the deque), the element in the array |
694 |
< |
* immediately following the end of the collection is set to <tt>null</tt>. |
694 |
> |
* immediately following the end of the collection is set to {@code null}. |
695 |
|
* |
696 |
|
* @param a the array into which the elements of the deque are to |
697 |
|
* be stored, if it is big enough; otherwise, a new array of the |
737 |
|
private static final long serialVersionUID = 2340985798034038923L; |
738 |
|
|
739 |
|
/** |
740 |
< |
* Serialize this deque. |
740 |
> |
* Serializes this deque. |
741 |
|
* |
742 |
< |
* @serialData The current size (<tt>int</tt>) of the deque, |
742 |
> |
* @serialData The current size ({@code int}) of the deque, |
743 |
|
* followed by all of its elements (each an object reference) in |
744 |
|
* first-to-last order. |
745 |
|
*/ |
760 |
|
} |
761 |
|
|
762 |
|
/** |
763 |
< |
* Deserialize this deque. |
763 |
> |
* Deserializes this deque. |
764 |
|
*/ |
765 |
|
private void readObject(ObjectInputStream s) |
766 |
|
throws IOException, ClassNotFoundException { |