1 |
|
/* |
2 |
< |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 |
< |
* |
4 |
< |
* This code is free software; you can redistribute it and/or modify it |
5 |
< |
* under the terms of the GNU General Public License version 2 only, as |
6 |
< |
* published by the Free Software Foundation. Oracle designates this |
7 |
< |
* particular file as subject to the "Classpath" exception as provided |
8 |
< |
* by Oracle in the LICENSE file that accompanied this code. |
9 |
< |
* |
10 |
< |
* This code is distributed in the hope that it will be useful, but WITHOUT |
11 |
< |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
12 |
< |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
13 |
< |
* version 2 for more details (a copy is included in the LICENSE file that |
14 |
< |
* accompanied this code). |
15 |
< |
* |
16 |
< |
* You should have received a copy of the GNU General Public License version |
17 |
< |
* 2 along with this work; if not, write to the Free Software Foundation, |
18 |
< |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
< |
* |
20 |
< |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
< |
* or visit www.oracle.com if you need additional information or have any |
22 |
< |
* questions. |
23 |
< |
*/ |
24 |
< |
|
25 |
< |
/* |
26 |
< |
* This file is available under and governed by the GNU General Public |
27 |
< |
* License version 2 only, as published by the Free Software Foundation. |
28 |
< |
* However, the following notice accompanied the original version of this |
29 |
< |
* file: |
30 |
< |
* |
31 |
< |
* Written by Josh Bloch of Google Inc. and released to the public domain, |
32 |
< |
* as explained at http://creativecommons.org/publicdomain/zero/1.0/. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package java.util; |
8 |
|
import java.util.Spliterator; |
9 |
|
import java.util.stream.Stream; |
10 |
|
import java.util.stream.Streams; |
11 |
< |
import java.util.function.Block; |
11 |
> |
import java.util.function.Consumer; |
12 |
|
|
13 |
|
/** |
14 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
19 |
|
* {@link Stack} when used as a stack, and faster than {@link LinkedList} |
20 |
|
* when used as a queue. |
21 |
|
* |
22 |
< |
* <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time. |
22 |
> |
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
23 |
|
* Exceptions include {@link #remove(Object) remove}, {@link |
24 |
|
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
25 |
|
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
26 |
|
* iterator.remove()}, and the bulk operations, all of which run in linear |
27 |
|
* time. |
28 |
|
* |
29 |
< |
* <p>The iterators returned by this class's <tt>iterator</tt> method are |
29 |
> |
* <p>The iterators returned by this class's {@code iterator} method are |
30 |
|
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
31 |
< |
* is created, in any way except through the iterator's own <tt>remove</tt> |
31 |
> |
* is created, in any way except through the iterator's own {@code remove} |
32 |
|
* method, the iterator will generally throw a {@link |
33 |
|
* ConcurrentModificationException}. Thus, in the face of concurrent |
34 |
|
* modification, the iterator fails quickly and cleanly, rather than risking |
38 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
39 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
40 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
41 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
41 |
> |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
42 |
|
* Therefore, it would be wrong to write a program that depended on this |
43 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
44 |
|
* should be used only to detect bugs.</i> |
859 |
|
} |
860 |
|
|
861 |
|
|
862 |
< |
static final class DeqSpliterator<E> implements Spliterator<E>, Iterator<E> { |
862 |
> |
static final class DeqSpliterator<E> implements Spliterator<E> { |
863 |
|
private final ArrayDeque<E> deq; |
892 |
– |
private final Object[] array; |
864 |
|
private final int fence; // initially tail |
865 |
|
private int index; // current index, modified on traverse/split |
866 |
|
|
867 |
|
/** Create new spliterator covering the given array and range */ |
868 |
|
DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) { |
869 |
< |
this.deq = deq; this.array = deq.elements; |
899 |
< |
this.index = origin; this.fence = fence; |
869 |
> |
this.deq = deq; this.index = origin; this.fence = fence; |
870 |
|
} |
871 |
|
|
872 |
|
public DeqSpliterator<E> trySplit() { |
873 |
< |
int n = array.length; |
873 |
> |
int n = deq.elements.length; |
874 |
|
int h = index, t = fence; |
875 |
|
if (h != t && ((h + 1) & (n - 1)) != t) { |
876 |
|
if (h > t) |
881 |
|
return null; |
882 |
|
} |
883 |
|
|
884 |
< |
@SuppressWarnings("unchecked") |
915 |
< |
public void forEach(Block<? super E> block) { |
884 |
> |
public void forEach(Consumer<? super E> block) { |
885 |
|
if (block == null) |
886 |
|
throw new NullPointerException(); |
887 |
< |
Object[] a = array; |
919 |
< |
if (a != deq.elements) |
920 |
< |
throw new ConcurrentModificationException(); |
887 |
> |
Object[] a = deq.elements; |
888 |
|
int m = a.length - 1, f = fence, i = index; |
889 |
|
index = f; |
890 |
|
while (i != f) { |
891 |
< |
Object e = a[i]; |
891 |
> |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
892 |
> |
i = (i + 1) & m; |
893 |
|
if (e == null) |
894 |
|
throw new ConcurrentModificationException(); |
895 |
< |
block.accept((E)e); |
928 |
< |
i = (i + 1) & m; |
895 |
> |
block.accept(e); |
896 |
|
} |
897 |
|
} |
898 |
|
|
899 |
< |
@SuppressWarnings("unchecked") |
933 |
< |
public boolean tryAdvance(Block<? super E> block) { |
899 |
> |
public boolean tryAdvance(Consumer<? super E> block) { |
900 |
|
if (block == null) |
901 |
|
throw new NullPointerException(); |
902 |
< |
Object[] a = array; |
937 |
< |
if (a != deq.elements) |
938 |
< |
throw new ConcurrentModificationException(); |
902 |
> |
Object[] a = deq.elements; |
903 |
|
int m = a.length - 1, i = index; |
904 |
|
if (i != fence) { |
905 |
< |
Object e = a[i]; |
905 |
> |
@SuppressWarnings("unchecked") E e = (E)a[i]; |
906 |
> |
index = (i + 1) & m; |
907 |
|
if (e == null) |
908 |
|
throw new ConcurrentModificationException(); |
909 |
< |
block.accept((E)e); |
945 |
< |
index = (i + 1) & m; |
909 |
> |
block.accept(e); |
910 |
|
return true; |
911 |
|
} |
912 |
|
return false; |
913 |
|
} |
914 |
|
|
951 |
– |
// Iterator support |
952 |
– |
public Iterator<E> iterator() { |
953 |
– |
return this; |
954 |
– |
} |
955 |
– |
|
956 |
– |
public boolean hasNext() { |
957 |
– |
return index >= 0 && index != fence; |
958 |
– |
} |
959 |
– |
|
960 |
– |
@SuppressWarnings("unchecked") |
961 |
– |
public E next() { |
962 |
– |
if (index < 0 || index == fence) |
963 |
– |
throw new NoSuchElementException(); |
964 |
– |
Object[] a = array; |
965 |
– |
if (a != deq.elements) |
966 |
– |
throw new ConcurrentModificationException(); |
967 |
– |
Object e = a[index]; |
968 |
– |
if (e == null) |
969 |
– |
throw new ConcurrentModificationException(); |
970 |
– |
index = (index + 1) & (a.length - 1); |
971 |
– |
return (E) e; |
972 |
– |
} |
973 |
– |
|
974 |
– |
public void remove() { throw new UnsupportedOperationException(); } |
975 |
– |
|
915 |
|
// Other spliterator methods |
916 |
< |
public long estimateSize() { |
916 |
> |
public long estimateSize() { |
917 |
|
int n = fence - index; |
918 |
|
if (n < 0) |
919 |
< |
n += array.length; |
919 |
> |
n += deq.elements.length; |
920 |
|
return (long)n; |
921 |
|
} |
922 |
|
public boolean hasExactSize() { return true; } |