ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArrayList.java
Revision: 1.50
Committed: Thu Aug 25 03:16:10 2005 UTC (18 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.49: +4 -3 lines
Log Message:
sync up listIterator comments

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