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