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.51 by jsr166, Wed Feb 20 12:39:06 2013 UTC vs.
Revision 1.68 by jsr166, Thu Sep 17 14:27:56 2015 UTC

# Line 4 | Line 4
4   */
5  
6   package java.util;
7 +
8   import java.io.Serializable;
9   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 54 | Line 53 | import java.util.stream.Streams;
53   *
54   * @author  Josh Bloch and Doug Lea
55   * @since   1.6
56 < * @param <E> the type of elements held in this collection
56 > * @param <E> the type of elements held in this deque
57   */
58   public class ArrayDeque<E> extends AbstractCollection<E>
59                             implements Deque<E>, Cloneable, Serializable
# Line 248 | Line 247 | public class ArrayDeque<E> extends Abstr
247      }
248  
249      public E pollFirst() {
250 <        int h = head;
250 >        final Object[] elements = this.elements;
251 >        final int h = head;
252          @SuppressWarnings("unchecked")
253          E result = (E) elements[h];
254          // Element is null if deque empty
255 <        if (result == null)
256 <            return null;
257 <        elements[h] = null;     // Must null out slot
258 <        head = (h + 1) & (elements.length - 1);
255 >        if (result != null) {
256 >            elements[h] = null; // Must null out slot
257 >            head = (h + 1) & (elements.length - 1);
258 >        }
259          return result;
260      }
261  
262      public E pollLast() {
263 <        int t = (tail - 1) & (elements.length - 1);
263 >        final Object[] elements = this.elements;
264 >        final int t = (tail - 1) & (elements.length - 1);
265          @SuppressWarnings("unchecked")
266          E result = (E) elements[t];
267 <        if (result == null)
268 <            return null;
269 <        elements[t] = null;
270 <        tail = t;
267 >        if (result != null) {
268 >            elements[t] = null;
269 >            tail = t;
270 >        }
271          return result;
272      }
273  
# Line 316 | Line 317 | public class ArrayDeque<E> extends Abstr
317       * @return {@code true} if the deque contained the specified element
318       */
319      public boolean removeFirstOccurrence(Object o) {
320 <        if (o == null)
321 <            return false;
322 <        int mask = elements.length - 1;
323 <        int i = head;
324 <        Object x;
325 <        while ( (x = elements[i]) != null) {
326 <            if (o.equals(x)) {
327 <                delete(i);
327 <                return true;
320 >        if (o != null) {
321 >            int mask = elements.length - 1;
322 >            int i = head;
323 >            for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) {
324 >                if (o.equals(x)) {
325 >                    delete(i);
326 >                    return true;
327 >                }
328              }
329            i = (i + 1) & mask;
329          }
330          return false;
331      }
# Line 344 | Line 343 | public class ArrayDeque<E> extends Abstr
343       * @return {@code true} if the deque contained the specified element
344       */
345      public boolean removeLastOccurrence(Object o) {
346 <        if (o == null)
347 <            return false;
348 <        int mask = elements.length - 1;
349 <        int i = (tail - 1) & mask;
350 <        Object x;
351 <        while ( (x = elements[i]) != null) {
352 <            if (o.equals(x)) {
353 <                delete(i);
355 <                return true;
346 >        if (o != null) {
347 >            int mask = elements.length - 1;
348 >            int i = (tail - 1) & mask;
349 >            for (Object x; (x = elements[i]) != null; i = (i - 1) & mask) {
350 >                if (o.equals(x)) {
351 >                    delete(i);
352 >                    return true;
353 >                }
354              }
357            i = (i - 1) & mask;
355          }
356          return false;
357      }
# Line 611 | Line 608 | public class ArrayDeque<E> extends Abstr
608              }
609              lastRet = -1;
610          }
611 +
612 +        public void forEachRemaining(Consumer<? super E> action) {
613 +            Objects.requireNonNull(action);
614 +            Object[] a = elements;
615 +            int m = a.length - 1, f = fence, i = cursor;
616 +            cursor = f;
617 +            while (i != f) {
618 +                @SuppressWarnings("unchecked") E e = (E)a[i];
619 +                i = (i + 1) & m;
620 +                if (e == null)
621 +                    throw new ConcurrentModificationException();
622 +                action.accept(e);
623 +            }
624 +        }
625      }
626  
627 +    /**
628 +     * This class is nearly a mirror-image of DeqIterator, using tail
629 +     * instead of head for initial cursor, and head instead of tail
630 +     * for fence.
631 +     */
632      private class DescendingIterator implements Iterator<E> {
617        /*
618         * This class is nearly a mirror-image of DeqIterator, using
619         * tail instead of head for initial cursor, and head instead of
620         * tail for fence.
621         */
633          private int cursor = tail;
634          private int fence = head;
635          private int lastRet = -1;
# Line 659 | Line 670 | public class ArrayDeque<E> extends Abstr
670       * @return {@code true} if this deque contains the specified element
671       */
672      public boolean contains(Object o) {
673 <        if (o == null)
674 <            return false;
675 <        int mask = elements.length - 1;
676 <        int i = head;
677 <        Object x;
678 <        while ( (x = elements[i]) != null) {
679 <            if (o.equals(x))
669 <                return true;
670 <            i = (i + 1) & mask;
673 >        if (o != null) {
674 >            int mask = elements.length - 1;
675 >            int i = head;
676 >            for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) {
677 >                if (o.equals(x))
678 >                    return true;
679 >            }
680          }
681          return false;
682      }
# Line 753 | Line 762 | public class ArrayDeque<E> extends Abstr
762       * The following code can be used to dump the deque into a newly
763       * allocated array of {@code String}:
764       *
765 <     *  <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
765 >     * <pre> {@code String[] y = x.toArray(new String[0]);}</pre>
766       *
767       * Note that {@code toArray(new Object[0])} is identical in function to
768       * {@code toArray()}.
# Line 811 | Line 820 | public class ArrayDeque<E> extends Abstr
820      /**
821       * Saves this deque to a stream (that is, serializes it).
822       *
823 +     * @param s the stream
824 +     * @throws java.io.IOException if an I/O error occurs
825       * @serialData The current size ({@code int}) of the deque,
826       * followed by all of its elements (each an object reference) in
827       * first-to-last order.
# Line 830 | Line 841 | public class ArrayDeque<E> extends Abstr
841  
842      /**
843       * Reconstitutes this deque from a stream (that is, deserializes it).
844 +     * @param s the stream
845 +     * @throws ClassNotFoundException if the class of a serialized object
846 +     *         could not be found
847 +     * @throws java.io.IOException if an I/O error occurs
848       */
849      private void readObject(java.io.ObjectInputStream s)
850              throws java.io.IOException, ClassNotFoundException {
# Line 846 | Line 861 | public class ArrayDeque<E> extends Abstr
861              elements[i] = s.readObject();
862      }
863  
864 <    Spliterator<E> spliterator() {
865 <        return new DeqSpliterator<E>(this, -1, -1);
851 <    }
852 <
853 <    public Stream<E> stream() {
854 <        return Streams.stream(spliterator());
855 <    }
856 <
857 <    public Stream<E> parallelStream() {
858 <        return Streams.parallelStream(spliterator());
864 >    public Spliterator<E> spliterator() {
865 >        return new DeqSpliterator<>(this, -1, -1);
866      }
867  
868      static final class DeqSpliterator<E> implements Spliterator<E> {
# Line 879 | Line 886 | public class ArrayDeque<E> extends Abstr
886              return t;
887          }
888  
889 <        public DeqSpliterator<E> trySplit() {
889 >        public Spliterator<E> trySplit() {
890              int t = getFence(), h = index, n = deq.elements.length;
891              if (h != t && ((h + 1) & (n - 1)) != t) {
892                  if (h > t)
# Line 890 | Line 897 | public class ArrayDeque<E> extends Abstr
897              return null;
898          }
899  
900 <        public void forEach(Consumer<? super E> consumer) {
900 >        public void forEachRemaining(Consumer<? super E> consumer) {
901              if (consumer == null)
902                  throw new NullPointerException();
903              Object[] a = deq.elements;
# Line 910 | Line 917 | public class ArrayDeque<E> extends Abstr
917                  throw new NullPointerException();
918              Object[] a = deq.elements;
919              int m = a.length - 1, f = getFence(), i = index;
920 <            if (i != fence) {
920 >            if (i != f) {
921                  @SuppressWarnings("unchecked") E e = (E)a[i];
922                  index = (i + 1) & m;
923                  if (e == null)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines