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 |
– |
import java.util.stream.Streams; |
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 |
315 |
|
* @return {@code true} if the deque contained the specified element |
316 |
|
*/ |
317 |
|
public boolean removeFirstOccurrence(Object o) { |
318 |
< |
if (o == null) |
319 |
< |
return false; |
320 |
< |
int mask = elements.length - 1; |
321 |
< |
int i = head; |
322 |
< |
Object x; |
323 |
< |
while ( (x = elements[i]) != null) { |
324 |
< |
if (o.equals(x)) { |
325 |
< |
delete(i); |
327 |
< |
return true; |
318 |
> |
if (o != null) { |
319 |
> |
int mask = elements.length - 1; |
320 |
> |
int i = head; |
321 |
> |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
322 |
> |
if (o.equals(x)) { |
323 |
> |
delete(i); |
324 |
> |
return true; |
325 |
> |
} |
326 |
|
} |
329 |
– |
i = (i + 1) & mask; |
327 |
|
} |
328 |
|
return false; |
329 |
|
} |
341 |
|
* @return {@code true} if the deque contained the specified element |
342 |
|
*/ |
343 |
|
public boolean removeLastOccurrence(Object o) { |
344 |
< |
if (o == null) |
345 |
< |
return false; |
346 |
< |
int mask = elements.length - 1; |
347 |
< |
int i = (tail - 1) & mask; |
348 |
< |
Object x; |
349 |
< |
while ( (x = elements[i]) != null) { |
350 |
< |
if (o.equals(x)) { |
351 |
< |
delete(i); |
355 |
< |
return true; |
344 |
> |
if (o != null) { |
345 |
> |
int mask = elements.length - 1; |
346 |
> |
int i = (tail - 1) & mask; |
347 |
> |
for (Object x; (x = elements[i]) != null; i = (i - 1) & mask) { |
348 |
> |
if (o.equals(x)) { |
349 |
> |
delete(i); |
350 |
> |
return true; |
351 |
> |
} |
352 |
|
} |
357 |
– |
i = (i - 1) & mask; |
353 |
|
} |
354 |
|
return false; |
355 |
|
} |
654 |
|
* @return {@code true} if this deque contains the specified element |
655 |
|
*/ |
656 |
|
public boolean contains(Object o) { |
657 |
< |
if (o == null) |
658 |
< |
return false; |
659 |
< |
int mask = elements.length - 1; |
660 |
< |
int i = head; |
661 |
< |
Object x; |
662 |
< |
while ( (x = elements[i]) != null) { |
663 |
< |
if (o.equals(x)) |
669 |
< |
return true; |
670 |
< |
i = (i + 1) & mask; |
657 |
> |
if (o != null) { |
658 |
> |
int mask = elements.length - 1; |
659 |
> |
int i = head; |
660 |
> |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
661 |
> |
if (o.equals(x)) |
662 |
> |
return true; |
663 |
> |
} |
664 |
|
} |
665 |
|
return false; |
666 |
|
} |
746 |
|
* The following code can be used to dump the deque into a newly |
747 |
|
* allocated array of {@code String}: |
748 |
|
* |
749 |
< |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
749 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
750 |
|
* |
751 |
|
* Note that {@code toArray(new Object[0])} is identical in function to |
752 |
|
* {@code toArray()}. |
804 |
|
/** |
805 |
|
* Saves this deque to a stream (that is, serializes it). |
806 |
|
* |
807 |
+ |
* @param s the stream |
808 |
+ |
* @throws java.io.IOException if an I/O error occurs |
809 |
|
* @serialData The current size ({@code int}) of the deque, |
810 |
|
* followed by all of its elements (each an object reference) in |
811 |
|
* first-to-last order. |
825 |
|
|
826 |
|
/** |
827 |
|
* Reconstitutes this deque from a stream (that is, deserializes it). |
828 |
+ |
* @param s the stream |
829 |
+ |
* @throws ClassNotFoundException if the class of a serialized object |
830 |
+ |
* could not be found |
831 |
+ |
* @throws java.io.IOException if an I/O error occurs |
832 |
|
*/ |
833 |
|
private void readObject(java.io.ObjectInputStream s) |
834 |
|
throws java.io.IOException, ClassNotFoundException { |
845 |
|
elements[i] = s.readObject(); |
846 |
|
} |
847 |
|
|
848 |
< |
Spliterator<E> spliterator() { |
848 |
> |
public Spliterator<E> spliterator() { |
849 |
|
return new DeqSpliterator<E>(this, -1, -1); |
850 |
|
} |
851 |
|
|
853 |
– |
public Stream<E> stream() { |
854 |
– |
return Streams.stream(spliterator()); |
855 |
– |
} |
856 |
– |
|
857 |
– |
public Stream<E> parallelStream() { |
858 |
– |
return Streams.parallelStream(spliterator()); |
859 |
– |
} |
860 |
– |
|
852 |
|
static final class DeqSpliterator<E> implements Spliterator<E> { |
853 |
|
private final ArrayDeque<E> deq; |
854 |
|
private int fence; // -1 until first use |
870 |
|
return t; |
871 |
|
} |
872 |
|
|
873 |
< |
public DeqSpliterator<E> trySplit() { |
873 |
> |
public Spliterator<E> trySplit() { |
874 |
|
int t = getFence(), h = index, n = deq.elements.length; |
875 |
|
if (h != t && ((h + 1) & (n - 1)) != t) { |
876 |
|
if (h > t) |
881 |
|
return null; |
882 |
|
} |
883 |
|
|
884 |
< |
public void forEach(Consumer<? super E> consumer) { |
884 |
> |
public void forEachRemaining(Consumer<? super E> consumer) { |
885 |
|
if (consumer == null) |
886 |
|
throw new NullPointerException(); |
887 |
|
Object[] a = deq.elements; |
901 |
|
throw new NullPointerException(); |
902 |
|
Object[] a = deq.elements; |
903 |
|
int m = a.length - 1, f = getFence(), i = index; |
904 |
< |
if (i != fence) { |
904 |
> |
if (i != f) { |
905 |
|
@SuppressWarnings("unchecked") E e = (E)a[i]; |
906 |
|
index = (i + 1) & m; |
907 |
|
if (e == null) |