ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArrayList.java
Revision: 1.55
Committed: Sun Sep 11 03:13:26 2005 UTC (18 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.54: +16 -21 lines
Log Message:
toString; equals

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