--- jsr166/src/main/java/util/ArrayDeque.java 2013/05/02 06:02:17 1.55 +++ jsr166/src/main/java/util/ArrayDeque.java 2015/02/28 22:16:45 1.66 @@ -4,9 +4,9 @@ */ 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 @@ -53,7 +53,7 @@ import java.util.stream.Stream; * * @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 @@ -247,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; } @@ -315,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; } @@ -343,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; } @@ -658,15 +656,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; } @@ -752,7 +748,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()}. @@ -810,6 +806,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. @@ -829,6 +827,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 { @@ -901,7 +903,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)