4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
+ |
|
8 |
|
import java.io.Serializable; |
9 |
|
import java.util.function.Consumer; |
9 |
– |
import java.util.stream.Stream; |
10 |
|
|
11 |
|
/** |
12 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
53 |
|
* |
54 |
|
* @author Josh Bloch and Doug Lea |
55 |
|
* @since 1.6 |
56 |
< |
* @param <E> the type of elements held in this collection |
56 |
> |
* @param <E> the type of elements held in this deque |
57 |
|
*/ |
58 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
59 |
|
implements Deque<E>, Cloneable, Serializable |
247 |
|
} |
248 |
|
|
249 |
|
public E pollFirst() { |
250 |
< |
int h = head; |
250 |
> |
final Object[] elements = this.elements; |
251 |
> |
final int h = head; |
252 |
|
@SuppressWarnings("unchecked") |
253 |
|
E result = (E) elements[h]; |
254 |
|
// Element is null if deque empty |
255 |
< |
if (result == null) |
256 |
< |
return null; |
257 |
< |
elements[h] = null; // Must null out slot |
258 |
< |
head = (h + 1) & (elements.length - 1); |
255 |
> |
if (result != null) { |
256 |
> |
elements[h] = null; // Must null out slot |
257 |
> |
head = (h + 1) & (elements.length - 1); |
258 |
> |
} |
259 |
|
return result; |
260 |
|
} |
261 |
|
|
262 |
|
public E pollLast() { |
263 |
< |
int t = (tail - 1) & (elements.length - 1); |
263 |
> |
final Object[] elements = this.elements; |
264 |
> |
final int t = (tail - 1) & (elements.length - 1); |
265 |
|
@SuppressWarnings("unchecked") |
266 |
|
E result = (E) elements[t]; |
267 |
< |
if (result == null) |
268 |
< |
return null; |
269 |
< |
elements[t] = null; |
270 |
< |
tail = t; |
267 |
> |
if (result != null) { |
268 |
> |
elements[t] = null; |
269 |
> |
tail = t; |
270 |
> |
} |
271 |
|
return result; |
272 |
|
} |
273 |
|
|
317 |
|
* @return {@code true} if the deque contained the specified element |
318 |
|
*/ |
319 |
|
public boolean removeFirstOccurrence(Object o) { |
320 |
< |
if (o == null) |
321 |
< |
return false; |
322 |
< |
int mask = elements.length - 1; |
323 |
< |
int i = head; |
324 |
< |
Object x; |
325 |
< |
while ( (x = elements[i]) != null) { |
326 |
< |
if (o.equals(x)) { |
327 |
< |
delete(i); |
326 |
< |
return true; |
320 |
> |
if (o != null) { |
321 |
> |
int mask = elements.length - 1; |
322 |
> |
int i = head; |
323 |
> |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
324 |
> |
if (o.equals(x)) { |
325 |
> |
delete(i); |
326 |
> |
return true; |
327 |
> |
} |
328 |
|
} |
328 |
– |
i = (i + 1) & mask; |
329 |
|
} |
330 |
|
return false; |
331 |
|
} |
343 |
|
* @return {@code true} if the deque contained the specified element |
344 |
|
*/ |
345 |
|
public boolean removeLastOccurrence(Object o) { |
346 |
< |
if (o == null) |
347 |
< |
return false; |
348 |
< |
int mask = elements.length - 1; |
349 |
< |
int i = (tail - 1) & mask; |
350 |
< |
Object x; |
351 |
< |
while ( (x = elements[i]) != null) { |
352 |
< |
if (o.equals(x)) { |
353 |
< |
delete(i); |
354 |
< |
return true; |
346 |
> |
if (o != null) { |
347 |
> |
int mask = elements.length - 1; |
348 |
> |
int i = (tail - 1) & mask; |
349 |
> |
for (Object x; (x = elements[i]) != null; i = (i - 1) & mask) { |
350 |
> |
if (o.equals(x)) { |
351 |
> |
delete(i); |
352 |
> |
return true; |
353 |
> |
} |
354 |
|
} |
356 |
– |
i = (i - 1) & mask; |
355 |
|
} |
356 |
|
return false; |
357 |
|
} |
656 |
|
* @return {@code true} if this deque contains the specified element |
657 |
|
*/ |
658 |
|
public boolean contains(Object o) { |
659 |
< |
if (o == null) |
660 |
< |
return false; |
661 |
< |
int mask = elements.length - 1; |
662 |
< |
int i = head; |
663 |
< |
Object x; |
664 |
< |
while ( (x = elements[i]) != null) { |
665 |
< |
if (o.equals(x)) |
668 |
< |
return true; |
669 |
< |
i = (i + 1) & mask; |
659 |
> |
if (o != null) { |
660 |
> |
int mask = elements.length - 1; |
661 |
> |
int i = head; |
662 |
> |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
663 |
> |
if (o.equals(x)) |
664 |
> |
return true; |
665 |
> |
} |
666 |
|
} |
667 |
|
return false; |
668 |
|
} |
748 |
|
* The following code can be used to dump the deque into a newly |
749 |
|
* allocated array of {@code String}: |
750 |
|
* |
751 |
< |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
751 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
752 |
|
* |
753 |
|
* Note that {@code toArray(new Object[0])} is identical in function to |
754 |
|
* {@code toArray()}. |
806 |
|
/** |
807 |
|
* Saves this deque to a stream (that is, serializes it). |
808 |
|
* |
809 |
+ |
* @param s the stream |
810 |
+ |
* @throws java.io.IOException if an I/O error occurs |
811 |
|
* @serialData The current size ({@code int}) of the deque, |
812 |
|
* followed by all of its elements (each an object reference) in |
813 |
|
* first-to-last order. |
827 |
|
|
828 |
|
/** |
829 |
|
* Reconstitutes this deque from a stream (that is, deserializes it). |
830 |
+ |
* @param s the stream |
831 |
+ |
* @throws ClassNotFoundException if the class of a serialized object |
832 |
+ |
* could not be found |
833 |
+ |
* @throws java.io.IOException if an I/O error occurs |
834 |
|
*/ |
835 |
|
private void readObject(java.io.ObjectInputStream s) |
836 |
|
throws java.io.IOException, ClassNotFoundException { |
903 |
|
throw new NullPointerException(); |
904 |
|
Object[] a = deq.elements; |
905 |
|
int m = a.length - 1, f = getFence(), i = index; |
906 |
< |
if (i != fence) { |
906 |
> |
if (i != f) { |
907 |
|
@SuppressWarnings("unchecked") E e = (E)a[i]; |
908 |
|
index = (i + 1) & m; |
909 |
|
if (e == null) |