ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CopyOnWriteArrayList.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/CopyOnWriteArrayList.java (file contents):
Revision 1.34 by jsr166, Mon May 2 18:38:53 2005 UTC vs.
Revision 1.35 by jsr166, Tue May 17 18:17:35 2005 UTC

# Line 19 | Line 19 | import java.util.*;
19  
20   /**
21   * A thread-safe variant of {@link java.util.ArrayList} in which all mutative
22 < * operations (add, set, and so on) are implemented by making a fresh
23 < * copy of the underlying array.
22 > * operations (<tt>add</tt>, <tt>set</tt>, and so on) are implemented by
23 > * making a fresh copy of the underlying array.
24   *
25   * <p> This is ordinarily too costly, but may be <em>more</em> efficient
26   * than alternatives when traversal operations vastly outnumber
# Line 33 | Line 33 | import java.util.*;
33   * guaranteed not to throw <tt>ConcurrentModificationException</tt>.
34   * The iterator will not reflect additions, removals, or changes to
35   * the list since the iterator was created.  Element-changing
36 < * operations on iterators themselves (remove, set, and add) are not
37 < * supported. These methods throw
36 > * operations on iterators themselves (<tt>remove</tt>, <tt>set</tt>, and
37 > * <tt>add</tt>) are not supported. These methods throw
38   * <tt>UnsupportedOperationException</tt>.
39   *
40 + * <p>All elements are permitted, including <tt>null</tt>.
41 + *
42   * <p>This class is a member of the
43   * <a href="{@docRoot}/../guide/collections/index.html">
44   * Java Collections Framework</a>.
# Line 51 | Line 53 | public class CopyOnWriteArrayList<E>
53  
54      /**
55       * The held array. Directly accessed only within synchronized
56 <     *  methods
56 >     * methods.
57       */
58      private volatile transient E[] array;
59  
# Line 74 | Line 76 | public class CopyOnWriteArrayList<E>
76       * iterator.
77       *
78       * @param c the collection of initially held elements
79 +     * @throws NullPointerException if the specified collection is null
80       */
81      public CopyOnWriteArrayList(Collection<? extends E> c) {
82          array = (E[]) new Object[c.size()];
# Line 84 | Line 87 | public class CopyOnWriteArrayList<E>
87      }
88  
89      /**
90 <     * Creates a new CopyOnWriteArrayList holding a copy of given array.
90 >     * Creates a list holding a copy of the given array.
91       *
92       * @param toCopyIn the array (a copy of this array is used as the
93       *        internal array)
# Line 106 | Line 109 | public class CopyOnWriteArrayList<E>
109       * the list.
110       */
111      private synchronized void copyIn(E[] toCopyIn, int first, int n) {
112 <        array  = (E[]) new Object[n];
112 >        array = (E[]) new Object[n];
113          System.arraycopy(toCopyIn, first, array, 0, n);
114      }
115  
116      /**
117       * Returns the number of elements in this list.
118       *
119 <     * @return  the number of elements in this list.
119 >     * @return the number of elements in this list
120       */
121      public int size() {
122          return array().length;
123      }
124  
125      /**
126 <     * Tests if this list has no elements.
126 >     * Returns <tt>true</tt> if this list contains no elements.
127       *
128 <     * @return  <tt>true</tt> if this list has no elements;
126 <     *          <tt>false</tt> otherwise.
128 >     * @return <tt>true</tt> if this list contains no elements
129       */
130      public boolean isEmpty() {
131          return size() == 0;
# Line 131 | Line 133 | public class CopyOnWriteArrayList<E>
133  
134      /**
135       * Returns <tt>true</tt> if this list contains the specified element.
136 +     * More formally, returns <tt>true</tt> if and only if this list contains
137 +     * at least one element <tt>e</tt> such that
138 +     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
139       *
140 <     * @param elem element whose presence in this List is to be tested.
141 <     * @return  <code>true</code> if the specified element is present;
137 <     *          <code>false</code> otherwise.
140 >     * @param o element whose presence in this list is to be tested
141 >     * @return <tt>true</tt> if this list contains the specified element
142       */
143 <    public boolean contains(Object elem) {
143 >    public boolean contains(Object o) {
144          E[] elementData = array();
145          int len = elementData.length;
146 <        return indexOf(elem, elementData, len) >= 0;
146 >        return indexOf(o, elementData, len) >= 0;
147      }
148  
149      /**
150 <     * Searches for the first occurrence of the given argument, testing
147 <     * for equality using the <tt>equals</tt> method.
148 <     *
149 <     * @param   elem   an object.
150 <     * @return  the index of the first occurrence of the argument in this
151 <     *          list; returns <tt>-1</tt> if the object is not found.
152 <     * @see     Object#equals(Object)
150 >     * {@inheritDoc}
151       */
152 <    public int indexOf(Object elem) {
152 >    public int indexOf(Object o) {
153          E[] elementData = array();
154          int len = elementData.length;
155 <        return indexOf(elem, elementData, len);
155 >        return indexOf(o, elementData, len);
156      }
157  
158      /**
159 <     * static version allows repeated call without needed
159 >     * static version allows repeated call without needing
160       * to grab lock for array each time
161       */
162 <    private static int indexOf(Object elem, Object[] elementData, int len) {
163 <        if (elem == null) {
162 >    private static int indexOf(Object o, Object[] elementData, int len) {
163 >        if (o == null) {
164              for (int i = 0; i < len; i++)
165                  if (elementData[i]==null)
166                      return i;
167          } else {
168              for (int i = 0; i < len; i++)
169 <                if (elem.equals(elementData[i]))
169 >                if (o.equals(elementData[i]))
170                      return i;
171          }
172          return -1;
173      }
174  
175      /**
176 <     * Searches for the first occurrence of the given argument, beginning
177 <     * the search at <tt>index</tt>, and testing for equality using
178 <     * the <tt>equals</tt> method.
179 <     *
180 <     * @param   elem    an object.
181 <     * @param   index   the index to start searching from.
182 <     * @return  the index of the first occurrence of the object argument in
183 <     *          this List at position <tt>index</tt> or later in the
184 <     *          List; returns <tt>-1</tt> if the object is not found.
185 <     * @see     Object#equals(Object)
176 >     * Returns the index of the first occurrence of the specified element in
177 >     * this list, searching forwards from <tt>index</tt>, or returns -1 if
178 >     * the element is not found.
179 >     * More formally, returns the lowest index <tt>i</tt> such that
180 >     * <tt>(i&nbsp;&gt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(e==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;e.equals(get(i))))</tt>,
181 >     * or -1 if there is no such index.
182 >     *
183 >     * @param e element to search for
184 >     * @param index index to start searching from
185 >     * @return the index of the first occurrence of the element in
186 >     *         this list at position <tt>index</tt> or later in the list;
187 >     *         <tt>-1</tt> if the element is not found.
188 >     * @throws IndexOutOfBoundsException if the specified index is negative
189       */
190 <    public int indexOf(E elem, int index) {
190 >    public int indexOf(E e, int index) {
191          E[] elementData = array();
192          int elementCount = elementData.length;
193  
194 <        if (elem == null) {
194 >        if (e == null) {
195              for (int i = index ; i < elementCount ; i++)
196                  if (elementData[i]==null)
197                      return i;
198          } else {
199              for (int i = index ; i < elementCount ; i++)
200 <                if (elem.equals(elementData[i]))
200 >                if (e.equals(elementData[i]))
201                      return i;
202          }
203          return -1;
204      }
205  
206      /**
207 <     * Returns the index of the last occurrence of the specified object in
207 <     * this list.
208 <     *
209 <     * @param   elem   the desired element.
210 <     * @return  the index of the last occurrence of the specified object in
211 <     *          this list; returns -1 if the object is not found.
207 >     * {@inheritDoc}
208       */
209 <    public int lastIndexOf(Object elem) {
209 >    public int lastIndexOf(Object o) {
210          E[] elementData = array();
211          int len = elementData.length;
212 <        return lastIndexOf(elem, elementData, len);
212 >        return lastIndexOf(o, elementData, len);
213      }
214  
215 <    private static int lastIndexOf(Object elem, Object[] elementData, int len) {
216 <        if (elem == null) {
215 >    private static int lastIndexOf(Object o, Object[] elementData, int len) {
216 >        if (o == null) {
217              for (int i = len-1; i >= 0; i--)
218                  if (elementData[i]==null)
219                      return i;
220          } else {
221              for (int i = len-1; i >= 0; i--)
222 <                if (elem.equals(elementData[i]))
222 >                if (o.equals(elementData[i]))
223                      return i;
224          }
225          return -1;
226      }
227  
228      /**
229 <     * Searches backwards for the specified object, starting from the
230 <     * specified index, and returns an index to it.
231 <     *
232 <     * @param  elem    the desired element.
233 <     * @param  index   the index to start searching from.
234 <     * @return the index of the last occurrence of the specified object in this
235 <     *          List at position less than index in the List;
236 <     *          -1 if the object is not found.
229 >     * Returns the index of the last occurrence of the specified element in
230 >     * this list, searching backwards from <tt>index</tt>, or returns -1 if
231 >     * the element is not found.
232 >     * More formally, returns the highest index <tt>i</tt> such that
233 >     * <tt>(i&nbsp;&lt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(e==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;e.equals(get(i))))</tt>,
234 >     * or -1 if there is no such index.
235 >     *
236 >     * @param e element to search for
237 >     * @param index index to start searching backwards from
238 >     * @return the index of the last occurrence of the element at position
239 >     *         less than or equal to <tt>index</tt> in this list;
240 >     *         -1 if the element is not found.
241 >     * @throws IndexOutOfBoundsException if the specified index is greater
242 >     *         than or equal to the current size of this list
243       */
244 <    public int lastIndexOf(E elem, int index) {
244 >    public int lastIndexOf(E e, int index) {
245          // needed in order to compile on 1.2b3
246          E[] elementData = array();
247 <        if (elem == null) {
247 >        if (e == null) {
248              for (int i = index; i >= 0; i--)
249                  if (elementData[i]==null)
250                      return i;
251          } else {
252              for (int i = index; i >= 0; i--)
253 <                if (elem.equals(elementData[i]))
253 >                if (e.equals(elementData[i]))
254                      return i;
255          }
256          return -1;
# Line 258 | Line 260 | public class CopyOnWriteArrayList<E>
260       * Returns a shallow copy of this list.  (The elements themselves
261       * are not copied.)
262       *
263 <     * @return  a clone of this list.
263 >     * @return a clone of this list
264       */
265      public Object clone() {
266          try {
# Line 275 | Line 277 | public class CopyOnWriteArrayList<E>
277  
278      /**
279       * Returns an array containing all of the elements in this list
280 <     * in the correct order.
281 <     * @return an array containing all of the elements in this list
282 <     *         in the correct order.
280 >     * in proper sequence (from first to last element).
281 >     *
282 >     * <p>The returned array will be "safe" in that no references to it are
283 >     * maintained by this list.  (In other words, this method must allocate
284 >     * a new array).  The caller is thus free to modify the returned array.
285 >     *
286 >     * <p>This method acts as bridge between array-based and collection-based
287 >     * APIs.
288 >     *
289 >     * @return an array containing all the elements in this list
290       */
291      public Object[] toArray() {
292          Object[] elementData = array();
# Line 287 | Line 296 | public class CopyOnWriteArrayList<E>
296      }
297  
298      /**
299 <     * Returns an array containing all of the elements in this list in the
300 <     * correct order.  The runtime type of the returned array is that of the
301 <     * specified array.  If the list fits in the specified array, it is
302 <     * returned therein.  Otherwise, a new array is allocated with the runtime
303 <     * type of the specified array and the size of this list.
299 >     * Returns an array containing all of the elements in this list in
300 >     * proper sequence (from first to last element); the runtime type of
301 >     * the returned array is that of the specified array.  If the list fits
302 >     * in the specified array, it is returned therein.  Otherwise, a new
303 >     * array is allocated with the runtime type of the specified array and
304 >     * the size of this list.
305       *
306       * <p>If this list fits in the specified array with room to spare
307 <     * (i.e., the array has more elements than this list), the element
308 <     * in the array immediately following the end of the list is set to
309 <     * <tt>null</tt>.  This is useful in determining the length of this
310 <     * list <i>only</i> if the caller knows that this collection does
311 <     * not contain any <tt>null</tt> elements.)
307 >     * (i.e., the array has more elements than this list), the element in
308 >     * the array immediately following the end of the list is set to
309 >     * <tt>null</tt>.  (This is useful in determining the length of this
310 >     * list <i>only</i> if the caller knows that this list does not contain
311 >     * any null elements.)
312 >     *
313 >     * <p>Like the {@link #toArray()} method, this method acts as bridge between
314 >     * array-based and collection-based APIs.  Further, this method allows
315 >     * precise control over the runtime type of the output array, and may,
316 >     * under certain circumstances, be used to save allocation costs.
317 >     *
318 >     * <p>Suppose <tt>x</tt> is a list known to contain only strings.
319 >     * The following code can be used to dump the list into a newly
320 >     * allocated array of <tt>String</tt>:
321 >     *
322 >     * <pre>
323 >     *     String[] y = x.toArray(new String[0]);</pre>
324 >     *
325 >     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
326 >     * <tt>toArray()</tt>.
327       *
328       * @param a the array into which the elements of the list are to
329 <     *            be stored, if it is big enough; otherwise, a new array of the
330 <     *            same runtime type is allocated for this purpose.
331 <     * @return an array containing the elements of the list.
332 <     * @throws ArrayStoreException if the runtime type of a is not a supertype
333 <     * of the runtime type of every element in this list.
329 >     *          be stored, if it is big enough; otherwise, a new array of the
330 >     *          same runtime type is allocated for this purpose.
331 >     * @return an array containing all the elements in this list
332 >     * @throws ArrayStoreException if the runtime type of the specified array
333 >     *         is not a supertype of the runtime type of every element in
334 >     *         this list
335 >     * @throws NullPointerException if the specified array is null
336       */
337      public <T> T[] toArray(T a[]) {
338          E[] elementData = array();
# Line 326 | Line 353 | public class CopyOnWriteArrayList<E>
353      // Positional Access Operations
354  
355      /**
356 <     * Returns the element at the specified position in this list.
356 >     * {@inheritDoc}
357       *
358 <     * @param  index index of element to return.
332 <     * @return the element at the specified position in this list.
333 <     * @throws IndexOutOfBoundsException if the index is out of range
334 <     *         (<tt>index &lt; 0 || index &gt; size()</tt>).
358 >     * @throws IndexOutOfBoundsException {@inheritDoc}
359       */
360      public E get(int index) {
361          E[] elementData = array();
# Line 340 | Line 364 | public class CopyOnWriteArrayList<E>
364      }
365  
366      /**
367 <     * Replaces the element at the specified position in this list with
368 <     * the specified element.
367 >     * Replaces the element at the specified position in this list with the
368 >     * specified element.
369       *
370 <     * @param index index of element to replace.
347 <     * @param element element to be stored at the specified position.
348 <     * @return the element previously at the specified position.
349 <     * @throws IndexOutOfBoundsException if the index is out of range
350 <     *         (<tt>index &lt; 0 || index &gt; size()</tt>).
370 >     * @throws IndexOutOfBoundsException {@inheritDoc}
371       */
372      public synchronized E set(int index, E element) {
373          int len = array.length;
# Line 368 | Line 388 | public class CopyOnWriteArrayList<E>
388      /**
389       * Appends the specified element to the end of this list.
390       *
391 <     * @param element element to be appended to this list.
392 <     * @return true (as per the general contract of <tt>Collection.add</tt>).
391 >     * @param element element to be appended to this list
392 >     * @return <tt>true</tt> (as per the spec for {@link Collection#add})
393       */
394      public synchronized boolean add(E element) {
395          int len = array.length;
# Line 385 | Line 405 | public class CopyOnWriteArrayList<E>
405       * list. Shifts the element currently at that position (if any) and
406       * any subsequent elements to the right (adds one to their indices).
407       *
408 <     * @param index index at which the specified element is to be inserted.
389 <     * @param element element to be inserted.
390 <     * @throws IndexOutOfBoundsException if the index is out of range
391 <     *         (<tt>index &lt; 0 || index &gt; size()</tt>).
408 >     * @throws IndexOutOfBoundsException {@inheritDoc}
409       */
410      public synchronized void add(int index, E element) {
411          int len = array.length;
# Line 405 | Line 422 | public class CopyOnWriteArrayList<E>
422      /**
423       * Removes the element at the specified position in this list.
424       * Shifts any subsequent elements to the left (subtracts one from their
425 <     * indices).
425 >     * indices).  Returns the element that was removed from the list.
426       *
427 <     * @param index the index of the element to removed.
411 <     * @return the element that was removed from the list.
412 <     * @throws IndexOutOfBoundsException if the index is out of range
413 <     *         (<tt>index &lt; 0 || index &gt; size()</tt>).
427 >     * @throws IndexOutOfBoundsException {@inheritDoc}
428       */
429      public synchronized E remove(int index) {
430          int len = array.length;
# Line 426 | Line 440 | public class CopyOnWriteArrayList<E>
440      }
441  
442      /**
443 <     * Removes a single instance of the specified element from this
444 <     * list, if it is present (optional operation).  More formally,
445 <     * removes an element <tt>e</tt> such that <tt>(o==null ? e==null :
446 <     * o.equals(e))</tt>, if the list contains one or more such
447 <     * elements.  Returns <tt>true</tt> if the list contained the
448 <     * specified element (or equivalently, if the list changed as a
449 <     * result of the call).<p>
443 >     * Removes the first occurrence of the specified element from this list,
444 >     * if it is present.  If this list does not contain the element, it is
445 >     * unchanged.  More formally, removes the element with the lowest index
446 >     * <tt>i</tt> such that
447 >     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>
448 >     * (if such an element exists).  Returns <tt>true</tt> if this list
449 >     * contained the specified element (or equivalently, if this list
450 >     * changed as a result of the call).
451       *
452 <     * @param o element to be removed from this list, if present.
453 <     * @return <tt>true</tt> if the list contained the specified element.
452 >     * @param o element to be removed from this list, if present
453 >     * @return <tt>true</tt> if this list contained the specified element
454       */
455      public synchronized boolean remove(Object o) {
456          int len = array.length;
# Line 467 | Line 482 | public class CopyOnWriteArrayList<E>
482              return false; // throw away copy
483      }
484  
470
485      /**
486 <     * Removes from this List all of the elements whose index is between
487 <     * fromIndex, inclusive and toIndex, exclusive.  Shifts any succeeding
488 <     * elements to the left (reduces their index).
486 >     * Removes from this list all of the elements whose index is between
487 >     * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
488 >     * Shifts any succeeding elements to the left (reduces their index).
489       * This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements.
490       * (If <tt>toIndex==fromIndex</tt>, this operation has no effect.)
491       *
492 <     * @param fromIndex index of first element to be removed.
493 <     * @param toIndex index after last element to be removed.
492 >     * @param fromIndex index of first element to be removed
493 >     * @param toIndex index after last element to be removed
494       * @throws IndexOutOfBoundsException if fromIndex or toIndex out of
495       *              range (fromIndex &lt; 0 || fromIndex &gt;= size() || toIndex
496 <     *              &gt; size() || toIndex &lt; fromIndex).
496 >     *              &gt; size() || toIndex &lt; fromIndex)
497       */
498      private synchronized void removeRange(int fromIndex, int toIndex) {
499          int len = array.length;
# Line 496 | Line 510 | public class CopyOnWriteArrayList<E>
510          array = newArray;
511      }
512  
499
513      /**
514       * Append the element if not present.
515 <     * @param element element to be added to this list, if absent.
515 >     * @param element element to be added to this list, if absent
516       * @return true if added
517       */
518      public synchronized boolean addIfAbsent(E element) {
# Line 520 | Line 533 | public class CopyOnWriteArrayList<E>
533      }
534  
535      /**
536 <     * Returns true if this list contains all of the elements in the
536 >     * Returns <tt>true</tt> if this list contains all of the elements of the
537       * specified collection.
538       *
539       * <p>This implementation iterates over the specified collection,
# Line 528 | Line 541 | public class CopyOnWriteArrayList<E>
541       * it's contained in this list.  If all elements are so contained
542       * <tt>true</tt> is returned, otherwise <tt>false</tt>.
543       *
544 <     * @param c the collection
545 <     * @return true if all elements are contained
544 >     * @param  c collection to be checked for containment in this list
545 >     * @return <tt>true</tt> if this list contains all of the elements of the
546 >     *         specified collection
547 >     * @throws NullPointerException if the specified collection is null
548       */
549      public boolean containsAll(Collection<?> c) {
550          E[] elementData = array();
# Line 542 | Line 557 | public class CopyOnWriteArrayList<E>
557          return true;
558      }
559  
545
560      /**
561       * Removes from this list all of its elements that are contained in
562       * the specified collection. This is a particularly expensive operation
563       * in this class because of the need for an internal temporary array.
564       *
565 <     * @param c the collection
566 <     * @return true if this list changed as a result of the call.
565 >     * @param c collection containing elements to be removed from this list
566 >     * @return <tt>true</tt> if this list changed as a result of the call
567 >     * @throws ClassCastException   {@inheritDoc}
568 >     * @throws NullPointerException {@inheritDoc}
569       */
570      public synchronized boolean removeAll(Collection<?> c) {
571          E[] elementData = array;
# Line 577 | Line 593 | public class CopyOnWriteArrayList<E>
593  
594      /**
595       * Retains only the elements in this list that are contained in the
596 <     * specified collection (optional operation).  In other words, removes
597 <     * from this list all of its elements that are not contained in the
582 <     * specified collection.
596 >     * specified collection.  In other words, removes from this list all of
597 >     * its elements that are not contained in the specified collection.
598       *
599 <     * @param c the collection
600 <     * @return true if this list changed as a result of the call.
599 >     * @param c collection containing elements to be retained in this list
600 >     * @return <tt>true</tt> if this list changed as a result of the call
601 >     * @throws ClassCastException   {@inheritDoc}
602 >     * @throws NullPointerException {@inheritDoc}
603       */
604      public synchronized boolean retainAll(Collection<?> c) {
605          E[] elementData = array;
# Line 612 | Line 629 | public class CopyOnWriteArrayList<E>
629       * this list, in the order that they are returned by the
630       * specified collection's iterator.
631       *
632 <     * @param c elements to be added to this list.
632 >     * @param c elements to be added to this list
633       * @return the number of elements added
634 +     * @throws NullPointerException if the specified collection is null
635       */
636      public synchronized int addAllAbsent(Collection<? extends E> c) {
637          int numNew = c.size();
# Line 656 | Line 674 | public class CopyOnWriteArrayList<E>
674       * of this list, in the order that they are returned by the specified
675       * collection's iterator.
676       *
677 <     * @param c elements to be inserted into this list.
677 >     * @param c elements to be inserted into this list
678       * @return true if any elements are added
679 +     * @throws NullPointerException if the specified collection is null
680       */
681      public synchronized boolean addAll(Collection<? extends E> c) {
682          int numNew = c.size();
# Line 675 | Line 694 | public class CopyOnWriteArrayList<E>
694      }
695  
696      /**
697 <     * Inserts all of the elements in the specified Collection into this
697 >     * Inserts all of the elements in the specified collection into this
698       * list, starting at the specified position.  Shifts the element
699       * currently at that position (if any) and any subsequent elements to
700       * the right (increases their indices).  The new elements will appear
701       * in the list in the order that they are returned by the
702       * specified Collection's iterator.
703       *
704 <     * @param index index at which to insert first element
705 <     *                from the specified collection.
706 <     * @param c elements to be inserted into this list.
707 <     * @throws IndexOutOfBoundsException if the index is out of range
708 <     *         (<tt>index &lt; 0 || index &gt; size()</tt>).
709 <     * @return true if any elements are added
704 >     * @param index index at which to insert the first element
705 >     *        from the specified collection
706 >     * @param c collection containing elements to be inserted into this list
707 >     * @return <tt>true</tt> if this list changed as a result of the call
708 >     * @throws IndexOutOfBoundsException {@inheritDoc}
709 >     * @throws NullPointerException if the specified collection is null
710       */
711      public synchronized boolean addAll(int index, Collection<? extends E> c) {
712          int len = array.length;
# Line 780 | Line 799 | public class CopyOnWriteArrayList<E>
799          return buf.toString();
800      }
801  
783
802      /**
803 <     * Compares the specified Object with this List for equality.  Returns true
804 <     * if and only if the specified Object is also a List, both Lists have the
805 <     * same size, and all corresponding pairs of elements in the two Lists are
806 <     * <em>equal</em>.  (Two elements <tt>e1</tt> and <tt>e2</tt> are
807 <     * <em>equal</em> if <tt>(e1==null ? e2==null : e1.equals(e2))</tt>.)
808 <     * In other words, two Lists are defined to be equal if they contain the
809 <     * same elements in the same order.
810 <     * <p>
793 <     * This implementation first checks if the specified object is this
794 <     * List. If so, it returns true; if not, it checks if the specified
795 <     * object is a List. If not, it returns false; if so, it iterates over
796 <     * both lists, comparing corresponding pairs of elements.  If any
797 <     * comparison returns false, this method returns false.  If either
798 <     * Iterator runs out of elements before the other it returns false
799 <     * (as the Lists are of unequal length); otherwise it returns true when
800 <     * the iterations complete.
803 >     * Compares the specified object with this list for equality.
804 >     * Returns true if and only if the specified object is also a {@link
805 >     * List}, both lists have the same size, and all corresponding pairs
806 >     * of elements in the two lists are <em>equal</em>.  (Two elements
807 >     * <tt>e1</tt> and <tt>e2</tt> are <em>equal</em> if <tt>(e1==null ?
808 >     * e2==null : e1.equals(e2))</tt>.)  In other words, two lists are
809 >     * defined to be equal if they contain the same elements in the same
810 >     * order.
811       *
812 <     * @param o the Object to be compared for equality with this List.
813 <     * @return true if the specified Object is equal to this List.
812 >     * @param o the object to be compared for equality with this list
813 >     * @return <tt>true</tt> if the specified object is equal to this list
814       */
815      public boolean equals(Object o) {
816          if (o == this)
# Line 814 | Line 824 | public class CopyOnWriteArrayList<E>
824  
825          ListIterator<E> e1 = listIterator();
826          ListIterator<E> e2 = l2.listIterator();
827 <        while(e1.hasNext()) {
827 >        while (e1.hasNext()) {
828              E o1 = e1.next();
829              E o2 = e2.next();
830              if (!(o1==null ? o2==null : o1.equals(o2)))
# Line 824 | Line 834 | public class CopyOnWriteArrayList<E>
834      }
835  
836      /**
837 <     * Returns the hash code value for this List.
837 >     * Returns the hash code value for this list.
838       *
839       * <p> This implementation uses the definition in {@link
840       * List#hashCode}.
# Line 841 | Line 851 | public class CopyOnWriteArrayList<E>
851      }
852  
853      /**
854 <     * Returns an Iterator over the elements contained in this list.
855 <     * The iterator provides a snapshot of the state of the list
856 <     * when the iterator was constructed. No synchronization is
857 <     * needed while traversing the iterator. The iterator does
858 <     * <em>NOT</em> support the <tt>remove</tt> method.
859 <     * @return the iterator
854 >     * Returns an iterator over the elements in this list in proper sequence.
855 >     *
856 >     * <p>The returned iterator provides a snapshot of the state of the list
857 >     * when the iterator was constructed. No synchronization is needed while
858 >     * traversing the iterator. The iterator does <em>NOT</em> support the
859 >     * <tt>remove</tt> method.
860 >     *
861 >     * @return an iterator over the elements in this list in proper sequence
862       */
863      public Iterator<E> iterator() {
864          return new COWIterator<E>(array(), 0);
865      }
866  
867      /**
868 <     * Returns an Iterator of the elements in this List (in proper sequence).
857 <     * The iterator provides a snapshot of the state of the list
858 <     * when the iterator was constructed. No synchronization is
859 <     * needed while traversing the iterator. The iterator does
860 <     * <em>NOT</em> support the <tt>remove</tt>, <tt>set</tt>,
861 <     * or <tt>add</tt> methods.
862 <     * @return the iterator
868 >     * {@inheritDoc}
869       *
870 +     * <p>The returned iterator provides a snapshot of the state of the list
871 +     * when the iterator was constructed. No synchronization is needed while
872 +     * traversing the iterator. The iterator does <em>NOT</em> support the
873 +     * <tt>remove</tt>, <tt>set</tt> or <tt>add</tt> methods.
874       */
875      public ListIterator<E> listIterator() {
876          return new COWIterator<E>(array(), 0);
877      }
878  
879      /**
880 <     * Returns a ListIterator of the elements in this List (in proper
881 <     * sequence), starting at the specified position in the List.  The
882 <     * specified index indicates the first element that would be returned by
883 <     * an initial call to nextElement.  An initial call to previousElement
884 <     * would return the element with the specified index minus one.
885 <     * The ListIterator returned by this implementation will throw
886 <     * an UnsupportedOperationException in its remove, set and
877 <     * add methods.
878 <     *
879 <     * @param index index of first element to be returned from the
880 <     *                ListIterator (by a call to getNext).
881 <     * @return the iterator
882 <     * @throws IndexOutOfBoundsException if the index is out of range
883 <     *         (<tt>index &lt; 0 || index &gt; size()</tt>).
880 >     * {@inheritDoc}
881 >     *
882 >     * <p>The list iterator returned by this implementation will throw an
883 >     * <tt>UnsupportedOperationException</tt> in its <tt>remove</tt>,
884 >     * <tt>set</tt> and <tt>add</tt> methods.
885 >     *
886 >     * @throws IndexOutOfBoundsException {@inheritDoc}
887       */
888      public ListIterator<E> listIterator(final int index) {
889          E[] elementData = array();
# Line 966 | Line 969 | public class CopyOnWriteArrayList<E>
969          }
970      }
971  
969
972      /**
973 <     * Returns a view of the portion of this List between fromIndex,
974 <     * inclusive, and toIndex, exclusive.  The returned List is backed by this
975 <     * List, so changes in the returned List are reflected in this List, and
976 <     * vice-versa.  While mutative operations are supported, they are
977 <     * probably not very useful for CopyOnWriteArrayLists.
978 <     * <p>
979 <     * The semantics of the List returned by this method become undefined if
980 <     * the backing list (i.e., this List) is <i>structurally modified</i> in
981 <     * any way other than via the returned List.  (Structural modifications are
982 <     * those that change the size of the List, or otherwise perturb it in such
973 >     * Returns a view of the portion of this list between <tt>fromIndex</tt>,
974 >     * inclusive, and <tt>toIndex</tt>, exclusive.  The returned list is
975 >     * backed by this list, so changes in the returned list are reflected in
976 >     * this list, and vice-versa.  While mutative operations are supported,
977 >     * they are probably not very useful for CopyOnWriteArrayLists.
978 >     *
979 >     * <p>The semantics of the list returned by this method become undefined if
980 >     * the backing list (i.e., this list) is <i>structurally modified</i> in
981 >     * any way other than via the returned list.  (Structural modifications are
982 >     * those that change the size of the list, or otherwise perturb it in such
983       * a fashion that iterations in progress may yield incorrect results.)
984       *
985 <     * @param fromIndex low endpoint (inclusive) of the subList.
986 <     * @param toIndex high endpoint (exclusive) of the subList.
987 <     * @return a view of the specified range within this List.
988 <     * @throws IndexOutOfBoundsException if
987 <     *     (fromIndex &lt; 0 || toIndex &gt; size || fromIndex &gt; toIndex).
985 >     * @param fromIndex low endpoint (inclusive) of the subList
986 >     * @param toIndex high endpoint (exclusive) of the subList
987 >     * @return a view of the specified range within this list
988 >     * @throws IndexOutOfBoundsException {@inheritDoc}
989       */
990      public synchronized List<E> subList(int fromIndex, int toIndex) {
991          // synchronized since sublist constructor depends on it.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines