--- jsr166/src/main/java/util/ArrayDeque.java 2015/02/28 20:35:47 1.65 +++ jsr166/src/main/java/util/ArrayDeque.java 2015/10/11 00:50:06 1.73 @@ -109,8 +109,8 @@ public class ArrayDeque extends Abstr initialCapacity |= (initialCapacity >>> 16); initialCapacity++; - if (initialCapacity < 0) // Too many elements, must back off - initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements + if (initialCapacity < 0) // Too many elements, must back off + initialCapacity >>>= 1; // Good luck allocating 2^30 elements } elements = new Object[initialCapacity]; } @@ -247,7 +247,8 @@ public class ArrayDeque extends Abstr } public E pollFirst() { - int h = head; + final Object[] elements = this.elements; + final int h = head; @SuppressWarnings("unchecked") E result = (E) elements[h]; // Element is null if deque empty @@ -259,7 +260,8 @@ public class ArrayDeque extends Abstr } public E pollLast() { - int t = (tail - 1) & (elements.length - 1); + final Object[] elements = this.elements; + final int t = (tail - 1) & (elements.length - 1); @SuppressWarnings("unchecked") E result = (E) elements[t]; if (result != null) { @@ -486,7 +488,7 @@ public class ArrayDeque extends Abstr * * @return true if elements moved backwards */ - private boolean delete(int i) { + boolean delete(int i) { checkInvariants(); final Object[] elements = this.elements; final int mask = elements.length - 1; @@ -606,6 +608,20 @@ public class ArrayDeque extends Abstr } lastRet = -1; } + + public void forEachRemaining(Consumer action) { + Objects.requireNonNull(action); + Object[] a = elements; + int m = a.length - 1, f = fence, i = cursor; + cursor = f; + while (i != f) { + @SuppressWarnings("unchecked") E e = (E)a[i]; + i = (i + 1) & m; + if (e == null) + throw new ConcurrentModificationException(); + action.accept(e); + } + } } /** @@ -845,8 +861,21 @@ public class ArrayDeque extends Abstr elements[i] = s.readObject(); } + /** + * Creates a late-binding + * and fail-fast {@link Spliterator} over the elements in this + * deque. + * + *

The {@code Spliterator} reports {@link Spliterator#SIZED}, + * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and + * {@link Spliterator#NONNULL}. Overriding implementations should document + * the reporting of additional characteristic values. + * + * @return a {@code Spliterator} over the elements in this deque + * @since 1.8 + */ public Spliterator spliterator() { - return new DeqSpliterator(this, -1, -1); + return new DeqSpliterator<>(this, -1, -1); } static final class DeqSpliterator implements Spliterator { @@ -854,7 +883,7 @@ public class ArrayDeque extends Abstr private int fence; // -1 until first use private int index; // current index, modified on traverse/split - /** Creates new spliterator covering the given array and range */ + /** Creates new spliterator covering the given array and range. */ DeqSpliterator(ArrayDeque deq, int origin, int fence) { this.deq = deq; this.index = origin; @@ -870,13 +899,13 @@ public class ArrayDeque extends Abstr return t; } - public Spliterator trySplit() { + public DeqSpliterator trySplit() { int t = getFence(), h = index, n = deq.elements.length; if (h != t && ((h + 1) & (n - 1)) != t) { if (h > t) t += n; int m = ((h + t) >>> 1) & (n - 1); - return new DeqSpliterator<>(deq, h, index = m); + return new DeqSpliterator(deq, h, index = m); } return null; }