ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayDeque.java
(Generate patch)

Comparing jsr166/src/main/java/util/ArrayDeque.java (file contents):
Revision 1.12 by jsr166, Tue May 17 16:14:34 2005 UTC vs.
Revision 1.17 by dl, Thu Sep 15 16:55:24 2005 UTC

# Line 4 | Line 4
4   */
5  
6   package java.util;
7 + import java.util.*; // for javadoc (till 6280605 is fixed)
8   import java.io.*;
9  
10   /**
# Line 202 | Line 203 | public class ArrayDeque<E> extends Abstr
203  
204      /**
205       * Inserts the specified element at the end of this deque.
206 <     * This method is equivalent to {@link #add} and {@link #push}.
206 >     *
207 >     * <p>This method is equivalent to {@link #add}.
208       *
209       * @param e the element to add
210       * @throws NullPointerException if the specified element is null
# Line 219 | Line 221 | public class ArrayDeque<E> extends Abstr
221       * Inserts the specified element at the front of this deque.
222       *
223       * @param e the element to add
224 <     * @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst})
224 >     * @return <tt>true</tt> (as specified by {@link Deque#offerFirst})
225       * @throws NullPointerException if the specified element is null
226       */
227      public boolean offerFirst(E e) {
# Line 231 | Line 233 | public class ArrayDeque<E> extends Abstr
233       * Inserts the specified element at the end of this deque.
234       *
235       * @param e the element to add
236 <     * @return <tt>true</tt> (as per the spec for {@link Deque#offerLast})
236 >     * @return <tt>true</tt> (as specified by {@link Deque#offerLast})
237       * @throws NullPointerException if the specified element is null
238       */
239      public boolean offerLast(E e) {
# Line 371 | Line 373 | public class ArrayDeque<E> extends Abstr
373       * <p>This method is equivalent to {@link #addLast}.
374       *
375       * @param e the element to add
376 <     * @return <tt>true</tt> (as per the spec for {@link Collection#add})
376 >     * @return <tt>true</tt> (as specified by {@link Collection#add})
377       * @throws NullPointerException if the specified element is null
378       */
379      public boolean add(E e) {
# Line 385 | Line 387 | public class ArrayDeque<E> extends Abstr
387       * <p>This method is equivalent to {@link #offerLast}.
388       *
389       * @param e the element to add
390 <     * @return <tt>true</tt> (as per the spec for {@link Queue#offer})
390 >     * @return <tt>true</tt> (as specified by {@link Queue#offer})
391       * @throws NullPointerException if the specified element is null
392       */
393      public boolean offer(E e) {
# Line 394 | Line 396 | public class ArrayDeque<E> extends Abstr
396  
397      /**
398       * Retrieves and removes the head of the queue represented by this deque.
399 <     * This method differs from {@link #poll} only in that it throws an
399 >     *
400 >     * This method differs from {@link #poll poll} only in that it throws an
401       * exception if this deque is empty.
402       *
403       * <p>This method is equivalent to {@link #removeFirst}.
# Line 422 | Line 425 | public class ArrayDeque<E> extends Abstr
425  
426      /**
427       * Retrieves, but does not remove, the head of the queue represented by
428 <     * this deque.  This method differs from {@link #peek} only in that it
429 <     * throws an exception if this deque is empty.
428 >     * this deque.  This method differs from {@link #peek peek} only in
429 >     * that it throws an exception if this deque is empty.
430       *
431       * <p>This method is equivalent to {@link #getFirst}.
432       *
# Line 541 | Line 544 | public class ArrayDeque<E> extends Abstr
544          return new DeqIterator();
545      }
546  
547 +    /**
548 +     * Returns an iterator over the elements in this deque in reverse
549 +     * sequential order.  The elements will be returned in order from
550 +     * last (tail) to first (head).
551 +     *
552 +     * @return an iterator over the elements in this deque in reverse
553 +     * sequence
554 +     */
555 +    public Iterator<E> descendingIterator() {
556 +        return new DescendingIterator();
557 +    }
558 +
559      private class DeqIterator implements Iterator<E> {
560          /**
561           * Index of element to be returned by subsequent call to next.
# Line 579 | Line 594 | public class ArrayDeque<E> extends Abstr
594          public void remove() {
595              if (lastRet < 0)
596                  throw new IllegalStateException();
597 <            if (delete(lastRet))
598 <                cursor--;
597 >            if (delete(lastRet)) // if left-shifted, undo increment in next()
598 >                cursor = (cursor - 1) & (elements.length - 1);
599              lastRet = -1;
600              fence = tail;
601          }
602      }
603  
604 +
605 +    private class DescendingIterator implements Iterator<E> {
606 +        /*
607 +         * This class is nearly a mirror-image of DeqIterator, using
608 +         * (tail-1) instead of head for initial cursor, (head-1)
609 +         * instead of tail for fence, and elements.length instead of -1
610 +         * for sentinel. It shares the same structure, but not many
611 +         * actual lines of code.
612 +         */
613 +        private int cursor = (tail - 1) & (elements.length - 1);
614 +        private int fence =  (head - 1) & (elements.length - 1);
615 +        private int lastRet = elements.length;
616 +
617 +        public boolean hasNext() {
618 +            return cursor != fence;
619 +        }
620 +
621 +        public E next() {
622 +            E result;
623 +            if (cursor == fence)
624 +                throw new NoSuchElementException();
625 +            if (((head - 1) & (elements.length - 1)) != fence ||
626 +                (result = elements[cursor]) == null)
627 +                throw new ConcurrentModificationException();
628 +            lastRet = cursor;
629 +            cursor = (cursor - 1) & (elements.length - 1);
630 +            return result;
631 +        }
632 +
633 +        public void remove() {
634 +            if (lastRet >= elements.length)
635 +                throw new IllegalStateException();
636 +            if (!delete(lastRet))
637 +                cursor = (cursor + 1) & (elements.length - 1);
638 +            lastRet = elements.length;
639 +            fence = (head - 1) & (elements.length - 1);
640 +        }
641 +    }
642 +
643      /**
644       * Returns <tt>true</tt> if this deque contains the specified element.
645       * More formally, returns <tt>true</tt> if and only if this deque contains
# Line 650 | Line 704 | public class ArrayDeque<E> extends Abstr
704       * <p>The returned array will be "safe" in that no references to it are
705       * maintained by this deque.  (In other words, this method must allocate
706       * a new array).  The caller is thus free to modify the returned array.
707 <     *
707 >     *
708       * <p>This method acts as bridge between array-based and collection-based
709       * APIs.
710       *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines