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.14 by dl, Sun Aug 31 13:33:13 2003 UTC vs.
Revision 1.15 by dl, Sat Sep 13 18:51:11 2003 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group.  Adapted and released under explicit permission
4 < * from JDK1.2 ArrayList.java which carries the following copyright:
4 > * from JDK ArrayList.java which carries the following copyright:
5   *
6   * Copyright 1997 by Sun Microsystems, Inc.,
7   * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
# Line 26 | Line 26 | import java.util.*;
26   * than alternatives when traversal operations vastly outnumber
27   * mutations, and is useful when you cannot or don't want to
28   * synchronize traversals, yet need to preclude interference among
29 < * concurrent threads.  The iterator method uses a reference to the
30 < * state of the array at the point that the iterator was created. This
31 < * array never changes during the lifetime of the iterator, so
32 < * interference is impossible and the iterator is guaranteed not to
33 < * throw <tt>ConcurrentModificationException</tt>.  The iterator will
34 < * not reflect additions, removals, or changes to the list since the
35 < * iterator was created.  Element-changing operations on iterators
36 < * themselves (remove, set, and add) are not supported. These methods
37 < * throw <tt>UnsupportedOperationException</tt>.
29 > * concurrent threads.  The "snapshot" style iterator method uses a
30 > * reference to the state of the array at the point that the iterator
31 > * was created. This array never changes during the lifetime of the
32 > * iterator, so interference is impossible and the iterator is
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
38 > * <tt>UnsupportedOperationException</tt>.
39   * @since 1.5
40   * @author Doug Lea
41   */
# Line 55 | Line 56 | public class CopyOnWriteArrayList<E>
56      private E[] array() { return array; }
57  
58      /**
59 <     * Constructs an empty list.
59 >     * Creates an empty list.
60       */
61      public CopyOnWriteArrayList() {
62          array = (E[]) new Object[0];
63      }
64  
65      /**
66 <     * Constructs an list containing the elements of the specified
66 >     * Creates a list containing the elements of the specified
67       * Collection, in the order they are returned by the Collection's
68       * iterator.
69       * @param c the collection of initially held elements
# Line 104 | Line 105 | public class CopyOnWriteArrayList<E>
105      }
106  
107      /**
108 <     * Returns the number of components in this list.
108 >     * Returns the number of elements in this list.
109       *
110 <     * @return  the number of components in this list.
110 >     * @return  the number of elements in this list.
111       */
112      public int size() {
113          return array().length;
114      }
115  
116      /**
117 <     * Tests if this list has no components.
117 >     * Tests if this list has no elements.
118       *
119 <     * @return  <tt>true</tt> if this list has no components;
119 >     * @return  <tt>true</tt> if this list has no elements;
120       *          <tt>false</tt> otherwise.
121       */
122      public boolean isEmpty() {
# Line 123 | Line 124 | public class CopyOnWriteArrayList<E>
124      }
125  
126      /**
127 <     * Returns true if this list contains the specified element.
127 >     * Returns <tt>true</tt> if this list contains the specified element.
128       *
129       * @param elem element whose presence in this List is to be tested.
130 +     * @return  <code>true</code> if the specified element is present;
131 +     *          <code>false</code> otherwise.
132       */
133      public boolean contains(Object elem) {
134          E[] elementData = array();
# Line 134 | Line 137 | public class CopyOnWriteArrayList<E>
137      }
138  
139      /**
140 <     * Searches for the first occurence of the given argument, testing
141 <     * for equality using the <tt>equals</tt> method.
140 >     * Searches for the first occurence of the given argument, testing
141 >     * for equality using the <tt>equals</tt> method.
142       *
143       * @param   elem   an object.
144       * @return  the index of the first occurrence of the argument in this
# Line 148 | Line 151 | public class CopyOnWriteArrayList<E>
151          return indexOf(elem, elementData, len);
152      }
153  
151
154      /**
155       * static version allows repeated call without needed
156       * to grab lock for array each time
# Line 198 | Line 200 | public class CopyOnWriteArrayList<E>
200       * Returns the index of the last occurrence of the specified object in
201       * this list.
202       *
203 <     * @param   elem   the desired component.
203 >     * @param   elem   the desired element.
204       * @return  the index of the last occurrence of the specified object in
205       *          this list; returns -1 if the object is not found.
206       */
# Line 225 | Line 227 | public class CopyOnWriteArrayList<E>
227       * Searches backwards for the specified object, starting from the
228       * specified index, and returns an index to it.
229       *
230 <     * @param  elem    the desired component.
230 >     * @param  elem    the desired element.
231       * @param  index   the index to start searching from.
232       * @return the index of the last occurrence of the specified object in this
233       *          List at position less than index in the List;
# Line 268 | Line 270 | public class CopyOnWriteArrayList<E>
270      /**
271       * Returns an array containing all of the elements in this list
272       * in the correct order.
273 +     * @return an array containing all of the elements in this list
274 +     *         in the correct order.
275       */
276      public Object[] toArray() {
277          Object[] elementData = array();
# Line 318 | Line 322 | public class CopyOnWriteArrayList<E>
322      /**
323       * Returns the element at the specified position in this list.
324       *
325 <     * @param index index of element to return.
326 <     * @return the element
327 <     * @throws IndexOutOfBoundsException index is out of range (index
328 <     *              &lt; 0 || index &gt;= size()).
325 >     * @param  index index of element to return.
326 >     * @return the element at the specified position in this list.
327 >     * @throws    IndexOutOfBoundsException if index is out of range <tt>(index
328 >     *            &lt; 0 || index &gt;= size())</tt>.
329       */
330      public E get(int index) {
331          E[] elementData = array();
# Line 336 | Line 340 | public class CopyOnWriteArrayList<E>
340       * @param index index of element to replace.
341       * @param element element to be stored at the specified position.
342       * @return the element previously at the specified position.
343 <     * @throws IndexOutOfBoundsException index out of range
344 <     *              (index &lt; 0 || index &gt;= size()).
343 >     * @throws    IndexOutOfBoundsException if index out of range
344 >     *            <tt>(index &lt; 0 || index &gt;= size())</tt>.
345       */
346      public synchronized E set(int index, E element) {
347          int len = array.length;
# Line 377 | Line 381 | public class CopyOnWriteArrayList<E>
381       *
382       * @param index index at which the specified element is to be inserted.
383       * @param element element to be inserted.
384 <     * @throws IndexOutOfBoundsException index is out of range
385 <     *              (index &lt; 0 || index &gt; size()).
384 >     * @throws    IndexOutOfBoundsException if index is out of range
385 >     *            <tt>(index &lt; 0 || index &gt; size())</tt>.
386       */
387      public synchronized void add(int index, E element) {
388          int len = array.length;
# Line 395 | Line 399 | public class CopyOnWriteArrayList<E>
399      /**
400       * Removes the element at the specified position in this list.
401       * Shifts any subsequent elements to the left (subtracts one from their
402 <     * indices).  Returns the element that was removed from the list.
402 >     * indices).
403       *
400     * @throws IndexOutOfBoundsException index out of range (index
401     *              &lt; 0 || index &gt;= size()).
404       * @param index the index of the element to removed.
405 +     * @return the element that was removed from the list.
406 +     * @throws    IndexOutOfBoundsException if index out of range <tt>(index
407 +     *            &lt; 0 || index &gt;= size())</tt>.
408       */
409      public synchronized E remove(int index) {
410          int len = array.length;
# Line 415 | Line 420 | public class CopyOnWriteArrayList<E>
420      }
421  
422      /**
423 <     * Removes a single instance of the specified element from this Collection,
424 <     * if it is present (optional operation).  More formally, removes an
425 <     * element <tt>e</tt> such that <tt>(o==null ? e==null :
426 <     * o.equals(e))</tt>, if the Collection contains one or more such
427 <     * elements.  Returns true if the Collection contained the specified
428 <     * element (or equivalently, if the Collection changed as a result of the
429 <     * call).
423 >     * Removes a single instance of the specified element from this
424 >     * list, if it is present (optional operation).  More formally,
425 >     * removes an element <tt>e</tt> such that <tt>(o==null ? e==null :
426 >     * o.equals(e))</tt>, if the list contains one or more such
427 >     * elements.  Returns <tt>true</tt> if the list contained the
428 >     * specified element (or equivalently, if the list changed as a
429 >     * result of the call).<p>
430       *
431 <     * @param element element to be removed from this Collection, if present.
432 <     * @return true if the Collection changed as a result of the call.
431 >     * @param o element to be removed from this list, if present.
432 >     * @return <tt>true</tt> if the list contained the specified element.
433       */
434 <    public synchronized boolean remove(Object element) {
434 >    public synchronized boolean remove(Object o) {
435          int len = array.length;
436          if (len == 0) return false;
437  
# Line 437 | Line 442 | public class CopyOnWriteArrayList<E>
442          E[] newArray = (E[]) new Object[newlen];
443  
444          for (int i = 0; i < newlen; ++i) {
445 <            if (element == array[i] ||
446 <            (element != null && element.equals(array[i]))) {
445 >            if (o == array[i] ||
446 >            (o != null && o.equals(array[i]))) {
447                  // found one;  copy remaining and exit
448                  for (int k = i + 1; k < len; ++k) newArray[k-1] = array[k];
449                  array = newArray;
# Line 448 | Line 453 | public class CopyOnWriteArrayList<E>
453          }
454          // special handling for last cell
455  
456 <        if (element == array[newlen] ||
457 <        (element != null && element.equals(array[newlen]))) {
456 >        if (o == array[newlen] ||
457 >        (o != null && o.equals(array[newlen]))) {
458              array = newArray;
459              return true;
460          } else
461              return false; // throw away copy
457
462      }
463  
464  
# Line 462 | Line 466 | public class CopyOnWriteArrayList<E>
466       * Removes from this List all of the elements whose index is between
467       * fromIndex, inclusive and toIndex, exclusive.  Shifts any succeeding
468       * elements to the left (reduces their index).
469 <     * This call shortens the List by (toIndex - fromIndex) elements.  (If
470 <     * toIndex==fromIndex, this operation has no effect.)
469 >     * This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements.
470 >     * (If <tt>toIndex==fromIndex</tt>, this operation has no effect.)
471       *
472       * @param fromIndex index of first element to be removed.
473       * @param toIndex index after last element to be removed.
# Line 813 | Line 817 | public class CopyOnWriteArrayList<E>
817      }
818  
819      /**
820 <     * Returns the hash code value for this List.
821 <     * <p>
822 <     * This implementation uses exactly the code that is used to define
819 <     * the List hash function in the documentation for List.hashCode.
820 >     * Returns the hash code value for this List.  <p> This
821 >     * implementation uses the definition in {@link List#hashCode}.
822 >     * @return the hash code
823       */
824      public int hashCode() {
825          int hashCode = 1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines