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; |
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 |
|
|
914 |
– |
@SuppressWarnings("unchecked") |
884 |
|
public void forEach(Block<? 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 |
|
|
932 |
– |
@SuppressWarnings("unchecked") |
899 |
|
public boolean tryAdvance(Block<? 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() { |
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; } |