--- jsr166/src/main/java/util/ArrayDeque.java 2013/02/20 12:32:01 1.50 +++ jsr166/src/main/java/util/ArrayDeque.java 2015/02/28 20:35:47 1.65 @@ -4,10 +4,9 @@ */ package java.util; + import java.io.Serializable; import java.util.function.Consumer; -import java.util.stream.Stream; -import java.util.stream.Streams; /** * Resizable-array implementation of the {@link Deque} interface. Array @@ -19,16 +18,18 @@ import java.util.stream.Streams; * when used as a queue. * *

Most {@code ArrayDeque} operations run in amortized constant time. - * Exceptions include {@link #remove(Object) remove}, {@link - * #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence - * removeLastOccurrence}, {@link #contains contains}, {@link #iterator - * iterator.remove()}, and the bulk operations, all of which run in linear - * time. + * Exceptions include + * {@link #remove(Object) remove}, + * {@link #removeFirstOccurrence removeFirstOccurrence}, + * {@link #removeLastOccurrence removeLastOccurrence}, + * {@link #contains contains}, + * {@link #iterator iterator.remove()}, + * and the bulk operations, all of which run in linear time. * - *

The iterators returned by this class's {@code iterator} method are - * fail-fast: If the deque is modified at any time after the iterator - * is created, in any way except through the iterator's own {@code remove} - * method, the iterator will generally throw a {@link + *

The iterators returned by this class's {@link #iterator() iterator} + * method are fail-fast: If the deque is modified at any time after + * the iterator is created, in any way except through the iterator's own + * {@code remove} method, the iterator will generally throw a {@link * ConcurrentModificationException}. Thus, in the face of concurrent * modification, the iterator fails quickly and cleanly, rather than risking * arbitrary, non-deterministic behavior at an undetermined time in the @@ -52,7 +53,7 @@ import java.util.stream.Streams; * * @author Josh Bloch and Doug Lea * @since 1.6 - * @param the type of elements held in this collection + * @param the type of elements held in this deque */ public class ArrayDeque extends AbstractCollection implements Deque, Cloneable, Serializable @@ -250,10 +251,10 @@ public class ArrayDeque extends Abstr @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; } @@ -261,10 +262,10 @@ public class ArrayDeque extends Abstr 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; } @@ -314,17 +315,15 @@ public class ArrayDeque extends Abstr * @return {@code true} if the deque contained the specified element */ public boolean removeFirstOccurrence(Object o) { - if (o == null) - return false; - int mask = elements.length - 1; - int i = head; - Object x; - while ( (x = elements[i]) != null) { - if (o.equals(x)) { - delete(i); - return true; + if (o != null) { + int mask = elements.length - 1; + int i = head; + for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { + if (o.equals(x)) { + delete(i); + return true; + } } - i = (i + 1) & mask; } return false; } @@ -342,17 +341,15 @@ public class ArrayDeque extends Abstr * @return {@code true} if the deque contained the specified element */ public boolean removeLastOccurrence(Object o) { - if (o == null) - return false; - int mask = elements.length - 1; - int i = (tail - 1) & mask; - Object x; - while ( (x = elements[i]) != null) { - if (o.equals(x)) { - delete(i); - return true; + if (o != null) { + int mask = elements.length - 1; + int i = (tail - 1) & mask; + for (Object x; (x = elements[i]) != null; i = (i - 1) & mask) { + if (o.equals(x)) { + delete(i); + return true; + } } - i = (i - 1) & mask; } return false; } @@ -611,12 +608,12 @@ public class ArrayDeque extends Abstr } } + /** + * This class is nearly a mirror-image of DeqIterator, using tail + * instead of head for initial cursor, and head instead of tail + * for fence. + */ private class DescendingIterator implements Iterator { - /* - * This class is nearly a mirror-image of DeqIterator, using - * tail instead of head for initial cursor, and head instead of - * tail for fence. - */ private int cursor = tail; private int fence = head; private int lastRet = -1; @@ -657,15 +654,13 @@ public class ArrayDeque extends Abstr * @return {@code true} if this deque contains the specified element */ public boolean contains(Object o) { - if (o == null) - return false; - int mask = elements.length - 1; - int i = head; - Object x; - while ( (x = elements[i]) != null) { - if (o.equals(x)) - return true; - i = (i + 1) & mask; + if (o != null) { + int mask = elements.length - 1; + int i = head; + for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { + if (o.equals(x)) + return true; + } } return false; } @@ -751,7 +746,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()}. @@ -809,6 +804,8 @@ public class ArrayDeque extends Abstr /** * Saves this deque to a stream (that is, serializes it). * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs * @serialData The current size ({@code int}) of the deque, * followed by all of its elements (each an object reference) in * first-to-last order. @@ -828,6 +825,10 @@ public class ArrayDeque extends Abstr /** * Reconstitutes this deque from a stream (that is, deserializes it). + * @param s the stream + * @throws ClassNotFoundException if the class of a serialized object + * could not be found + * @throws java.io.IOException if an I/O error occurs */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { @@ -844,18 +845,10 @@ public class ArrayDeque extends Abstr elements[i] = s.readObject(); } - Spliterator spliterator() { + public Spliterator spliterator() { return new DeqSpliterator(this, -1, -1); } - public Stream stream() { - return Streams.stream(spliterator()); - } - - public Stream parallelStream() { - return Streams.parallelStream(spliterator()); - } - static final class DeqSpliterator implements Spliterator { private final ArrayDeque deq; private int fence; // -1 until first use @@ -877,7 +870,7 @@ public class ArrayDeque extends Abstr return t; } - public DeqSpliterator trySplit() { + public Spliterator trySplit() { int t = getFence(), h = index, n = deq.elements.length; if (h != t && ((h + 1) & (n - 1)) != t) { if (h > t) @@ -888,7 +881,7 @@ public class ArrayDeque extends Abstr return null; } - public void forEach(Consumer consumer) { + public void forEachRemaining(Consumer consumer) { if (consumer == null) throw new NullPointerException(); Object[] a = deq.elements; @@ -908,7 +901,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)