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.50 by jsr166, Wed Feb 20 12:32:01 2013 UTC vs.
Revision 1.59 by jsr166, Sun Nov 23 18:06:50 2014 UTC

# Line 7 | Line 7 | package java.util;
7   import java.io.Serializable;
8   import java.util.function.Consumer;
9   import java.util.stream.Stream;
10 import java.util.stream.Streams;
10  
11   /**
12   * Resizable-array implementation of the {@link Deque} interface.  Array
# Line 19 | Line 18 | import java.util.stream.Streams;
18   * when used as a queue.
19   *
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.
21 > * Exceptions include
22 > * {@link #remove(Object) remove},
23 > * {@link #removeFirstOccurrence removeFirstOccurrence},
24 > * {@link #removeLastOccurrence removeLastOccurrence},
25 > * {@link #contains contains},
26 > * {@link #iterator iterator.remove()},
27 > * and the bulk operations, all of which run in linear time.
28   *
29 < * <p>The iterators returned by this class's {@code iterator} method are
30 < * <i>fail-fast</i>: If the deque is modified at any time after the iterator
31 < * is created, in any way except through the iterator's own {@code remove}
32 < * method, the iterator will generally throw a {@link
29 > * <p>The iterators returned by this class's {@link #iterator() iterator}
30 > * method are <em>fail-fast</em>: If the deque is modified at any time after
31 > * the iterator is created, in any way except through the iterator's own
32 > * {@code remove} method, the iterator will generally throw a {@link
33   * ConcurrentModificationException}.  Thus, in the face of concurrent
34   * modification, the iterator fails quickly and cleanly, rather than risking
35   * arbitrary, non-deterministic behavior at an undetermined time in the
# Line 314 | Line 315 | public class ArrayDeque<E> extends Abstr
315       * @return {@code true} if the deque contained the specified element
316       */
317      public boolean removeFirstOccurrence(Object o) {
318 <        if (o == null)
319 <            return false;
320 <        int mask = elements.length - 1;
321 <        int i = head;
322 <        Object x;
323 <        while ( (x = elements[i]) != null) {
324 <            if (o.equals(x)) {
325 <                delete(i);
325 <                return true;
318 >        if (o != null) {
319 >            int mask = elements.length - 1;
320 >            int i = head;
321 >            for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) {
322 >                if (o.equals(x)) {
323 >                    delete(i);
324 >                    return true;
325 >                }
326              }
327            i = (i + 1) & mask;
327          }
328          return false;
329      }
# Line 342 | Line 341 | public class ArrayDeque<E> extends Abstr
341       * @return {@code true} if the deque contained the specified element
342       */
343      public boolean removeLastOccurrence(Object o) {
344 <        if (o == null)
345 <            return false;
346 <        int mask = elements.length - 1;
347 <        int i = (tail - 1) & mask;
348 <        Object x;
349 <        while ( (x = elements[i]) != null) {
350 <            if (o.equals(x)) {
351 <                delete(i);
353 <                return true;
344 >        if (o != null) {
345 >            int mask = elements.length - 1;
346 >            int i = (tail - 1) & mask;
347 >            for (Object x; (x = elements[i]) != null; i = (i - 1) & mask) {
348 >                if (o.equals(x)) {
349 >                    delete(i);
350 >                    return true;
351 >                }
352              }
355            i = (i - 1) & mask;
353          }
354          return false;
355      }
# Line 611 | Line 608 | public class ArrayDeque<E> extends Abstr
608          }
609      }
610  
611 +    /**
612 +     * This class is nearly a mirror-image of DeqIterator, using tail
613 +     * instead of head for initial cursor, and head instead of tail
614 +     * for fence.
615 +     */
616      private class DescendingIterator implements Iterator<E> {
615        /*
616         * This class is nearly a mirror-image of DeqIterator, using
617         * tail instead of head for initial cursor, and head instead of
618         * tail for fence.
619         */
617          private int cursor = tail;
618          private int fence = head;
619          private int lastRet = -1;
# Line 657 | Line 654 | public class ArrayDeque<E> extends Abstr
654       * @return {@code true} if this deque contains the specified element
655       */
656      public boolean contains(Object o) {
657 <        if (o == null)
658 <            return false;
659 <        int mask = elements.length - 1;
660 <        int i = head;
661 <        Object x;
662 <        while ( (x = elements[i]) != null) {
663 <            if (o.equals(x))
667 <                return true;
668 <            i = (i + 1) & mask;
657 >        if (o != null) {
658 >            int mask = elements.length - 1;
659 >            int i = head;
660 >            for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) {
661 >                if (o.equals(x))
662 >                    return true;
663 >            }
664          }
665          return false;
666      }
# Line 809 | Line 804 | public class ArrayDeque<E> extends Abstr
804      /**
805       * Saves this deque to a stream (that is, serializes it).
806       *
807 +     * @param s the stream
808 +     * @throws java.io.IOException if an I/O error occurs
809       * @serialData The current size ({@code int}) of the deque,
810       * followed by all of its elements (each an object reference) in
811       * first-to-last order.
# Line 828 | Line 825 | public class ArrayDeque<E> extends Abstr
825  
826      /**
827       * Reconstitutes this deque from a stream (that is, deserializes it).
828 +     * @param s the stream
829 +     * @throws ClassNotFoundException if the class of a serialized object
830 +     *         could not be found
831 +     * @throws java.io.IOException if an I/O error occurs
832       */
833      private void readObject(java.io.ObjectInputStream s)
834              throws java.io.IOException, ClassNotFoundException {
# Line 844 | Line 845 | public class ArrayDeque<E> extends Abstr
845              elements[i] = s.readObject();
846      }
847  
848 <    Spliterator<E> spliterator() {
848 >    public Spliterator<E> spliterator() {
849          return new DeqSpliterator<E>(this, -1, -1);
850      }
851  
851    public Stream<E> stream() {
852        return Streams.stream(spliterator());
853    }
854
855    public Stream<E> parallelStream() {
856        return Streams.parallelStream(spliterator());
857    }
858
852      static final class DeqSpliterator<E> implements Spliterator<E> {
853          private final ArrayDeque<E> deq;
854          private int fence;  // -1 until first use
# Line 877 | Line 870 | public class ArrayDeque<E> extends Abstr
870              return t;
871          }
872  
873 <        public DeqSpliterator<E> trySplit() {
873 >        public Spliterator<E> trySplit() {
874              int t = getFence(), h = index, n = deq.elements.length;
875              if (h != t && ((h + 1) & (n - 1)) != t) {
876                  if (h > t)
# Line 888 | Line 881 | public class ArrayDeque<E> extends Abstr
881              return null;
882          }
883  
884 <        public void forEach(Consumer<? super E> consumer) {
884 >        public void forEachRemaining(Consumer<? super E> consumer) {
885              if (consumer == null)
886                  throw new NullPointerException();
887              Object[] a = deq.elements;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines