author | mchung |
Tue, 06 Nov 2018 10:01:16 -0800 | |
changeset 52427 | 3c6aa484536c |
parent 50764 | 5637aca18f1d |
child 54683 | 3ffdc15cd044 |
permissions | -rw-r--r-- |
2 | 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 |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
27 |
* Expert Group. Adapted and released, under explicit permission, |
|
28 |
* from JDK ArrayList.java which carries the following copyright: |
|
29 |
* |
|
30 |
* Copyright 1997 by Sun Microsystems, Inc., |
|
31 |
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. |
|
32 |
* All rights reserved. |
|
33 |
*/ |
|
34 |
||
35 |
package java.util.concurrent; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
36 |
|
50764
5637aca18f1d
8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents:
49564
diff
changeset
|
37 |
import java.lang.invoke.VarHandle; |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
38 |
import java.lang.reflect.Field; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
39 |
import java.util.Arrays; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
40 |
import java.util.Collection; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
41 |
import java.util.Comparator; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
42 |
import java.util.ConcurrentModificationException; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
43 |
import java.util.Iterator; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
44 |
import java.util.List; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
45 |
import java.util.ListIterator; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
46 |
import java.util.NoSuchElementException; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
47 |
import java.util.Objects; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
48 |
import java.util.RandomAccess; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
49 |
import java.util.Spliterator; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
50 |
import java.util.Spliterators; |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
51 |
import java.util.function.Consumer; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
52 |
import java.util.function.Predicate; |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
53 |
import java.util.function.UnaryOperator; |
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
50764
diff
changeset
|
54 |
import jdk.internal.access.SharedSecrets; |
2 | 55 |
|
56 |
/** |
|
57 |
* A thread-safe variant of {@link java.util.ArrayList} in which all mutative |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
58 |
* operations ({@code add}, {@code set}, and so on) are implemented by |
2 | 59 |
* making a fresh copy of the underlying array. |
60 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
61 |
* <p>This is ordinarily too costly, but may be <em>more</em> efficient |
2 | 62 |
* than alternatives when traversal operations vastly outnumber |
63 |
* mutations, and is useful when you cannot or don't want to |
|
64 |
* synchronize traversals, yet need to preclude interference among |
|
65 |
* concurrent threads. The "snapshot" style iterator method uses a |
|
66 |
* reference to the state of the array at the point that the iterator |
|
67 |
* was created. This array never changes during the lifetime of the |
|
68 |
* iterator, so interference is impossible and the iterator is |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
69 |
* guaranteed not to throw {@code ConcurrentModificationException}. |
2 | 70 |
* The iterator will not reflect additions, removals, or changes to |
71 |
* the list since the iterator was created. Element-changing |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
72 |
* operations on iterators themselves ({@code remove}, {@code set}, and |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
73 |
* {@code add}) are not supported. These methods throw |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
74 |
* {@code UnsupportedOperationException}. |
2 | 75 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
76 |
* <p>All elements are permitted, including {@code null}. |
2 | 77 |
* |
78 |
* <p>Memory consistency effects: As with other concurrent |
|
79 |
* collections, actions in a thread prior to placing an object into a |
|
80 |
* {@code CopyOnWriteArrayList} |
|
81 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
|
82 |
* actions subsequent to the access or removal of that element from |
|
83 |
* the {@code CopyOnWriteArrayList} in another thread. |
|
84 |
* |
|
85 |
* <p>This class is a member of the |
|
49433
b6671a111395
8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents:
48541
diff
changeset
|
86 |
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> |
2 | 87 |
* Java Collections Framework</a>. |
88 |
* |
|
89 |
* @since 1.5 |
|
90 |
* @author Doug Lea |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
91 |
* @param <E> the type of elements held in this list |
2 | 92 |
*/ |
93 |
public class CopyOnWriteArrayList<E> |
|
94 |
implements List<E>, RandomAccess, Cloneable, java.io.Serializable { |
|
95 |
private static final long serialVersionUID = 8673264195747942595L; |
|
96 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
97 |
/** |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
98 |
* The lock protecting all mutators. (We have a mild preference |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
99 |
* for builtin monitors over ReentrantLock when either will do.) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
100 |
*/ |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
101 |
final transient Object lock = new Object(); |
2 | 102 |
|
103 |
/** The array, accessed only via getArray/setArray. */ |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
104 |
private transient volatile Object[] array; |
2 | 105 |
|
106 |
/** |
|
107 |
* Gets the array. Non-private so as to also be accessible |
|
108 |
* from CopyOnWriteArraySet class. |
|
109 |
*/ |
|
110 |
final Object[] getArray() { |
|
111 |
return array; |
|
112 |
} |
|
113 |
||
114 |
/** |
|
115 |
* Sets the array. |
|
116 |
*/ |
|
117 |
final void setArray(Object[] a) { |
|
118 |
array = a; |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Creates an empty list. |
|
123 |
*/ |
|
124 |
public CopyOnWriteArrayList() { |
|
125 |
setArray(new Object[0]); |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Creates a list containing the elements of the specified |
|
130 |
* collection, in the order they are returned by the collection's |
|
131 |
* iterator. |
|
132 |
* |
|
133 |
* @param c the collection of initially held elements |
|
134 |
* @throws NullPointerException if the specified collection is null |
|
135 |
*/ |
|
136 |
public CopyOnWriteArrayList(Collection<? extends E> c) { |
|
49564 | 137 |
Object[] es; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
138 |
if (c.getClass() == CopyOnWriteArrayList.class) |
49564 | 139 |
es = ((CopyOnWriteArrayList<?>)c).getArray(); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
140 |
else { |
49564 | 141 |
es = c.toArray(); |
31540
6efd719b3330
6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents:
25859
diff
changeset
|
142 |
// defend against c.toArray (incorrectly) not returning Object[] |
6efd719b3330
6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents:
25859
diff
changeset
|
143 |
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652) |
49564 | 144 |
if (es.getClass() != Object[].class) |
145 |
es = Arrays.copyOf(es, es.length, Object[].class); |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
146 |
} |
49564 | 147 |
setArray(es); |
2 | 148 |
} |
149 |
||
150 |
/** |
|
151 |
* Creates a list holding a copy of the given array. |
|
152 |
* |
|
153 |
* @param toCopyIn the array (a copy of this array is used as the |
|
154 |
* internal array) |
|
155 |
* @throws NullPointerException if the specified array is null |
|
156 |
*/ |
|
157 |
public CopyOnWriteArrayList(E[] toCopyIn) { |
|
158 |
setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class)); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Returns the number of elements in this list. |
|
163 |
* |
|
164 |
* @return the number of elements in this list |
|
165 |
*/ |
|
166 |
public int size() { |
|
167 |
return getArray().length; |
|
168 |
} |
|
169 |
||
170 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
171 |
* Returns {@code true} if this list contains no elements. |
2 | 172 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
173 |
* @return {@code true} if this list contains no elements |
2 | 174 |
*/ |
175 |
public boolean isEmpty() { |
|
176 |
return size() == 0; |
|
177 |
} |
|
178 |
||
179 |
/** |
|
180 |
* static version of indexOf, to allow repeated calls without |
|
181 |
* needing to re-acquire array each time. |
|
182 |
* @param o element to search for |
|
49564 | 183 |
* @param es the array |
184 |
* @param from first index to search |
|
185 |
* @param to one past last index to search |
|
2 | 186 |
* @return index of element, or -1 if absent |
187 |
*/ |
|
49564 | 188 |
private static int indexOfRange(Object o, Object[] es, int from, int to) { |
2 | 189 |
if (o == null) { |
49564 | 190 |
for (int i = from; i < to; i++) |
191 |
if (es[i] == null) |
|
2 | 192 |
return i; |
193 |
} else { |
|
49564 | 194 |
for (int i = from; i < to; i++) |
195 |
if (o.equals(es[i])) |
|
2 | 196 |
return i; |
197 |
} |
|
198 |
return -1; |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* static version of lastIndexOf. |
|
203 |
* @param o element to search for |
|
49564 | 204 |
* @param es the array |
205 |
* @param from index of first element of range, last element to search |
|
206 |
* @param to one past last element of range, first element to search |
|
2 | 207 |
* @return index of element, or -1 if absent |
208 |
*/ |
|
49564 | 209 |
private static int lastIndexOfRange(Object o, Object[] es, int from, int to) { |
2 | 210 |
if (o == null) { |
49564 | 211 |
for (int i = to - 1; i >= from; i--) |
212 |
if (es[i] == null) |
|
2 | 213 |
return i; |
214 |
} else { |
|
49564 | 215 |
for (int i = to - 1; i >= from; i--) |
216 |
if (o.equals(es[i])) |
|
2 | 217 |
return i; |
218 |
} |
|
219 |
return -1; |
|
220 |
} |
|
221 |
||
222 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
223 |
* Returns {@code true} if this list contains the specified element. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
224 |
* More formally, returns {@code true} if and only if this list contains |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
225 |
* at least one element {@code e} such that {@code Objects.equals(o, e)}. |
2 | 226 |
* |
227 |
* @param o element whose presence in this list is to be tested |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
228 |
* @return {@code true} if this list contains the specified element |
2 | 229 |
*/ |
230 |
public boolean contains(Object o) { |
|
49564 | 231 |
return indexOf(o) >= 0; |
2 | 232 |
} |
233 |
||
234 |
/** |
|
235 |
* {@inheritDoc} |
|
236 |
*/ |
|
237 |
public int indexOf(Object o) { |
|
49564 | 238 |
Object[] es = getArray(); |
239 |
return indexOfRange(o, es, 0, es.length); |
|
2 | 240 |
} |
241 |
||
242 |
/** |
|
243 |
* Returns the index of the first occurrence of the specified element in |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
244 |
* this list, searching forwards from {@code index}, or returns -1 if |
2 | 245 |
* the element is not found. |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
246 |
* More formally, returns the lowest index {@code i} such that |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
247 |
* {@code i >= index && Objects.equals(get(i), e)}, |
2 | 248 |
* or -1 if there is no such index. |
249 |
* |
|
250 |
* @param e element to search for |
|
251 |
* @param index index to start searching from |
|
252 |
* @return the index of the first occurrence of the element in |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
253 |
* this list at position {@code index} or later in the list; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
254 |
* {@code -1} if the element is not found. |
2 | 255 |
* @throws IndexOutOfBoundsException if the specified index is negative |
256 |
*/ |
|
257 |
public int indexOf(E e, int index) { |
|
49564 | 258 |
Object[] es = getArray(); |
259 |
return indexOfRange(e, es, index, es.length); |
|
2 | 260 |
} |
261 |
||
262 |
/** |
|
263 |
* {@inheritDoc} |
|
264 |
*/ |
|
265 |
public int lastIndexOf(Object o) { |
|
49564 | 266 |
Object[] es = getArray(); |
267 |
return lastIndexOfRange(o, es, 0, es.length); |
|
2 | 268 |
} |
269 |
||
270 |
/** |
|
271 |
* Returns the index of the last occurrence of the specified element in |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
272 |
* this list, searching backwards from {@code index}, or returns -1 if |
2 | 273 |
* the element is not found. |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
274 |
* More formally, returns the highest index {@code i} such that |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
275 |
* {@code i <= index && Objects.equals(get(i), e)}, |
2 | 276 |
* or -1 if there is no such index. |
277 |
* |
|
278 |
* @param e element to search for |
|
279 |
* @param index index to start searching backwards from |
|
280 |
* @return the index of the last occurrence of the element at position |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
281 |
* less than or equal to {@code index} in this list; |
2 | 282 |
* -1 if the element is not found. |
283 |
* @throws IndexOutOfBoundsException if the specified index is greater |
|
284 |
* than or equal to the current size of this list |
|
285 |
*/ |
|
286 |
public int lastIndexOf(E e, int index) { |
|
49564 | 287 |
Object[] es = getArray(); |
288 |
return lastIndexOfRange(e, es, 0, index + 1); |
|
2 | 289 |
} |
290 |
||
291 |
/** |
|
292 |
* Returns a shallow copy of this list. (The elements themselves |
|
293 |
* are not copied.) |
|
294 |
* |
|
295 |
* @return a clone of this list |
|
296 |
*/ |
|
297 |
public Object clone() { |
|
298 |
try { |
|
11279 | 299 |
@SuppressWarnings("unchecked") |
300 |
CopyOnWriteArrayList<E> clone = |
|
301 |
(CopyOnWriteArrayList<E>) super.clone(); |
|
302 |
clone.resetLock(); |
|
50764
5637aca18f1d
8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents:
49564
diff
changeset
|
303 |
// Unlike in readObject, here we cannot visibility-piggyback on the |
5637aca18f1d
8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents:
49564
diff
changeset
|
304 |
// volatile write in setArray(). |
5637aca18f1d
8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents:
49564
diff
changeset
|
305 |
VarHandle.releaseFence(); |
11279 | 306 |
return clone; |
2 | 307 |
} catch (CloneNotSupportedException e) { |
308 |
// this shouldn't happen, since we are Cloneable |
|
309 |
throw new InternalError(); |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Returns an array containing all of the elements in this list |
|
315 |
* in proper sequence (from first to last element). |
|
316 |
* |
|
317 |
* <p>The returned array will be "safe" in that no references to it are |
|
318 |
* maintained by this list. (In other words, this method must allocate |
|
319 |
* a new array). The caller is thus free to modify the returned array. |
|
320 |
* |
|
321 |
* <p>This method acts as bridge between array-based and collection-based |
|
322 |
* APIs. |
|
323 |
* |
|
324 |
* @return an array containing all the elements in this list |
|
325 |
*/ |
|
326 |
public Object[] toArray() { |
|
49564 | 327 |
return getArray().clone(); |
2 | 328 |
} |
329 |
||
330 |
/** |
|
331 |
* Returns an array containing all of the elements in this list in |
|
332 |
* proper sequence (from first to last element); the runtime type of |
|
333 |
* the returned array is that of the specified array. If the list fits |
|
334 |
* in the specified array, it is returned therein. Otherwise, a new |
|
335 |
* array is allocated with the runtime type of the specified array and |
|
336 |
* the size of this list. |
|
337 |
* |
|
338 |
* <p>If this list fits in the specified array with room to spare |
|
339 |
* (i.e., the array has more elements than this list), the element in |
|
340 |
* the array immediately following the end of the list is set to |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
341 |
* {@code null}. (This is useful in determining the length of this |
2 | 342 |
* list <i>only</i> if the caller knows that this list does not contain |
343 |
* any null elements.) |
|
344 |
* |
|
345 |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
|
346 |
* array-based and collection-based APIs. Further, this method allows |
|
347 |
* precise control over the runtime type of the output array, and may, |
|
348 |
* under certain circumstances, be used to save allocation costs. |
|
349 |
* |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
350 |
* <p>Suppose {@code x} is a list known to contain only strings. |
2 | 351 |
* The following code can be used to dump the list into a newly |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
352 |
* allocated array of {@code String}: |
2 | 353 |
* |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
354 |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
2 | 355 |
* |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
356 |
* Note that {@code toArray(new Object[0])} is identical in function to |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
357 |
* {@code toArray()}. |
2 | 358 |
* |
359 |
* @param a the array into which the elements of the list are to |
|
360 |
* be stored, if it is big enough; otherwise, a new array of the |
|
361 |
* same runtime type is allocated for this purpose. |
|
362 |
* @return an array containing all the elements in this list |
|
363 |
* @throws ArrayStoreException if the runtime type of the specified array |
|
364 |
* is not a supertype of the runtime type of every element in |
|
365 |
* this list |
|
366 |
* @throws NullPointerException if the specified array is null |
|
367 |
*/ |
|
368 |
@SuppressWarnings("unchecked") |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
369 |
public <T> T[] toArray(T[] a) { |
49564 | 370 |
Object[] es = getArray(); |
371 |
int len = es.length; |
|
2 | 372 |
if (a.length < len) |
49564 | 373 |
return (T[]) Arrays.copyOf(es, len, a.getClass()); |
2 | 374 |
else { |
49564 | 375 |
System.arraycopy(es, 0, a, 0, len); |
2 | 376 |
if (a.length > len) |
377 |
a[len] = null; |
|
378 |
return a; |
|
379 |
} |
|
380 |
} |
|
381 |
||
382 |
// Positional Access Operations |
|
383 |
||
384 |
@SuppressWarnings("unchecked") |
|
42319 | 385 |
static <E> E elementAt(Object[] a, int index) { |
2 | 386 |
return (E) a[index]; |
387 |
} |
|
388 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
389 |
static String outOfBounds(int index, int size) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
390 |
return "Index: " + index + ", Size: " + size; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
391 |
} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
392 |
|
2 | 393 |
/** |
394 |
* {@inheritDoc} |
|
395 |
* |
|
396 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
397 |
*/ |
|
398 |
public E get(int index) { |
|
42319 | 399 |
return elementAt(getArray(), index); |
2 | 400 |
} |
401 |
||
402 |
/** |
|
403 |
* Replaces the element at the specified position in this list with the |
|
404 |
* specified element. |
|
405 |
* |
|
406 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
407 |
*/ |
|
408 |
public E set(int index, E element) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
409 |
synchronized (lock) { |
49564 | 410 |
Object[] es = getArray(); |
411 |
E oldValue = elementAt(es, index); |
|
2 | 412 |
|
413 |
if (oldValue != element) { |
|
49564 | 414 |
es = es.clone(); |
415 |
es[index] = element; |
|
416 |
setArray(es); |
|
2 | 417 |
} |
418 |
return oldValue; |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
/** |
|
423 |
* Appends the specified element to the end of this list. |
|
424 |
* |
|
425 |
* @param e element to be appended to this list |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
426 |
* @return {@code true} (as specified by {@link Collection#add}) |
2 | 427 |
*/ |
428 |
public boolean add(E e) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
429 |
synchronized (lock) { |
49564 | 430 |
Object[] es = getArray(); |
431 |
int len = es.length; |
|
432 |
es = Arrays.copyOf(es, len + 1); |
|
433 |
es[len] = e; |
|
434 |
setArray(es); |
|
2 | 435 |
return true; |
436 |
} |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Inserts the specified element at the specified position in this |
|
441 |
* list. Shifts the element currently at that position (if any) and |
|
442 |
* any subsequent elements to the right (adds one to their indices). |
|
443 |
* |
|
444 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
445 |
*/ |
|
446 |
public void add(int index, E element) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
447 |
synchronized (lock) { |
49564 | 448 |
Object[] es = getArray(); |
449 |
int len = es.length; |
|
2 | 450 |
if (index > len || index < 0) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
451 |
throw new IndexOutOfBoundsException(outOfBounds(index, len)); |
2 | 452 |
Object[] newElements; |
453 |
int numMoved = len - index; |
|
454 |
if (numMoved == 0) |
|
49564 | 455 |
newElements = Arrays.copyOf(es, len + 1); |
2 | 456 |
else { |
457 |
newElements = new Object[len + 1]; |
|
49564 | 458 |
System.arraycopy(es, 0, newElements, 0, index); |
459 |
System.arraycopy(es, index, newElements, index + 1, |
|
2 | 460 |
numMoved); |
461 |
} |
|
462 |
newElements[index] = element; |
|
463 |
setArray(newElements); |
|
464 |
} |
|
465 |
} |
|
466 |
||
467 |
/** |
|
468 |
* Removes the element at the specified position in this list. |
|
469 |
* Shifts any subsequent elements to the left (subtracts one from their |
|
470 |
* indices). Returns the element that was removed from the list. |
|
471 |
* |
|
472 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
473 |
*/ |
|
474 |
public E remove(int index) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
475 |
synchronized (lock) { |
49564 | 476 |
Object[] es = getArray(); |
477 |
int len = es.length; |
|
478 |
E oldValue = elementAt(es, index); |
|
2 | 479 |
int numMoved = len - index - 1; |
49564 | 480 |
Object[] newElements; |
2 | 481 |
if (numMoved == 0) |
49564 | 482 |
newElements = Arrays.copyOf(es, len - 1); |
2 | 483 |
else { |
49564 | 484 |
newElements = new Object[len - 1]; |
485 |
System.arraycopy(es, 0, newElements, 0, index); |
|
486 |
System.arraycopy(es, index + 1, newElements, index, |
|
2 | 487 |
numMoved); |
488 |
} |
|
49564 | 489 |
setArray(newElements); |
2 | 490 |
return oldValue; |
491 |
} |
|
492 |
} |
|
493 |
||
494 |
/** |
|
495 |
* Removes the first occurrence of the specified element from this list, |
|
496 |
* if it is present. If this list does not contain the element, it is |
|
497 |
* unchanged. More formally, removes the element with the lowest index |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
498 |
* {@code i} such that {@code Objects.equals(o, get(i))} |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
499 |
* (if such an element exists). Returns {@code true} if this list |
2 | 500 |
* contained the specified element (or equivalently, if this list |
501 |
* changed as a result of the call). |
|
502 |
* |
|
503 |
* @param o element to be removed from this list, if present |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
504 |
* @return {@code true} if this list contained the specified element |
2 | 505 |
*/ |
506 |
public boolean remove(Object o) { |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
507 |
Object[] snapshot = getArray(); |
49564 | 508 |
int index = indexOfRange(o, snapshot, 0, snapshot.length); |
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47423
diff
changeset
|
509 |
return index >= 0 && remove(o, snapshot, index); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
510 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
511 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
512 |
/** |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
513 |
* A version of remove(Object) using the strong hint that given |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
514 |
* recent snapshot contains o at the given index. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
515 |
*/ |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
516 |
private boolean remove(Object o, Object[] snapshot, int index) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
517 |
synchronized (lock) { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
518 |
Object[] current = getArray(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
519 |
int len = current.length; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
520 |
if (snapshot != current) findIndex: { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
521 |
int prefix = Math.min(index, len); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
522 |
for (int i = 0; i < prefix; i++) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
523 |
if (current[i] != snapshot[i] |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
524 |
&& Objects.equals(o, current[i])) { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
525 |
index = i; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
526 |
break findIndex; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
527 |
} |
2 | 528 |
} |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
529 |
if (index >= len) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
530 |
return false; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
531 |
if (current[index] == o) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
532 |
break findIndex; |
49564 | 533 |
index = indexOfRange(o, current, index, len); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
534 |
if (index < 0) |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
535 |
return false; |
2 | 536 |
} |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
537 |
Object[] newElements = new Object[len - 1]; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
538 |
System.arraycopy(current, 0, newElements, 0, index); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
539 |
System.arraycopy(current, index + 1, |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
540 |
newElements, index, |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
541 |
len - index - 1); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
542 |
setArray(newElements); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
543 |
return true; |
2 | 544 |
} |
545 |
} |
|
546 |
||
547 |
/** |
|
548 |
* Removes from this list all of the elements whose index is between |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
549 |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
2 | 550 |
* Shifts any succeeding elements to the left (reduces their index). |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
551 |
* This call shortens the list by {@code (toIndex - fromIndex)} elements. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
552 |
* (If {@code toIndex==fromIndex}, this operation has no effect.) |
2 | 553 |
* |
554 |
* @param fromIndex index of first element to be removed |
|
555 |
* @param toIndex index after last element to be removed |
|
556 |
* @throws IndexOutOfBoundsException if fromIndex or toIndex out of range |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
557 |
* ({@code fromIndex < 0 || toIndex > size() || toIndex < fromIndex}) |
2 | 558 |
*/ |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
559 |
void removeRange(int fromIndex, int toIndex) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
560 |
synchronized (lock) { |
49564 | 561 |
Object[] es = getArray(); |
562 |
int len = es.length; |
|
2 | 563 |
|
564 |
if (fromIndex < 0 || toIndex > len || toIndex < fromIndex) |
|
565 |
throw new IndexOutOfBoundsException(); |
|
566 |
int newlen = len - (toIndex - fromIndex); |
|
567 |
int numMoved = len - toIndex; |
|
568 |
if (numMoved == 0) |
|
49564 | 569 |
setArray(Arrays.copyOf(es, newlen)); |
2 | 570 |
else { |
571 |
Object[] newElements = new Object[newlen]; |
|
49564 | 572 |
System.arraycopy(es, 0, newElements, 0, fromIndex); |
573 |
System.arraycopy(es, toIndex, newElements, |
|
2 | 574 |
fromIndex, numMoved); |
575 |
setArray(newElements); |
|
576 |
} |
|
577 |
} |
|
578 |
} |
|
579 |
||
580 |
/** |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
581 |
* Appends the element, if not present. |
2 | 582 |
* |
583 |
* @param e element to be added to this list, if absent |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
584 |
* @return {@code true} if the element was added |
2 | 585 |
*/ |
586 |
public boolean addIfAbsent(E e) { |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
587 |
Object[] snapshot = getArray(); |
49564 | 588 |
return indexOfRange(e, snapshot, 0, snapshot.length) < 0 |
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47423
diff
changeset
|
589 |
&& addIfAbsent(e, snapshot); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
590 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
591 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
592 |
/** |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
593 |
* A version of addIfAbsent using the strong hint that given |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
594 |
* recent snapshot does not contain e. |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
595 |
*/ |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
596 |
private boolean addIfAbsent(E e, Object[] snapshot) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
597 |
synchronized (lock) { |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
598 |
Object[] current = getArray(); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
599 |
int len = current.length; |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
600 |
if (snapshot != current) { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
601 |
// Optimize for lost race to another addXXX operation |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
602 |
int common = Math.min(snapshot.length, len); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
603 |
for (int i = 0; i < common; i++) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
604 |
if (current[i] != snapshot[i] |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
605 |
&& Objects.equals(e, current[i])) |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
606 |
return false; |
49564 | 607 |
if (indexOfRange(e, current, common, len) >= 0) |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
608 |
return false; |
2 | 609 |
} |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
610 |
Object[] newElements = Arrays.copyOf(current, len + 1); |
2 | 611 |
newElements[len] = e; |
612 |
setArray(newElements); |
|
613 |
return true; |
|
614 |
} |
|
615 |
} |
|
616 |
||
617 |
/** |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
618 |
* Returns {@code true} if this list contains all of the elements of the |
2 | 619 |
* specified collection. |
620 |
* |
|
621 |
* @param c collection to be checked for containment in this list |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
622 |
* @return {@code true} if this list contains all of the elements of the |
2 | 623 |
* specified collection |
624 |
* @throws NullPointerException if the specified collection is null |
|
625 |
* @see #contains(Object) |
|
626 |
*/ |
|
627 |
public boolean containsAll(Collection<?> c) { |
|
49564 | 628 |
Object[] es = getArray(); |
629 |
int len = es.length; |
|
2 | 630 |
for (Object e : c) { |
49564 | 631 |
if (indexOfRange(e, es, 0, len) < 0) |
2 | 632 |
return false; |
633 |
} |
|
634 |
return true; |
|
635 |
} |
|
636 |
||
637 |
/** |
|
638 |
* Removes from this list all of its elements that are contained in |
|
639 |
* the specified collection. This is a particularly expensive operation |
|
640 |
* in this class because of the need for an internal temporary array. |
|
641 |
* |
|
642 |
* @param c collection containing elements to be removed from this list |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
643 |
* @return {@code true} if this list changed as a result of the call |
2 | 644 |
* @throws ClassCastException if the class of an element of this list |
9504 | 645 |
* is incompatible with the specified collection |
49433
b6671a111395
8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents:
48541
diff
changeset
|
646 |
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>) |
2 | 647 |
* @throws NullPointerException if this list contains a null element and the |
9504 | 648 |
* specified collection does not permit null elements |
49433
b6671a111395
8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents:
48541
diff
changeset
|
649 |
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>), |
2 | 650 |
* or if the specified collection is null |
651 |
* @see #remove(Object) |
|
652 |
*/ |
|
653 |
public boolean removeAll(Collection<?> c) { |
|
42319 | 654 |
Objects.requireNonNull(c); |
655 |
return bulkRemove(e -> c.contains(e)); |
|
2 | 656 |
} |
657 |
||
658 |
/** |
|
659 |
* Retains only the elements in this list that are contained in the |
|
660 |
* specified collection. In other words, removes from this list all of |
|
661 |
* its elements that are not contained in the specified collection. |
|
662 |
* |
|
663 |
* @param c collection containing elements to be retained in this list |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
664 |
* @return {@code true} if this list changed as a result of the call |
2 | 665 |
* @throws ClassCastException if the class of an element of this list |
9504 | 666 |
* is incompatible with the specified collection |
49433
b6671a111395
8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents:
48541
diff
changeset
|
667 |
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>) |
2 | 668 |
* @throws NullPointerException if this list contains a null element and the |
9504 | 669 |
* specified collection does not permit null elements |
49433
b6671a111395
8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents:
48541
diff
changeset
|
670 |
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>), |
2 | 671 |
* or if the specified collection is null |
672 |
* @see #remove(Object) |
|
673 |
*/ |
|
674 |
public boolean retainAll(Collection<?> c) { |
|
42319 | 675 |
Objects.requireNonNull(c); |
676 |
return bulkRemove(e -> !c.contains(e)); |
|
2 | 677 |
} |
678 |
||
679 |
/** |
|
680 |
* Appends all of the elements in the specified collection that |
|
681 |
* are not already contained in this list, to the end of |
|
682 |
* this list, in the order that they are returned by the |
|
683 |
* specified collection's iterator. |
|
684 |
* |
|
685 |
* @param c collection containing elements to be added to this list |
|
686 |
* @return the number of elements added |
|
687 |
* @throws NullPointerException if the specified collection is null |
|
688 |
* @see #addIfAbsent(Object) |
|
689 |
*/ |
|
690 |
public int addAllAbsent(Collection<? extends E> c) { |
|
691 |
Object[] cs = c.toArray(); |
|
692 |
if (cs.length == 0) |
|
693 |
return 0; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
694 |
synchronized (lock) { |
49564 | 695 |
Object[] es = getArray(); |
696 |
int len = es.length; |
|
2 | 697 |
int added = 0; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
698 |
// uniquify and compact elements in cs |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
699 |
for (int i = 0; i < cs.length; ++i) { |
2 | 700 |
Object e = cs[i]; |
49564 | 701 |
if (indexOfRange(e, es, 0, len) < 0 && |
702 |
indexOfRange(e, cs, 0, added) < 0) |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
703 |
cs[added++] = e; |
2 | 704 |
} |
705 |
if (added > 0) { |
|
49564 | 706 |
Object[] newElements = Arrays.copyOf(es, len + added); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
707 |
System.arraycopy(cs, 0, newElements, len, added); |
2 | 708 |
setArray(newElements); |
709 |
} |
|
710 |
return added; |
|
711 |
} |
|
712 |
} |
|
713 |
||
714 |
/** |
|
715 |
* Removes all of the elements from this list. |
|
716 |
* The list will be empty after this call returns. |
|
717 |
*/ |
|
718 |
public void clear() { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
719 |
synchronized (lock) { |
2 | 720 |
setArray(new Object[0]); |
721 |
} |
|
722 |
} |
|
723 |
||
724 |
/** |
|
725 |
* Appends all of the elements in the specified collection to the end |
|
726 |
* of this list, in the order that they are returned by the specified |
|
727 |
* collection's iterator. |
|
728 |
* |
|
729 |
* @param c collection containing elements to be added to this list |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
730 |
* @return {@code true} if this list changed as a result of the call |
2 | 731 |
* @throws NullPointerException if the specified collection is null |
732 |
* @see #add(Object) |
|
733 |
*/ |
|
734 |
public boolean addAll(Collection<? extends E> c) { |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
735 |
Object[] cs = (c.getClass() == CopyOnWriteArrayList.class) ? |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
736 |
((CopyOnWriteArrayList<?>)c).getArray() : c.toArray(); |
2 | 737 |
if (cs.length == 0) |
738 |
return false; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
739 |
synchronized (lock) { |
49564 | 740 |
Object[] es = getArray(); |
741 |
int len = es.length; |
|
742 |
Object[] newElements; |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
743 |
if (len == 0 && cs.getClass() == Object[].class) |
49564 | 744 |
newElements = cs; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
745 |
else { |
49564 | 746 |
newElements = Arrays.copyOf(es, len + cs.length); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
747 |
System.arraycopy(cs, 0, newElements, len, cs.length); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
748 |
} |
49564 | 749 |
setArray(newElements); |
2 | 750 |
return true; |
751 |
} |
|
752 |
} |
|
753 |
||
754 |
/** |
|
755 |
* Inserts all of the elements in the specified collection into this |
|
756 |
* list, starting at the specified position. Shifts the element |
|
757 |
* currently at that position (if any) and any subsequent elements to |
|
758 |
* the right (increases their indices). The new elements will appear |
|
759 |
* in this list in the order that they are returned by the |
|
760 |
* specified collection's iterator. |
|
761 |
* |
|
762 |
* @param index index at which to insert the first element |
|
763 |
* from the specified collection |
|
764 |
* @param c collection containing elements to be added to this list |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
765 |
* @return {@code true} if this list changed as a result of the call |
2 | 766 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
767 |
* @throws NullPointerException if the specified collection is null |
|
768 |
* @see #add(int,Object) |
|
769 |
*/ |
|
770 |
public boolean addAll(int index, Collection<? extends E> c) { |
|
771 |
Object[] cs = c.toArray(); |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
772 |
synchronized (lock) { |
49564 | 773 |
Object[] es = getArray(); |
774 |
int len = es.length; |
|
2 | 775 |
if (index > len || index < 0) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
776 |
throw new IndexOutOfBoundsException(outOfBounds(index, len)); |
2 | 777 |
if (cs.length == 0) |
778 |
return false; |
|
779 |
int numMoved = len - index; |
|
780 |
Object[] newElements; |
|
781 |
if (numMoved == 0) |
|
49564 | 782 |
newElements = Arrays.copyOf(es, len + cs.length); |
2 | 783 |
else { |
784 |
newElements = new Object[len + cs.length]; |
|
49564 | 785 |
System.arraycopy(es, 0, newElements, 0, index); |
786 |
System.arraycopy(es, index, |
|
2 | 787 |
newElements, index + cs.length, |
788 |
numMoved); |
|
789 |
} |
|
790 |
System.arraycopy(cs, 0, newElements, index, cs.length); |
|
791 |
setArray(newElements); |
|
792 |
return true; |
|
793 |
} |
|
794 |
} |
|
795 |
||
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
796 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
797 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
798 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
799 |
public void forEach(Consumer<? super E> action) { |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42927
diff
changeset
|
800 |
Objects.requireNonNull(action); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
801 |
for (Object x : getArray()) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
802 |
@SuppressWarnings("unchecked") E e = (E) x; |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
803 |
action.accept(e); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
804 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
805 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
806 |
|
42927
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
807 |
/** |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
808 |
* @throws NullPointerException {@inheritDoc} |
1d31e540bfcb
8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents:
42319
diff
changeset
|
809 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
810 |
public boolean removeIf(Predicate<? super E> filter) { |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42927
diff
changeset
|
811 |
Objects.requireNonNull(filter); |
42319 | 812 |
return bulkRemove(filter); |
813 |
} |
|
814 |
||
815 |
// A tiny bit set implementation |
|
816 |
||
817 |
private static long[] nBits(int n) { |
|
818 |
return new long[((n - 1) >> 6) + 1]; |
|
819 |
} |
|
820 |
private static void setBit(long[] bits, int i) { |
|
821 |
bits[i >> 6] |= 1L << i; |
|
822 |
} |
|
823 |
private static boolean isClear(long[] bits, int i) { |
|
824 |
return (bits[i >> 6] & (1L << i)) == 0; |
|
825 |
} |
|
826 |
||
827 |
private boolean bulkRemove(Predicate<? super E> filter) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
828 |
synchronized (lock) { |
42319 | 829 |
return bulkRemove(filter, 0, getArray().length); |
830 |
} |
|
831 |
} |
|
832 |
||
833 |
boolean bulkRemove(Predicate<? super E> filter, int i, int end) { |
|
834 |
// assert Thread.holdsLock(lock); |
|
835 |
final Object[] es = getArray(); |
|
836 |
// Optimize for initial run of survivors |
|
837 |
for (; i < end && !filter.test(elementAt(es, i)); i++) |
|
838 |
; |
|
839 |
if (i < end) { |
|
840 |
final int beg = i; |
|
841 |
final long[] deathRow = nBits(end - beg); |
|
842 |
int deleted = 1; |
|
843 |
deathRow[0] = 1L; // set bit 0 |
|
844 |
for (i = beg + 1; i < end; i++) |
|
845 |
if (filter.test(elementAt(es, i))) { |
|
846 |
setBit(deathRow, i - beg); |
|
847 |
deleted++; |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
848 |
} |
42319 | 849 |
// Did filter reentrantly modify the list? |
850 |
if (es != getArray()) |
|
851 |
throw new ConcurrentModificationException(); |
|
852 |
final Object[] newElts = Arrays.copyOf(es, es.length - deleted); |
|
853 |
int w = beg; |
|
854 |
for (i = beg; i < end; i++) |
|
855 |
if (isClear(deathRow, i - beg)) |
|
856 |
newElts[w++] = es[i]; |
|
857 |
System.arraycopy(es, i, newElts, w, es.length - i); |
|
858 |
setArray(newElts); |
|
859 |
return true; |
|
860 |
} else { |
|
861 |
if (es != getArray()) |
|
862 |
throw new ConcurrentModificationException(); |
|
863 |
return false; |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
864 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
865 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
866 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
867 |
public void replaceAll(UnaryOperator<E> operator) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
868 |
synchronized (lock) { |
49564 | 869 |
replaceAllRange(operator, 0, getArray().length); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
870 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
871 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
872 |
|
49564 | 873 |
void replaceAllRange(UnaryOperator<E> operator, int i, int end) { |
42319 | 874 |
// assert Thread.holdsLock(lock); |
49564 | 875 |
Objects.requireNonNull(operator); |
42319 | 876 |
final Object[] es = getArray().clone(); |
877 |
for (; i < end; i++) |
|
878 |
es[i] = operator.apply(elementAt(es, i)); |
|
879 |
setArray(es); |
|
880 |
} |
|
881 |
||
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
882 |
public void sort(Comparator<? super E> c) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
883 |
synchronized (lock) { |
49564 | 884 |
sortRange(c, 0, getArray().length); |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
885 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
886 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
887 |
|
42319 | 888 |
@SuppressWarnings("unchecked") |
49564 | 889 |
void sortRange(Comparator<? super E> c, int i, int end) { |
42319 | 890 |
// assert Thread.holdsLock(lock); |
891 |
final Object[] es = getArray().clone(); |
|
892 |
Arrays.sort(es, i, end, (Comparator<Object>)c); |
|
893 |
setArray(es); |
|
894 |
} |
|
895 |
||
2 | 896 |
/** |
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
897 |
* Saves this list to a stream (that is, serializes it). |
2 | 898 |
* |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
899 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
900 |
* @throws java.io.IOException if an I/O error occurs |
2 | 901 |
* @serialData The length of the array backing the list is emitted |
902 |
* (int), followed by all of its elements (each an Object) |
|
903 |
* in the proper order. |
|
904 |
*/ |
|
905 |
private void writeObject(java.io.ObjectOutputStream s) |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
906 |
throws java.io.IOException { |
2 | 907 |
|
908 |
s.defaultWriteObject(); |
|
909 |
||
49564 | 910 |
Object[] es = getArray(); |
2 | 911 |
// Write out array length |
49564 | 912 |
s.writeInt(es.length); |
2 | 913 |
|
914 |
// Write out all elements in the proper order. |
|
49564 | 915 |
for (Object element : es) |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
7668
diff
changeset
|
916 |
s.writeObject(element); |
2 | 917 |
} |
918 |
||
919 |
/** |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
920 |
* Reconstitutes this list from a stream (that is, deserializes it). |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
921 |
* @param s the stream |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
922 |
* @throws ClassNotFoundException if the class of a serialized object |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
923 |
* could not be found |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
924 |
* @throws java.io.IOException if an I/O error occurs |
2 | 925 |
*/ |
926 |
private void readObject(java.io.ObjectInputStream s) |
|
927 |
throws java.io.IOException, ClassNotFoundException { |
|
928 |
||
929 |
s.defaultReadObject(); |
|
930 |
||
931 |
// bind to new lock |
|
932 |
resetLock(); |
|
933 |
||
934 |
// Read in array length and allocate array |
|
935 |
int len = s.readInt(); |
|
47423 | 936 |
SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, len); |
49564 | 937 |
Object[] es = new Object[len]; |
2 | 938 |
|
939 |
// Read in all elements in the proper order. |
|
940 |
for (int i = 0; i < len; i++) |
|
49564 | 941 |
es[i] = s.readObject(); |
942 |
setArray(es); |
|
2 | 943 |
} |
944 |
||
945 |
/** |
|
946 |
* Returns a string representation of this list. The string |
|
947 |
* representation consists of the string representations of the list's |
|
948 |
* elements in the order they are returned by its iterator, enclosed in |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
949 |
* square brackets ({@code "[]"}). Adjacent elements are separated by |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
950 |
* the characters {@code ", "} (comma and space). Elements are |
2 | 951 |
* converted to strings as by {@link String#valueOf(Object)}. |
952 |
* |
|
953 |
* @return a string representation of this list |
|
954 |
*/ |
|
955 |
public String toString() { |
|
956 |
return Arrays.toString(getArray()); |
|
957 |
} |
|
958 |
||
959 |
/** |
|
960 |
* Compares the specified object with this list for equality. |
|
961 |
* Returns {@code true} if the specified object is the same object |
|
962 |
* as this object, or if it is also a {@link List} and the sequence |
|
963 |
* of elements returned by an {@linkplain List#iterator() iterator} |
|
964 |
* over the specified list is the same as the sequence returned by |
|
965 |
* an iterator over this list. The two sequences are considered to |
|
966 |
* be the same if they have the same length and corresponding |
|
967 |
* elements at the same position in the sequence are <em>equal</em>. |
|
968 |
* Two elements {@code e1} and {@code e2} are considered |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
969 |
* <em>equal</em> if {@code Objects.equals(e1, e2)}. |
2 | 970 |
* |
971 |
* @param o the object to be compared for equality with this list |
|
972 |
* @return {@code true} if the specified object is equal to this list |
|
973 |
*/ |
|
974 |
public boolean equals(Object o) { |
|
975 |
if (o == this) |
|
976 |
return true; |
|
977 |
if (!(o instanceof List)) |
|
978 |
return false; |
|
979 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
980 |
List<?> list = (List<?>)o; |
2 | 981 |
Iterator<?> it = list.iterator(); |
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47423
diff
changeset
|
982 |
for (Object element : getArray()) |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47423
diff
changeset
|
983 |
if (!it.hasNext() || !Objects.equals(element, it.next())) |
2 | 984 |
return false; |
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47423
diff
changeset
|
985 |
return !it.hasNext(); |
2 | 986 |
} |
987 |
||
49564 | 988 |
private static int hashCodeOfRange(Object[] es, int from, int to) { |
989 |
int hashCode = 1; |
|
990 |
for (int i = from; i < to; i++) { |
|
991 |
Object x = es[i]; |
|
992 |
hashCode = 31 * hashCode + (x == null ? 0 : x.hashCode()); |
|
993 |
} |
|
994 |
return hashCode; |
|
995 |
} |
|
996 |
||
2 | 997 |
/** |
998 |
* Returns the hash code value for this list. |
|
999 |
* |
|
1000 |
* <p>This implementation uses the definition in {@link List#hashCode}. |
|
1001 |
* |
|
1002 |
* @return the hash code value for this list |
|
1003 |
*/ |
|
1004 |
public int hashCode() { |
|
49564 | 1005 |
Object[] es = getArray(); |
1006 |
return hashCodeOfRange(es, 0, es.length); |
|
2 | 1007 |
} |
1008 |
||
1009 |
/** |
|
1010 |
* Returns an iterator over the elements in this list in proper sequence. |
|
1011 |
* |
|
1012 |
* <p>The returned iterator provides a snapshot of the state of the list |
|
1013 |
* when the iterator was constructed. No synchronization is needed while |
|
1014 |
* traversing the iterator. The iterator does <em>NOT</em> support the |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1015 |
* {@code remove} method. |
2 | 1016 |
* |
1017 |
* @return an iterator over the elements in this list in proper sequence |
|
1018 |
*/ |
|
1019 |
public Iterator<E> iterator() { |
|
1020 |
return new COWIterator<E>(getArray(), 0); |
|
1021 |
} |
|
1022 |
||
1023 |
/** |
|
1024 |
* {@inheritDoc} |
|
1025 |
* |
|
1026 |
* <p>The returned iterator provides a snapshot of the state of the list |
|
1027 |
* when the iterator was constructed. No synchronization is needed while |
|
1028 |
* traversing the iterator. The iterator does <em>NOT</em> support the |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1029 |
* {@code remove}, {@code set} or {@code add} methods. |
2 | 1030 |
*/ |
1031 |
public ListIterator<E> listIterator() { |
|
1032 |
return new COWIterator<E>(getArray(), 0); |
|
1033 |
} |
|
1034 |
||
1035 |
/** |
|
1036 |
* {@inheritDoc} |
|
1037 |
* |
|
1038 |
* <p>The returned iterator provides a snapshot of the state of the list |
|
1039 |
* when the iterator was constructed. No synchronization is needed while |
|
1040 |
* traversing the iterator. The iterator does <em>NOT</em> support the |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1041 |
* {@code remove}, {@code set} or {@code add} methods. |
2 | 1042 |
* |
1043 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
1044 |
*/ |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1045 |
public ListIterator<E> listIterator(int index) { |
49564 | 1046 |
Object[] es = getArray(); |
1047 |
int len = es.length; |
|
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1048 |
if (index < 0 || index > len) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
1049 |
throw new IndexOutOfBoundsException(outOfBounds(index, len)); |
2 | 1050 |
|
49564 | 1051 |
return new COWIterator<E>(es, index); |
2 | 1052 |
} |
1053 |
||
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1054 |
/** |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1055 |
* Returns a {@link Spliterator} over the elements in this list. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1056 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1057 |
* <p>The {@code Spliterator} reports {@link Spliterator#IMMUTABLE}, |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1058 |
* {@link Spliterator#ORDERED}, {@link Spliterator#SIZED}, and |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1059 |
* {@link Spliterator#SUBSIZED}. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1060 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1061 |
* <p>The spliterator provides a snapshot of the state of the list |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1062 |
* when the spliterator was constructed. No synchronization is needed while |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1063 |
* operating on the spliterator. |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1064 |
* |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1065 |
* @return a {@code Spliterator} over the elements in this list |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1066 |
* @since 1.8 |
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1067 |
*/ |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1068 |
public Spliterator<E> spliterator() { |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1069 |
return Spliterators.spliterator |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1070 |
(getArray(), Spliterator.IMMUTABLE | Spliterator.ORDERED); |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1071 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1072 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1073 |
static final class COWIterator<E> implements ListIterator<E> { |
7518 | 1074 |
/** Snapshot of the array */ |
2 | 1075 |
private final Object[] snapshot; |
1076 |
/** Index of element to be returned by subsequent call to next. */ |
|
1077 |
private int cursor; |
|
1078 |
||
49564 | 1079 |
COWIterator(Object[] es, int initialCursor) { |
2 | 1080 |
cursor = initialCursor; |
49564 | 1081 |
snapshot = es; |
2 | 1082 |
} |
1083 |
||
1084 |
public boolean hasNext() { |
|
1085 |
return cursor < snapshot.length; |
|
1086 |
} |
|
1087 |
||
1088 |
public boolean hasPrevious() { |
|
1089 |
return cursor > 0; |
|
1090 |
} |
|
1091 |
||
1092 |
@SuppressWarnings("unchecked") |
|
1093 |
public E next() { |
|
1094 |
if (! hasNext()) |
|
1095 |
throw new NoSuchElementException(); |
|
1096 |
return (E) snapshot[cursor++]; |
|
1097 |
} |
|
1098 |
||
1099 |
@SuppressWarnings("unchecked") |
|
1100 |
public E previous() { |
|
1101 |
if (! hasPrevious()) |
|
1102 |
throw new NoSuchElementException(); |
|
1103 |
return (E) snapshot[--cursor]; |
|
1104 |
} |
|
1105 |
||
1106 |
public int nextIndex() { |
|
1107 |
return cursor; |
|
1108 |
} |
|
1109 |
||
1110 |
public int previousIndex() { |
|
49564 | 1111 |
return cursor - 1; |
2 | 1112 |
} |
1113 |
||
1114 |
/** |
|
1115 |
* Not supported. Always throws UnsupportedOperationException. |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1116 |
* @throws UnsupportedOperationException always; {@code remove} |
2 | 1117 |
* is not supported by this iterator. |
1118 |
*/ |
|
1119 |
public void remove() { |
|
1120 |
throw new UnsupportedOperationException(); |
|
1121 |
} |
|
1122 |
||
1123 |
/** |
|
1124 |
* Not supported. Always throws UnsupportedOperationException. |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1125 |
* @throws UnsupportedOperationException always; {@code set} |
2 | 1126 |
* is not supported by this iterator. |
1127 |
*/ |
|
1128 |
public void set(E e) { |
|
1129 |
throw new UnsupportedOperationException(); |
|
1130 |
} |
|
1131 |
||
1132 |
/** |
|
1133 |
* Not supported. Always throws UnsupportedOperationException. |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1134 |
* @throws UnsupportedOperationException always; {@code add} |
2 | 1135 |
* is not supported by this iterator. |
1136 |
*/ |
|
1137 |
public void add(E e) { |
|
1138 |
throw new UnsupportedOperationException(); |
|
1139 |
} |
|
17180
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1140 |
|
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1141 |
@Override |
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1142 |
public void forEachRemaining(Consumer<? super E> action) { |
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1143 |
Objects.requireNonNull(action); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
1144 |
final int size = snapshot.length; |
49564 | 1145 |
int i = cursor; |
17180
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1146 |
cursor = size; |
49564 | 1147 |
for (; i < size; i++) |
1148 |
action.accept(elementAt(snapshot, i)); |
|
17180
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1149 |
} |
2 | 1150 |
} |
1151 |
||
1152 |
/** |
|
1153 |
* Returns a view of the portion of this list between |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1154 |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. |
2 | 1155 |
* The returned list is backed by this list, so changes in the |
1156 |
* returned list are reflected in this list. |
|
1157 |
* |
|
1158 |
* <p>The semantics of the list returned by this method become |
|
1159 |
* undefined if the backing list (i.e., this list) is modified in |
|
1160 |
* any way other than via the returned list. |
|
1161 |
* |
|
1162 |
* @param fromIndex low endpoint (inclusive) of the subList |
|
1163 |
* @param toIndex high endpoint (exclusive) of the subList |
|
1164 |
* @return a view of the specified range within this list |
|
1165 |
* @throws IndexOutOfBoundsException {@inheritDoc} |
|
1166 |
*/ |
|
1167 |
public List<E> subList(int fromIndex, int toIndex) { |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
1168 |
synchronized (lock) { |
49564 | 1169 |
Object[] es = getArray(); |
1170 |
int len = es.length; |
|
1171 |
int size = toIndex - fromIndex; |
|
1172 |
if (fromIndex < 0 || toIndex > len || size < 0) |
|
2 | 1173 |
throw new IndexOutOfBoundsException(); |
49564 | 1174 |
return new COWSubList(es, fromIndex, size); |
2 | 1175 |
} |
1176 |
} |
|
1177 |
||
1178 |
/** |
|
1179 |
* Sublist for CopyOnWriteArrayList. |
|
1180 |
*/ |
|
49564 | 1181 |
private class COWSubList implements List<E>, RandomAccess { |
2 | 1182 |
private final int offset; |
1183 |
private int size; |
|
1184 |
private Object[] expectedArray; |
|
1185 |
||
49564 | 1186 |
COWSubList(Object[] es, int offset, int size) { |
1187 |
// assert Thread.holdsLock(lock); |
|
1188 |
expectedArray = es; |
|
1189 |
this.offset = offset; |
|
1190 |
this.size = size; |
|
2 | 1191 |
} |
1192 |
||
1193 |
private void checkForComodification() { |
|
49564 | 1194 |
// assert Thread.holdsLock(lock); |
1195 |
if (getArray() != expectedArray) |
|
2 | 1196 |
throw new ConcurrentModificationException(); |
1197 |
} |
|
1198 |
||
42319 | 1199 |
private Object[] getArrayChecked() { |
49564 | 1200 |
// assert Thread.holdsLock(lock); |
1201 |
Object[] a = getArray(); |
|
42319 | 1202 |
if (a != expectedArray) |
1203 |
throw new ConcurrentModificationException(); |
|
1204 |
return a; |
|
1205 |
} |
|
1206 |
||
2 | 1207 |
private void rangeCheck(int index) { |
49564 | 1208 |
// assert Thread.holdsLock(lock); |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1209 |
if (index < 0 || index >= size) |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
1210 |
throw new IndexOutOfBoundsException(outOfBounds(index, size)); |
2 | 1211 |
} |
1212 |
||
49564 | 1213 |
private void rangeCheckForAdd(int index) { |
1214 |
// assert Thread.holdsLock(lock); |
|
1215 |
if (index < 0 || index > size) |
|
1216 |
throw new IndexOutOfBoundsException(outOfBounds(index, size)); |
|
1217 |
} |
|
1218 |
||
1219 |
public Object[] toArray() { |
|
1220 |
final Object[] es; |
|
1221 |
final int offset; |
|
1222 |
final int size; |
|
1223 |
synchronized (lock) { |
|
1224 |
es = getArrayChecked(); |
|
1225 |
offset = this.offset; |
|
1226 |
size = this.size; |
|
1227 |
} |
|
1228 |
return Arrays.copyOfRange(es, offset, offset + size); |
|
1229 |
} |
|
1230 |
||
1231 |
@SuppressWarnings("unchecked") |
|
1232 |
public <T> T[] toArray(T[] a) { |
|
1233 |
final Object[] es; |
|
1234 |
final int offset; |
|
1235 |
final int size; |
|
1236 |
synchronized (lock) { |
|
1237 |
es = getArrayChecked(); |
|
1238 |
offset = this.offset; |
|
1239 |
size = this.size; |
|
1240 |
} |
|
1241 |
if (a.length < size) |
|
1242 |
return (T[]) Arrays.copyOfRange( |
|
1243 |
es, offset, offset + size, a.getClass()); |
|
1244 |
else { |
|
1245 |
System.arraycopy(es, offset, a, 0, size); |
|
1246 |
if (a.length > size) |
|
1247 |
a[size] = null; |
|
1248 |
return a; |
|
1249 |
} |
|
1250 |
} |
|
1251 |
||
1252 |
public int indexOf(Object o) { |
|
1253 |
final Object[] es; |
|
1254 |
final int offset; |
|
1255 |
final int size; |
|
1256 |
synchronized (lock) { |
|
1257 |
es = getArrayChecked(); |
|
1258 |
offset = this.offset; |
|
1259 |
size = this.size; |
|
1260 |
} |
|
1261 |
int i = indexOfRange(o, es, offset, offset + size); |
|
1262 |
return (i == -1) ? -1 : i - offset; |
|
1263 |
} |
|
1264 |
||
1265 |
public int lastIndexOf(Object o) { |
|
1266 |
final Object[] es; |
|
1267 |
final int offset; |
|
1268 |
final int size; |
|
1269 |
synchronized (lock) { |
|
1270 |
es = getArrayChecked(); |
|
1271 |
offset = this.offset; |
|
1272 |
size = this.size; |
|
1273 |
} |
|
1274 |
int i = lastIndexOfRange(o, es, offset, offset + size); |
|
1275 |
return (i == -1) ? -1 : i - offset; |
|
1276 |
} |
|
1277 |
||
1278 |
public boolean contains(Object o) { |
|
1279 |
return indexOf(o) >= 0; |
|
1280 |
} |
|
1281 |
||
1282 |
public boolean containsAll(Collection<?> c) { |
|
1283 |
final Object[] es; |
|
1284 |
final int offset; |
|
1285 |
final int size; |
|
1286 |
synchronized (lock) { |
|
1287 |
es = getArrayChecked(); |
|
1288 |
offset = this.offset; |
|
1289 |
size = this.size; |
|
1290 |
} |
|
1291 |
for (Object o : c) |
|
1292 |
if (indexOfRange(o, es, offset, offset + size) < 0) |
|
1293 |
return false; |
|
1294 |
return true; |
|
1295 |
} |
|
1296 |
||
1297 |
public boolean isEmpty() { |
|
1298 |
return size() == 0; |
|
1299 |
} |
|
1300 |
||
1301 |
public String toString() { |
|
1302 |
return Arrays.toString(toArray()); |
|
1303 |
} |
|
1304 |
||
1305 |
public int hashCode() { |
|
1306 |
final Object[] es; |
|
1307 |
final int offset; |
|
1308 |
final int size; |
|
1309 |
synchronized (lock) { |
|
1310 |
es = getArrayChecked(); |
|
1311 |
offset = this.offset; |
|
1312 |
size = this.size; |
|
1313 |
} |
|
1314 |
return hashCodeOfRange(es, offset, offset + size); |
|
1315 |
} |
|
1316 |
||
1317 |
public boolean equals(Object o) { |
|
1318 |
if (o == this) |
|
1319 |
return true; |
|
1320 |
if (!(o instanceof List)) |
|
1321 |
return false; |
|
1322 |
Iterator<?> it = ((List<?>)o).iterator(); |
|
1323 |
||
1324 |
final Object[] es; |
|
1325 |
final int offset; |
|
1326 |
final int size; |
|
1327 |
synchronized (lock) { |
|
1328 |
es = getArrayChecked(); |
|
1329 |
offset = this.offset; |
|
1330 |
size = this.size; |
|
1331 |
} |
|
1332 |
||
1333 |
for (int i = offset, end = offset + size; i < end; i++) |
|
1334 |
if (!it.hasNext() || !Objects.equals(es[i], it.next())) |
|
1335 |
return false; |
|
1336 |
return !it.hasNext(); |
|
1337 |
} |
|
1338 |
||
2 | 1339 |
public E set(int index, E element) { |
49564 | 1340 |
synchronized (lock) { |
2 | 1341 |
rangeCheck(index); |
1342 |
checkForComodification(); |
|
49564 | 1343 |
E x = CopyOnWriteArrayList.this.set(offset + index, element); |
1344 |
expectedArray = getArray(); |
|
2 | 1345 |
return x; |
1346 |
} |
|
1347 |
} |
|
1348 |
||
1349 |
public E get(int index) { |
|
49564 | 1350 |
synchronized (lock) { |
2 | 1351 |
rangeCheck(index); |
1352 |
checkForComodification(); |
|
49564 | 1353 |
return CopyOnWriteArrayList.this.get(offset + index); |
2 | 1354 |
} |
1355 |
} |
|
1356 |
||
1357 |
public int size() { |
|
49564 | 1358 |
synchronized (lock) { |
2 | 1359 |
checkForComodification(); |
1360 |
return size; |
|
1361 |
} |
|
1362 |
} |
|
1363 |
||
42319 | 1364 |
public boolean add(E element) { |
49564 | 1365 |
synchronized (lock) { |
42319 | 1366 |
checkForComodification(); |
49564 | 1367 |
CopyOnWriteArrayList.this.add(offset + size, element); |
1368 |
expectedArray = getArray(); |
|
42319 | 1369 |
size++; |
1370 |
} |
|
1371 |
return true; |
|
1372 |
} |
|
1373 |
||
2 | 1374 |
public void add(int index, E element) { |
49564 | 1375 |
synchronized (lock) { |
2 | 1376 |
checkForComodification(); |
49564 | 1377 |
rangeCheckForAdd(index); |
1378 |
CopyOnWriteArrayList.this.add(offset + index, element); |
|
1379 |
expectedArray = getArray(); |
|
2 | 1380 |
size++; |
1381 |
} |
|
1382 |
} |
|
1383 |
||
42319 | 1384 |
public boolean addAll(Collection<? extends E> c) { |
49564 | 1385 |
synchronized (lock) { |
42319 | 1386 |
final Object[] oldArray = getArrayChecked(); |
49564 | 1387 |
boolean modified = |
1388 |
CopyOnWriteArrayList.this.addAll(offset + size, c); |
|
1389 |
size += (expectedArray = getArray()).length - oldArray.length; |
|
1390 |
return modified; |
|
1391 |
} |
|
1392 |
} |
|
1393 |
||
1394 |
public boolean addAll(int index, Collection<? extends E> c) { |
|
1395 |
synchronized (lock) { |
|
1396 |
rangeCheckForAdd(index); |
|
1397 |
final Object[] oldArray = getArrayChecked(); |
|
1398 |
boolean modified = |
|
1399 |
CopyOnWriteArrayList.this.addAll(offset + index, c); |
|
1400 |
size += (expectedArray = getArray()).length - oldArray.length; |
|
42319 | 1401 |
return modified; |
1402 |
} |
|
1403 |
} |
|
1404 |
||
2 | 1405 |
public void clear() { |
49564 | 1406 |
synchronized (lock) { |
2 | 1407 |
checkForComodification(); |
49564 | 1408 |
removeRange(offset, offset + size); |
1409 |
expectedArray = getArray(); |
|
2 | 1410 |
size = 0; |
1411 |
} |
|
1412 |
} |
|
1413 |
||
1414 |
public E remove(int index) { |
|
49564 | 1415 |
synchronized (lock) { |
2 | 1416 |
rangeCheck(index); |
1417 |
checkForComodification(); |
|
49564 | 1418 |
E result = CopyOnWriteArrayList.this.remove(offset + index); |
1419 |
expectedArray = getArray(); |
|
2 | 1420 |
size--; |
1421 |
return result; |
|
1422 |
} |
|
1423 |
} |
|
1424 |
||
1425 |
public boolean remove(Object o) { |
|
49564 | 1426 |
synchronized (lock) { |
42319 | 1427 |
checkForComodification(); |
1428 |
int index = indexOf(o); |
|
1429 |
if (index == -1) |
|
1430 |
return false; |
|
1431 |
remove(index); |
|
1432 |
return true; |
|
1433 |
} |
|
2 | 1434 |
} |
1435 |
||
1436 |
public Iterator<E> iterator() { |
|
49564 | 1437 |
return listIterator(0); |
1438 |
} |
|
1439 |
||
1440 |
public ListIterator<E> listIterator() { |
|
1441 |
return listIterator(0); |
|
2 | 1442 |
} |
1443 |
||
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1444 |
public ListIterator<E> listIterator(int index) { |
49564 | 1445 |
synchronized (lock) { |
2 | 1446 |
checkForComodification(); |
49564 | 1447 |
rangeCheckForAdd(index); |
1448 |
return new COWSubListIterator<E>( |
|
1449 |
CopyOnWriteArrayList.this, index, offset, size); |
|
2 | 1450 |
} |
1451 |
} |
|
1452 |
||
1453 |
public List<E> subList(int fromIndex, int toIndex) { |
|
49564 | 1454 |
synchronized (lock) { |
2 | 1455 |
checkForComodification(); |
22628
9186623f7045
8011645: CopyOnWriteArrayList.COWSubList.subList does not validate range properly
chegar
parents:
19428
diff
changeset
|
1456 |
if (fromIndex < 0 || toIndex > size || fromIndex > toIndex) |
2 | 1457 |
throw new IndexOutOfBoundsException(); |
49564 | 1458 |
return new COWSubList(expectedArray, fromIndex + offset, toIndex - fromIndex); |
2 | 1459 |
} |
1460 |
} |
|
1461 |
||
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1462 |
public void forEach(Consumer<? super E> action) { |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42927
diff
changeset
|
1463 |
Objects.requireNonNull(action); |
42319 | 1464 |
int i, end; final Object[] es; |
49564 | 1465 |
synchronized (lock) { |
42319 | 1466 |
es = getArrayChecked(); |
1467 |
i = offset; |
|
1468 |
end = i + size; |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1469 |
} |
42319 | 1470 |
for (; i < end; i++) |
1471 |
action.accept(elementAt(es, i)); |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1472 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1473 |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1474 |
public void replaceAll(UnaryOperator<E> operator) { |
49564 | 1475 |
synchronized (lock) { |
42319 | 1476 |
checkForComodification(); |
49564 | 1477 |
replaceAllRange(operator, offset, offset + size); |
1478 |
expectedArray = getArray(); |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1479 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1480 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1481 |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1482 |
public void sort(Comparator<? super E> c) { |
49564 | 1483 |
synchronized (lock) { |
42319 | 1484 |
checkForComodification(); |
49564 | 1485 |
sortRange(c, offset, offset + size); |
1486 |
expectedArray = getArray(); |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1487 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1488 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1489 |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1490 |
public boolean removeAll(Collection<?> c) { |
42319 | 1491 |
Objects.requireNonNull(c); |
1492 |
return bulkRemove(e -> c.contains(e)); |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1493 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1494 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1495 |
public boolean retainAll(Collection<?> c) { |
42319 | 1496 |
Objects.requireNonNull(c); |
1497 |
return bulkRemove(e -> !c.contains(e)); |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1498 |
} |
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1499 |
|
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1500 |
public boolean removeIf(Predicate<? super E> filter) { |
42319 | 1501 |
Objects.requireNonNull(filter); |
1502 |
return bulkRemove(filter); |
|
1503 |
} |
|
1504 |
||
1505 |
private boolean bulkRemove(Predicate<? super E> filter) { |
|
49564 | 1506 |
synchronized (lock) { |
42319 | 1507 |
final Object[] oldArray = getArrayChecked(); |
49564 | 1508 |
boolean modified = CopyOnWriteArrayList.this.bulkRemove( |
1509 |
filter, offset, offset + size); |
|
1510 |
size += (expectedArray = getArray()).length - oldArray.length; |
|
42319 | 1511 |
return modified; |
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1512 |
} |
18767
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1513 |
} |
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1514 |
|
6214297bf27d
8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents:
17180
diff
changeset
|
1515 |
public Spliterator<E> spliterator() { |
49564 | 1516 |
synchronized (lock) { |
42319 | 1517 |
return Spliterators.spliterator( |
1518 |
getArrayChecked(), offset, offset + size, |
|
1519 |
Spliterator.IMMUTABLE | Spliterator.ORDERED); |
|
1520 |
} |
|
17166
c83a0fa44906
8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
14342
diff
changeset
|
1521 |
} |
19428
83f87aff7b07
8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents:
18767
diff
changeset
|
1522 |
|
2 | 1523 |
} |
1524 |
||
1525 |
private static class COWSubListIterator<E> implements ListIterator<E> { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1526 |
private final ListIterator<E> it; |
2 | 1527 |
private final int offset; |
1528 |
private final int size; |
|
1529 |
||
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1530 |
COWSubListIterator(List<E> l, int index, int offset, int size) { |
2 | 1531 |
this.offset = offset; |
1532 |
this.size = size; |
|
49564 | 1533 |
it = l.listIterator(index + offset); |
2 | 1534 |
} |
1535 |
||
1536 |
public boolean hasNext() { |
|
1537 |
return nextIndex() < size; |
|
1538 |
} |
|
1539 |
||
1540 |
public E next() { |
|
1541 |
if (hasNext()) |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1542 |
return it.next(); |
2 | 1543 |
else |
1544 |
throw new NoSuchElementException(); |
|
1545 |
} |
|
1546 |
||
1547 |
public boolean hasPrevious() { |
|
1548 |
return previousIndex() >= 0; |
|
1549 |
} |
|
1550 |
||
1551 |
public E previous() { |
|
1552 |
if (hasPrevious()) |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1553 |
return it.previous(); |
2 | 1554 |
else |
1555 |
throw new NoSuchElementException(); |
|
1556 |
} |
|
1557 |
||
1558 |
public int nextIndex() { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1559 |
return it.nextIndex() - offset; |
2 | 1560 |
} |
1561 |
||
1562 |
public int previousIndex() { |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
11279
diff
changeset
|
1563 |
return it.previousIndex() - offset; |
2 | 1564 |
} |
1565 |
||
1566 |
public void remove() { |
|
1567 |
throw new UnsupportedOperationException(); |
|
1568 |
} |
|
1569 |
||
1570 |
public void set(E e) { |
|
1571 |
throw new UnsupportedOperationException(); |
|
1572 |
} |
|
1573 |
||
1574 |
public void add(E e) { |
|
1575 |
throw new UnsupportedOperationException(); |
|
1576 |
} |
|
17180
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1577 |
|
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1578 |
@Override |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
1579 |
@SuppressWarnings("unchecked") |
17180
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1580 |
public void forEachRemaining(Consumer<? super E> action) { |
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1581 |
Objects.requireNonNull(action); |
49564 | 1582 |
while (hasNext()) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
31540
diff
changeset
|
1583 |
action.accept(it.next()); |
17180
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1584 |
} |
f568bc4ece21
8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents:
17166
diff
changeset
|
1585 |
} |
2 | 1586 |
} |
1587 |
||
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1588 |
/** Initializes the lock; for use when deserializing or cloning. */ |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1589 |
private void resetLock() { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1590 |
Field lockField = java.security.AccessController.doPrivileged( |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1591 |
(java.security.PrivilegedAction<Field>) () -> { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1592 |
try { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1593 |
Field f = CopyOnWriteArrayList.class |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1594 |
.getDeclaredField("lock"); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1595 |
f.setAccessible(true); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1596 |
return f; |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1597 |
} catch (ReflectiveOperationException e) { |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1598 |
throw new Error(e); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1599 |
}}); |
2 | 1600 |
try { |
39725
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1601 |
lockField.set(this, new Object()); |
9548f8d846e9
8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents:
33674
diff
changeset
|
1602 |
} catch (IllegalAccessException e) { |
8544
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1603 |
throw new Error(e); |
225896f7b33c
7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents:
7976
diff
changeset
|
1604 |
} |
2 | 1605 |
} |
1606 |
} |