ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArrayList.java
Revision: 1.144
Committed: Sun Oct 22 17:44:03 2017 UTC (6 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.143: +2 -0 lines
Log Message:
sync 8174109: Better queuing priorities

File Contents

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