--- jsr166/src/main/java/util/ArrayDeque.java 2013/02/20 12:32:01 1.50
+++ jsr166/src/main/java/util/ArrayDeque.java 2015/09/17 14:27:56 1.68
@@ -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
@@ -246,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;
}
@@ -314,17 +317,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 +343,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;
}
@@ -609,14 +608,28 @@ public class ArrayDeque extends Abstr
}
lastRet = -1;
}
+
+ public void forEachRemaining(Consumer super E> 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);
+ }
+ }
}
+ /**
+ * 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 +670,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 +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()}.
@@ -809,6 +820,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 +841,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,16 +861,8 @@ public class ArrayDeque extends Abstr
elements[i] = s.readObject();
}
- Spliterator spliterator() {
- return new DeqSpliterator(this, -1, -1);
- }
-
- public Stream stream() {
- return Streams.stream(spliterator());
- }
-
- public Stream parallelStream() {
- return Streams.parallelStream(spliterator());
+ public Spliterator spliterator() {
+ return new DeqSpliterator<>(this, -1, -1);
}
static final class DeqSpliterator implements Spliterator {
@@ -877,7 +886,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 +897,7 @@ public class ArrayDeque extends Abstr
return null;
}
- public void forEach(Consumer super E> consumer) {
+ public void forEachRemaining(Consumer super E> consumer) {
if (consumer == null)
throw new NullPointerException();
Object[] a = deq.elements;
@@ -908,7 +917,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)