--- jsr166/src/main/java/util/ArrayDeque.java 2014/12/31 07:54:13 1.61 +++ jsr166/src/main/java/util/ArrayDeque.java 2015/10/11 00:50:06 1.73 @@ -7,7 +7,6 @@ package java.util; import java.io.Serializable; import java.util.function.Consumer; -import java.util.stream.Stream; /** * Resizable-array implementation of the {@link Deque} interface. Array @@ -110,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]; } @@ -248,25 +247,27 @@ 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 - if (result == null) - return null; - elements[h] = null; // Must null out slot - head = (h + 1) & (elements.length - 1); + if (result != null) { + elements[h] = null; // Must null out slot + head = (h + 1) & (elements.length - 1); + } return result; } 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) - return null; - elements[t] = null; - tail = t; + if (result != null) { + elements[t] = null; + tail = t; + } return result; } @@ -487,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; @@ -607,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); + } + } } /** @@ -747,7 +762,7 @@ public class ArrayDeque extends Abstr * The following code can be used to dump the deque into a newly * allocated array of {@code String}: * - *
 {@code String[] y = x.toArray(new String[0]);}
+ *
 {@code String[] y = x.toArray(new String[0]);}
* * Note that {@code toArray(new Object[0])} is identical in function to * {@code toArray()}. @@ -846,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 { @@ -855,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; @@ -871,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; } @@ -902,7 +930,7 @@ public class ArrayDeque extends Abstr throw new NullPointerException(); Object[] a = deq.elements; int m = a.length - 1, f = getFence(), i = index; - if (i != fence) { + if (i != f) { @SuppressWarnings("unchecked") E e = (E)a[i]; index = (i + 1) & m; if (e == null)