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.40 by jsr166, Sun Feb 26 22:43:03 2012 UTC vs.
Revision 1.45 by dl, Fri Feb 1 01:02:25 2013 UTC

# Line 1 | Line 1
1   /*
2 < * Written by Josh Bloch of Google Inc. and released to the public domain,
3 < * as explained at http://creativecommons.org/publicdomain/zero/1.0/.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   package java.util;
8 + import java.util.Spliterator;
9 + import java.util.stream.Stream;
10 + import java.util.stream.Streams;
11 + import java.util.function.Consumer;
12  
13   /**
14   * Resizable-array implementation of the {@link Deque} interface.  Array
# Line 15 | Line 20 | package java.util;
20   * when used as a queue.
21   *
22   * <p>Most {@code ArrayDeque} operations run in amortized constant time.
23 < * Exceptions include
24 < * {@link #remove(Object) remove},
25 < * {@link #removeFirstOccurrence removeFirstOccurrence},
26 < * {@link #removeLastOccurrence removeLastOccurrence},
27 < * {@link #contains contains},
23 < * {@link #iterator iterator.remove()},
24 < * and the bulk operations, all of which run in linear time.
23 > * Exceptions include {@link #remove(Object) remove}, {@link
24 > * #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence
25 > * removeLastOccurrence}, {@link #contains contains}, {@link #iterator
26 > * iterator.remove()}, and the bulk operations, all of which run in linear
27 > * time.
28   *
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
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
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 65 | Line 68 | public class ArrayDeque<E> extends Abstr
68       * other.  We also guarantee that all array cells not holding
69       * deque elements are always null.
70       */
71 <    private transient Object[] elements;
71 >    transient Object[] elements; // non-private to simplify nested class access
72  
73      /**
74       * The index of the element at the head of the deque (which is the
75       * element that would be removed by remove() or pop()); or an
76       * arbitrary number equal to tail if the deque is empty.
77       */
78 <    private transient int head;
78 >    transient int head;
79  
80      /**
81       * The index at which the next element would be added to the tail
82       * of the deque (via addLast(E), add(E), or push(E)).
83       */
84 <    private transient int tail;
84 >    transient int tail;
85  
86      /**
87       * The minimum capacity that we'll use for a newly created deque.
# Line 843 | Line 846 | public class ArrayDeque<E> extends Abstr
846          for (int i = 0; i < size; i++)
847              elements[i] = s.readObject();
848      }
849 +
850 +    public Stream<E> stream() {
851 +        int flags = Streams.STREAM_IS_ORDERED | Streams.STREAM_IS_SIZED;
852 +        return Streams.stream
853 +            (() -> new DeqSpliterator<E>(this, head, tail), flags);
854 +    }
855 +    public Stream<E> parallelStream() {
856 +        int flags = Streams.STREAM_IS_ORDERED | Streams.STREAM_IS_SIZED;
857 +        return Streams.parallelStream
858 +            (() -> new DeqSpliterator<E>(this, head, tail), flags);
859 +    }
860 +
861 +
862 +    static final class DeqSpliterator<E> implements Spliterator<E> {
863 +        private final ArrayDeque<E> deq;
864 +        private final int fence;  // initially tail
865 +        private int index;        // current index, modified on traverse/split
866 +
867 +        /** Create new spliterator covering the given array and range */
868 +        DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) {
869 +            this.deq = deq; this.index = origin; this.fence = fence;
870 +        }
871 +
872 +        public DeqSpliterator<E> trySplit() {
873 +            int n = deq.elements.length;
874 +            int h = index, t = fence;
875 +            if (h != t && ((h + 1) & (n - 1)) != t) {
876 +                if (h > t)
877 +                    t += n;
878 +                int m = ((h + t) >>> 1) & (n - 1);
879 +                return new DeqSpliterator<E>(deq, h, index = m);
880 +            }
881 +            return null;
882 +        }
883 +
884 +        public void forEach(Consumer<? super E> block) {
885 +            if (block == null)
886 +                throw new NullPointerException();
887 +            Object[] a = deq.elements;
888 +            int m = a.length - 1, f = fence, i = index;
889 +            index = f;
890 +            while (i != f) {
891 +                @SuppressWarnings("unchecked") E e = (E)a[i];
892 +                i = (i + 1) & m;
893 +                if (e == null)
894 +                    throw new ConcurrentModificationException();
895 +                block.accept(e);
896 +            }
897 +        }
898 +
899 +        public boolean tryAdvance(Consumer<? super E> block) {
900 +            if (block == null)
901 +                throw new NullPointerException();
902 +            Object[] a = deq.elements;
903 +            int m = a.length - 1, i = index;
904 +            if (i != fence) {
905 +                @SuppressWarnings("unchecked") E e = (E)a[i];
906 +                index = (i + 1) & m;
907 +                if (e == null)
908 +                    throw new ConcurrentModificationException();
909 +                block.accept(e);
910 +                return true;
911 +            }
912 +            return false;
913 +        }
914 +
915 +        // Other spliterator methods
916 +        public long estimateSize() {
917 +            int n = fence - index;
918 +            if (n < 0)
919 +                n += deq.elements.length;
920 +            return (long)n;
921 +        }
922 +        public boolean hasExactSize() { return true; }
923 +        public boolean hasExactSplits() { return true; }
924 +    }
925 +
926   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines