ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArrayList.java
Revision: 1.60
Committed: Sat Oct 1 22:09:39 2005 UTC (18 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.59: +17 -16 lines
Log Message:
Review Rework

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