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.64 by dl, Mon Feb 23 19:54:04 2015 UTC vs.
Revision 1.74 by jsr166, Wed Aug 24 21:46:18 2016 UTC

# Line 52 | Line 52 | import java.util.function.Consumer;
52   * Java Collections Framework</a>.
53   *
54   * @author  Josh Bloch and Doug Lea
55 * @since   1.6
55   * @param <E> the type of elements held in this deque
56 + * @since   1.6
57   */
58   public class ArrayDeque<E> extends AbstractCollection<E>
59                             implements Deque<E>, Cloneable, Serializable
# Line 109 | Line 109 | public class ArrayDeque<E> extends Abstr
109              initialCapacity |= (initialCapacity >>> 16);
110              initialCapacity++;
111  
112 <            if (initialCapacity < 0)   // Too many elements, must back off
113 <                initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
112 >            if (initialCapacity < 0)    // Too many elements, must back off
113 >                initialCapacity >>>= 1; // Good luck allocating 2^30 elements
114          }
115          elements = new Object[initialCapacity];
116      }
# Line 247 | 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 486 | Line 488 | public class ArrayDeque<E> extends Abstr
488       *
489       * @return true if elements moved backwards
490       */
491 <    private boolean delete(int i) {
491 >    boolean delete(int i) {
492          checkInvariants();
493          final Object[] elements = this.elements;
494          final int mask = elements.length - 1;
# Line 606 | 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      /**
# Line 845 | Line 861 | public class ArrayDeque<E> extends Abstr
861              elements[i] = s.readObject();
862      }
863  
864 +    /**
865 +     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
866 +     * and <em>fail-fast</em> {@link Spliterator} over the elements in this
867 +     * deque.
868 +     *
869 +     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
870 +     * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
871 +     * {@link Spliterator#NONNULL}.  Overriding implementations should document
872 +     * the reporting of additional characteristic values.
873 +     *
874 +     * @return a {@code Spliterator} over the elements in this deque
875 +     * @since 1.8
876 +     */
877      public Spliterator<E> spliterator() {
878 <        return new DeqSpliterator<E>(this, -1, -1);
878 >        return new DeqSpliterator<>(this, -1, -1);
879      }
880  
881      static final class DeqSpliterator<E> implements Spliterator<E> {
# Line 854 | Line 883 | public class ArrayDeque<E> extends Abstr
883          private int fence;  // -1 until first use
884          private int index;  // current index, modified on traverse/split
885  
886 <        /** Creates new spliterator covering the given array and range */
886 >        /** Creates new spliterator covering the given array and range. */
887          DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) {
888              this.deq = deq;
889              this.index = origin;
# Line 870 | Line 899 | public class ArrayDeque<E> extends Abstr
899              return t;
900          }
901  
902 <        public Spliterator<E> trySplit() {
902 >        public DeqSpliterator<E> trySplit() {
903              int t = getFence(), h = index, n = deq.elements.length;
904              if (h != t && ((h + 1) & (n - 1)) != t) {
905                  if (h > t)
906                      t += n;
907                  int m = ((h + t) >>> 1) & (n - 1);
908 <                return new DeqSpliterator<>(deq, h, index = m);
908 >                return new DeqSpliterator<E>(deq, h, index = m);
909              }
910              return null;
911          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines