4 |
|
*/ |
5 |
|
|
6 |
|
package jsr166x; // XXX This belongs in java.util!!! XXX |
7 |
+ |
|
8 |
|
import java.util.*; // XXX This import goes away XXX |
9 |
|
import java.io.*; |
10 |
|
|
17 |
|
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
18 |
|
* when used as a queue. |
19 |
|
* |
20 |
< |
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
20 |
> |
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
21 |
|
* Exceptions include {@link #remove(Object) remove}, {@link |
22 |
|
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
23 |
|
* removeLastOccurrence}, {@link #contains contains }, {@link #iterator |
24 |
|
* iterator.remove()}, and the bulk operations, all of which run in linear |
25 |
|
* time. |
26 |
|
* |
27 |
< |
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
27 |
> |
* <p>The iterators returned by this class's {@code iterator} method are |
28 |
|
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
29 |
|
* is created, in any way except through the iterator's own remove method, the |
30 |
|
* iterator will generally throw a {@link ConcurrentModificationException}. |
35 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
36 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
37 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
38 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
38 |
> |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
39 |
|
* Therefore, it would be wrong to write a program that depended on this |
40 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
41 |
|
* should be used only to detect bugs.</i> |
89 |
|
/** |
90 |
|
* Allocates empty array to hold the given number of elements. |
91 |
|
* |
92 |
< |
* @param numElements the number of elements to hold. |
92 |
> |
* @param numElements the number of elements to hold |
93 |
|
*/ |
94 |
|
private void allocateElements(int numElements) { |
95 |
|
int initialCapacity = MIN_INITIAL_CAPACITY; |
189 |
|
* Inserts the specified element to the front this deque. |
190 |
|
* |
191 |
|
* @param e the element to insert |
192 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
192 |
> |
* @throws NullPointerException if {@code e} is null |
193 |
|
*/ |
194 |
|
public void addFirst(E e) { |
195 |
|
if (e == null) |
205 |
|
* {@link #push}. |
206 |
|
* |
207 |
|
* @param e the element to insert |
208 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
208 |
> |
* @throws NullPointerException if {@code e} is null |
209 |
|
*/ |
210 |
|
public void addLast(E e) { |
211 |
|
if (e == null) |
217 |
|
|
218 |
|
/** |
219 |
|
* Retrieves and removes the first element of this deque, or |
220 |
< |
* <tt>null</tt> if this deque is empty. |
220 |
> |
* {@code null} if this deque is empty. |
221 |
|
* |
222 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
222 |
> |
* @return the first element of this deque, or {@code null} if |
223 |
|
* this deque is empty |
224 |
|
*/ |
225 |
|
public E pollFirst() { |
234 |
|
|
235 |
|
/** |
236 |
|
* Retrieves and removes the last element of this deque, or |
237 |
< |
* <tt>null</tt> if this deque is empty. |
237 |
> |
* {@code null} if this deque is empty. |
238 |
|
* |
239 |
< |
* @return the last element of this deque, or <tt>null</tt> if |
239 |
> |
* @return the last element of this deque, or {@code null} if |
240 |
|
* this deque is empty |
241 |
|
*/ |
242 |
|
public E pollLast() { |
253 |
|
* Inserts the specified element to the front this deque. |
254 |
|
* |
255 |
|
* @param e the element to insert |
256 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
257 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
256 |
> |
* @return {@code true} (as per the spec for {@link Deque#offerFirst}) |
257 |
> |
* @throws NullPointerException if {@code e} is null |
258 |
|
*/ |
259 |
|
public boolean offerFirst(E e) { |
260 |
|
addFirst(e); |
265 |
|
* Inserts the specified element to the end this deque. |
266 |
|
* |
267 |
|
* @param e the element to insert |
268 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
269 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
268 |
> |
* @return {@code true} (as per the spec for {@link Deque#offerLast}) |
269 |
> |
* @throws NullPointerException if {@code e} is null |
270 |
|
*/ |
271 |
|
public boolean offerLast(E e) { |
272 |
|
addLast(e); |
275 |
|
|
276 |
|
/** |
277 |
|
* Retrieves and removes the first element of this deque. This method |
278 |
< |
* differs from the <tt>pollFirst</tt> method in that it throws an |
278 |
> |
* differs from the {@code pollFirst} method in that it throws an |
279 |
|
* exception if this deque is empty. |
280 |
|
* |
281 |
|
* @return the first element of this deque |
290 |
|
|
291 |
|
/** |
292 |
|
* Retrieves and removes the last element of this deque. This method |
293 |
< |
* differs from the <tt>pollLast</tt> method in that it throws an |
293 |
> |
* differs from the {@code pollLast} method in that it throws an |
294 |
|
* exception if this deque is empty. |
295 |
|
* |
296 |
|
* @return the last element of this deque |
305 |
|
|
306 |
|
/** |
307 |
|
* Retrieves, but does not remove, the first element of this deque, |
308 |
< |
* returning <tt>null</tt> if this deque is empty. |
308 |
> |
* returning {@code null} if this deque is empty. |
309 |
|
* |
310 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
310 |
> |
* @return the first element of this deque, or {@code null} if |
311 |
|
* this deque is empty |
312 |
|
*/ |
313 |
|
public E peekFirst() { |
316 |
|
|
317 |
|
/** |
318 |
|
* Retrieves, but does not remove, the last element of this deque, |
319 |
< |
* returning <tt>null</tt> if this deque is empty. |
319 |
> |
* returning {@code null} if this deque is empty. |
320 |
|
* |
321 |
< |
* @return the last element of this deque, or <tt>null</tt> if this deque |
321 |
> |
* @return the last element of this deque, or {@code null} if this deque |
322 |
|
* is empty |
323 |
|
*/ |
324 |
|
public E peekLast() { |
327 |
|
|
328 |
|
/** |
329 |
|
* Retrieves, but does not remove, the first element of this |
330 |
< |
* deque. This method differs from the <tt>peek</tt> method only |
330 |
> |
* deque. This method differs from the {@code peek} method only |
331 |
|
* in that it throws an exception if this deque is empty. |
332 |
|
* |
333 |
|
* @return the first element of this deque |
342 |
|
|
343 |
|
/** |
344 |
|
* Retrieves, but does not remove, the last element of this |
345 |
< |
* deque. This method differs from the <tt>peek</tt> method only |
345 |
> |
* deque. This method differs from the {@code peek} method only |
346 |
|
* in that it throws an exception if this deque is empty. |
347 |
|
* |
348 |
|
* @return the last element of this deque |
361 |
|
* does not contain the element, it is unchanged. |
362 |
|
* |
363 |
|
* @param e element to be removed from this deque, if present |
364 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
364 |
> |
* @return {@code true} if the deque contained the specified element |
365 |
|
*/ |
366 |
|
public boolean removeFirstOccurrence(Object e) { |
367 |
|
if (e == null) |
385 |
|
* does not contain the element, it is unchanged. |
386 |
|
* |
387 |
|
* @param e element to be removed from this deque, if present |
388 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
388 |
> |
* @return {@code true} if the deque contained the specified element |
389 |
|
*/ |
390 |
|
public boolean removeLastOccurrence(Object e) { |
391 |
|
if (e == null) |
411 |
|
* <p>This method is equivalent to {@link #offerLast}. |
412 |
|
* |
413 |
|
* @param e the element to insert |
414 |
< |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
415 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
414 |
> |
* @return {@code true} (as per the spec for {@link Queue#offer}) |
415 |
> |
* @throws NullPointerException if {@code e} is null |
416 |
|
*/ |
417 |
|
public boolean offer(E e) { |
418 |
|
return offerLast(e); |
424 |
|
* <p>This method is equivalent to {@link #addLast}. |
425 |
|
* |
426 |
|
* @param e the element to insert |
427 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
428 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
427 |
> |
* @return {@code true} (as per the spec for {@link Collection#add}) |
428 |
> |
* @throws NullPointerException if {@code e} is null |
429 |
|
*/ |
430 |
|
public boolean add(E e) { |
431 |
|
addLast(e); |
434 |
|
|
435 |
|
/** |
436 |
|
* Retrieves and removes the head of the queue represented by |
437 |
< |
* this deque, or <tt>null</tt> if this deque is empty. In other words, |
438 |
< |
* retrieves and removes the first element of this deque, or <tt>null</tt> |
437 |
> |
* this deque, or {@code null} if this deque is empty. In other words, |
438 |
> |
* retrieves and removes the first element of this deque, or {@code null} |
439 |
|
* if this deque is empty. |
440 |
|
* |
441 |
|
* <p>This method is equivalent to {@link #pollFirst}. |
442 |
|
* |
443 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
443 |
> |
* @return the first element of this deque, or {@code null} if |
444 |
|
* this deque is empty |
445 |
|
*/ |
446 |
|
public E poll() { |
449 |
|
|
450 |
|
/** |
451 |
|
* Retrieves and removes the head of the queue represented by this deque. |
452 |
< |
* This method differs from the <tt>poll</tt> method in that it throws an |
452 |
> |
* This method differs from the {@code poll} method in that it throws an |
453 |
|
* exception if this deque is empty. |
454 |
|
* |
455 |
|
* <p>This method is equivalent to {@link #removeFirst}. |
463 |
|
|
464 |
|
/** |
465 |
|
* Retrieves, but does not remove, the head of the queue represented by |
466 |
< |
* this deque, returning <tt>null</tt> if this deque is empty. |
466 |
> |
* this deque, returning {@code null} if this deque is empty. |
467 |
|
* |
468 |
< |
* <p>This method is equivalent to {@link #peekFirst} |
468 |
> |
* <p>This method is equivalent to {@link #peekFirst}. |
469 |
|
* |
470 |
|
* @return the head of the queue represented by this deque, or |
471 |
< |
* <tt>null</tt> if this deque is empty |
471 |
> |
* {@code null} if this deque is empty |
472 |
|
*/ |
473 |
|
public E peek() { |
474 |
|
return peekFirst(); |
476 |
|
|
477 |
|
/** |
478 |
|
* Retrieves, but does not remove, the head of the queue represented by |
479 |
< |
* this deque. This method differs from the <tt>peek</tt> method only in |
479 |
> |
* this deque. This method differs from the {@code peek} method only in |
480 |
|
* that it throws an exception if this deque is empty. |
481 |
|
* |
482 |
< |
* <p>This method is equivalent to {@link #getFirst} |
482 |
> |
* <p>This method is equivalent to {@link #getFirst}. |
483 |
|
* |
484 |
|
* @return the head of the queue represented by this deque |
485 |
|
* @throws NoSuchElementException if this deque is empty |
497 |
|
* <p>This method is equivalent to {@link #addFirst}. |
498 |
|
* |
499 |
|
* @param e the element to push |
500 |
< |
* @throws NullPointerException if <tt>e</tt> is null |
500 |
> |
* @throws NullPointerException if {@code e} is null |
501 |
|
*/ |
502 |
|
public void push(E e) { |
503 |
|
addFirst(e); |
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* Returns <tt>true</tt> if this collection contains no elements.<p> |
559 |
> |
* Returns {@code true} if this collection contains no elements. |
560 |
|
* |
561 |
< |
* @return <tt>true</tt> if this collection contains no elements. |
561 |
> |
* @return {@code true} if this collection contains no elements |
562 |
|
*/ |
563 |
|
public boolean isEmpty() { |
564 |
|
return head == tail; |
570 |
|
* order that elements would be dequeued (via successive calls to |
571 |
|
* {@link #remove} or popped (via successive calls to {@link #pop}). |
572 |
|
* |
573 |
< |
* @return an <tt>Iterator</tt> over the elements in this deque |
573 |
> |
* @return an {@code Iterator} over the elements in this deque |
574 |
|
*/ |
575 |
|
public Iterator<E> iterator() { |
576 |
|
return new DeqIterator(); |
622 |
|
} |
623 |
|
|
624 |
|
/** |
625 |
< |
* Returns <tt>true</tt> if this deque contains the specified |
626 |
< |
* element. More formally, returns <tt>true</tt> if and only if this |
627 |
< |
* deque contains at least one element <tt>e</tt> such that |
628 |
< |
* <tt>e.equals(o)</tt>. |
625 |
> |
* Returns {@code true} if this deque contains the specified |
626 |
> |
* element. More formally, returns {@code true} if and only if this |
627 |
> |
* deque contains at least one element {@code e} such that |
628 |
> |
* {@code e.equals(o)}. |
629 |
|
* |
630 |
|
* @param o object to be checked for containment in this deque |
631 |
< |
* @return <tt>true</tt> if this deque contains the specified element |
631 |
> |
* @return {@code true} if this deque contains the specified element |
632 |
|
*/ |
633 |
|
public boolean contains(Object o) { |
634 |
|
if (o == null) |
649 |
|
* This method is equivalent to {@link #removeFirstOccurrence}. |
650 |
|
* |
651 |
|
* @param e element to be removed from this deque, if present |
652 |
< |
* @return <tt>true</tt> if this deque contained the specified element |
652 |
> |
* @return {@code true} if this deque contained the specified element |
653 |
|
*/ |
654 |
|
public boolean remove(Object e) { |
655 |
|
return removeFirstOccurrence(e); |
692 |
|
* |
693 |
|
* <p>If the deque fits in the specified array with room to spare (i.e., |
694 |
|
* the array has more elements than the deque), the element in the array |
695 |
< |
* immediately following the end of the collection is set to <tt>null</tt>. |
695 |
> |
* immediately following the end of the collection is set to {@code null}. |
696 |
|
* |
697 |
|
* @param a the array into which the elements of the deque are to |
698 |
|
* be stored, if it is big enough; otherwise, a new array of the |
740 |
|
/** |
741 |
|
* Serializes this deque. |
742 |
|
* |
743 |
< |
* @serialData The current size (<tt>int</tt>) of the deque, |
743 |
> |
* @serialData The current size ({@code int}) of the deque, |
744 |
|
* followed by all of its elements (each an object reference) in |
745 |
|
* first-to-last order. |
746 |
|
*/ |