--- jsr166/src/main/java/util/ArrayDeque.java 2013/02/20 12:32:01 1.50 +++ jsr166/src/main/java/util/ArrayDeque.java 2014/12/31 07:54:13 1.61 @@ -4,10 +4,10 @@ */ 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 +19,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 +54,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 @@ -314,17 +316,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 +342,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 +609,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 +655,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; } @@ -809,6 +805,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 +826,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 +846,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 +871,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 +882,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;