/* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group. Adapted and released under explicit permission * from JDK1.2 ArrayList.java which carries the following copyright: * * Copyright 1997 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ package java.util.concurrent; import java.util.*; /** * A variant of {@link java.util.ArrayList} in which all mutative * operations (add, set, and so on) are implemented by making a fresh * copy of the underlying array.
* * This is ordinarily too costly, but it becomes attractive when * traversal operations vastly outnumber mutations, and, especially * when you cannot or don't want to synchronize traversals, yet need * to preclude interference among concurrent threads. The iterator * method uses a reference to the state of the array at the point that * the iterator was created. This array never changes during the * lifetime of the iterator, so interference is impossible and the * iterator is guaranteed not to throw * ConcurrentModificationException. The iterator will not * reflect additions, removals, or changes to the List since the * iterator was created.
* * Because of the copy-on-write policy, some one-by-one mutative * operations in the java.util.Arrays and java.util.Collections * classes are so time/space intensive as to never be worth calling. * Also, due to their strict read-only nature, element-changing * operations on iterators (remove, set, and add) are not * supported. These are the only methods throwing * UnsupportedOperationException.
* @since 1.5
* @author Doug Lea
*/
public class CopyOnWriteArrayList
* If the list fits in the specified array with room to spare
* (i.e., the array has more elements than the list),
* the element in the array immediately following the end of the
* collection is set to null. This is useful in determining the length
* of the list only if the caller knows that the list
* does not contain any null elements.
*
* @param a the array into which the elements of the list are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose.
* @return an array containing the elements of the list.
* @throws ArrayStoreException the runtime type of a is not a supertype
* of the runtime type of every element in this list.
*/
public
* This implementation iterates over the specified Collection, checking
* each element returned by the Iterator in turn to see if it's
* contained in this Collection. If all elements are so contained
* true is returned, otherwise false.
* @param c the collection
* @return true if all elements are contained
*/
public boolean containsAll(Collection> c) {
E[] elementData = array();
int len = elementData.length;
Iterator e = c.iterator();
while (e.hasNext())
if (indexOf((E) e.next(), elementData, len) < 0)
return false;
return true;
}
/**
* Removes from this Collection all of its elements that are contained in
* the specified Collection. This is a particularly expensive operation
* in this class because of the need for an internal temporary array.
*
*
* @param c the collection
* @return true if this Collection changed as a result of the call.
*/
public synchronized boolean removeAll(Collection> c) {
E[] elementData = array_;
int len = elementData.length;
if (len == 0) return false;
// temp array holds those elements we know we want to keep
E[] temp = (E[]) new Object[len];
int newlen = 0;
for (int i = 0; i < len; ++i) {
E element = elementData[i];
if (!c.contains(element)) {
temp[newlen++] = element;
}
}
if (newlen == len) return false;
// copy temp as new array
E[] newArray = (E[]) new Object[newlen];
System.arraycopy(temp, 0, newArray, 0, newlen);
array_ = newArray;
return true;
}
/**
* Retains only the elements in this Collection that are contained in the
* specified Collection (optional operation). In other words, removes from
* this Collection all of its elements that are not contained in the
* specified Collection.
* @param c the collection
* @return true if this Collection changed as a result of the call.
*/
public synchronized boolean retainAll(Collection> c) {
E[] elementData = array_;
int len = elementData.length;
if (len == 0) return false;
E[] temp = (E[]) new Object[len];
int newlen = 0;
for (int i = 0; i < len; ++i) {
E element = elementData[i];
if (c.contains(element)) {
temp[newlen++] = element;
}
}
if (newlen == len) return false;
E[] newArray = (E[]) new Object[newlen];
System.arraycopy(temp, 0, newArray, 0, newlen);
array_ = newArray;
return true;
}
/**
* Appends all of the elements in the specified Collection that
* are not already contained in this list, to the end of
* this list, in the order that they are returned by the
* specified Collection's Iterator.
*
* @param c elements to be added into this list.
* @return the number of elements added
*/
public synchronized int addAllAbsent(Collection extends E> c) {
int numNew = c.size();
if (numNew == 0) return 0;
E[] elementData = array_;
int len = elementData.length;
E[] temp = (E[]) new Object[numNew];
int added = 0;
Iterator e = c.iterator();
while (e.hasNext()) {
E element = (E) e.next();
if (indexOf(element, elementData, len) < 0) {
if (indexOf(element, temp, added) < 0) {
temp[added++] = element;
}
}
}
if (added == 0) return 0;
E[] newArray = (E[]) new Object[len+added];
System.arraycopy(elementData, 0, newArray, 0, len);
System.arraycopy(temp, 0, newArray, len, added);
array_ = newArray;
return added;
}
/**
* Removes all of the elements from this list.
*
*/
public synchronized void clear() {
array_ = (E[]) new Object[0];
}
/**
* Appends all of the elements in the specified Collection to the end of
* this list, in the order that they are returned by the
* specified Collection's Iterator.
*
* @param c elements to be inserted into this list.
* @return true if any elements are added
*/
public synchronized boolean addAll(Collection extends E> c) {
int numNew = c.size();
if (numNew == 0) return false;
int len = array_.length;
E[] newArray = (E[]) new Object[len+numNew];
System.arraycopy(array_, 0, newArray, 0, len);
Iterator e = c.iterator();
for (int i=0; i
* This implementation first checks if the specified object is this
* List. If so, it returns true; if not, it checks if the specified
* object is a List. If not, it returns false; if so, it iterates over
* both lists, comparing corresponding pairs of elements. If any
* comparison returns false, this method returns false. If either
* Iterator runs out of elements before before the other it returns false
* (as the Lists are of unequal length); otherwise it returns true when
* the iterations complete.
*
* @param o the Object to be compared for equality with this List.
* @return true if the specified Object is equal to this List.
*/
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof List))
return false;
List
* This implementation uses exactly the code that is used to define
* the List hash function in the documentation for List.hashCode.
*/
public int hashCode() {
int hashCode = 1;
Iterator
* The semantics of the List returned by this method become undefined if
* the backing list (i.e., this List) is structurally modified in
* any way other than via the returned List. (Structural modifications are
* those that change the size of the List, or otherwise perturb it in such
* a fashion that iterations in progress may yield incorrect results.)
*
* @param fromIndex low endpoint (inclusive) of the subList.
* @param toIndex high endpoint (exclusive) of the subList.
* @return a view of the specified range within this List.
* @throws IndexOutOfBoundsException Illegal endpoint index value
* (fromIndex < 0 || toIndex > size || fromIndex > toIndex).
*/
public synchronized Listn
* elements of the provided array, starting at position first
.
* To copy an entire array, call with arguments (array, 0, array.length).
* @param toCopyIn the array. A copy of the indicated elements of
* this array is used as the
* internal array.
* @param first The index of first position of the array to
* start copying from.
* @param n the number of elements to copy. This will be the new size of
* the list.
**/
private synchronized void copyIn(E[] toCopyIn, int first, int n) {
array_ = (E[]) new Object[n];
System.arraycopy(toCopyIn, first, array_, 0, n);
}
/**
* Returns the number of components in this list.
*
* @return the number of components in this list.
*/
public int size() {
return array().length;
}
/**
* Tests if this list has no components.
*
* @return true
if this list has no components;
* false
otherwise.
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* Returns true if this list contains the specified element.
*
* @param elem element whose presence in this List is to be tested.
*/
public boolean contains(Object elem) {
E[] elementData = array();
int len = elementData.length;
return indexOf(elem, elementData, len) >= 0;
}
/**
* Searches for the first occurence of the given argument, testing
* for equality using the equals
method.
*
* @param elem an object.
* @return the index of the first occurrence of the argument in this
* list; returns -1
if the object is not found.
* @see Object#equals(Object)
*/
public int indexOf(Object elem) {
E[] elementData = array();
int len = elementData.length;
return indexOf(elem, elementData, len);
}
/**
* static version allows repeated call without needed
* to grab lock for array each time
**/
private static int indexOf(Object elem, Object[] elementData, int len) {
if (elem == null) {
for (int i = 0; i < len; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < len; i++)
if (elem.equals(elementData[i]))
return i;
}
return -1;
}
/**
* Searches for the first occurence of the given argument, beginning
* the search at index
, and testing for equality using
* the equals
method.
*
* @param elem an object.
* @param index the index to start searching from.
* @return the index of the first occurrence of the object argument in
* this List at position index
or later in the
* List; returns -1
if the object is not found.
* @see Object#equals(Object)
*/
public int indexOf(E elem, int index) {
E[] elementData = array();
int elementCount = elementData.length;
if (elem == null) {
for (int i = index ; i < elementCount ; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = index ; i < elementCount ; i++)
if (elem.equals(elementData[i]))
return i;
}
return -1;
}
/**
* Returns the index of the last occurrence of the specified object in
* this list.
*
* @param elem the desired component.
* @return the index of the last occurrence of the specified object in
* this list; returns -1 if the object is not found.
*/
public int lastIndexOf(Object elem) {
E[] elementData = array();
int len = elementData.length;
return lastIndexOf(elem, elementData, len);
}
private static int lastIndexOf(Object elem, Object[] elementData, int len) {
if (elem == null) {
for (int i = len-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = len-1; i >= 0; i--)
if (elem.equals(elementData[i]))
return i;
}
return -1;
}
/**
* Searches backwards for the specified object, starting from the
* specified index, and returns an index to it.
*
* @param elem the desired component.
* @param index the index to start searching from.
* @return the index of the last occurrence of the specified object in this
* List at position less than index in the List;
* -1 if the object is not found.
*/
public int lastIndexOf(E elem, int index) {
// needed in order to compile on 1.2b3
E[] elementData = array();
if (elem == null) {
for (int i = index; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = index; i >= 0; i--)
if (elem.equals(elementData[i]))
return i;
}
return -1;
}
/**
* Returns a shallow copy of this list. (The elements themselves
* are not copied.)
*
* @return a clone of this list.
*/
public Object clone() {
try {
E[] elementData = array();
CopyOnWriteArrayListe
such that (o==null ? e==null :
* o.equals(e))
, if the Collection contains one or more such
* elements. Returns true if the Collection contained the specified
* element (or equivalently, if the Collection changed as a result of the
* call).
*
* @param element element to be removed from this Collection, if present.
* @return true if the Collection changed as a result of the call.
*/
public synchronized boolean remove(Object element) {
int len = array_.length;
if (len == 0) return false;
// Copy while searching for element to remove
// This wins in the normal case of element being present
int newlen = len-1;
E[] newArray = (E[]) new Object[newlen];
for (int i = 0; i < newlen; ++i) {
if (element == array_[i] ||
(element != null && element.equals(array_[i]))) {
// found one; copy remaining and exit
for (int k = i + 1; k < len; ++k) newArray[k-1] = array_[k];
array_ = newArray;
return true;
} else
newArray[i] = array_[i];
}
// special handling for last cell
if (element == array_[newlen] ||
(element != null && element.equals(array_[newlen]))) {
array_ = newArray;
return true;
} else
return false; // throw away copy
}
/**
* Removes from this List all of the elements whose index is between
* fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding
* elements to the left (reduces their index).
* This call shortens the List by (toIndex - fromIndex) elements. (If
* toIndex==fromIndex, this operation has no effect.)
*
* @param fromIndex index of first element to be removed.
* @param toIndex index after last element to be removed.
* @throws IndexOutOfBoundsException fromIndex or toIndex out of
* range (fromIndex < 0 || fromIndex >= size() || toIndex
* > size() || toIndex < fromIndex).
*/
private synchronized void removeRange(int fromIndex, int toIndex) {
int len = array_.length;
if (fromIndex < 0 || fromIndex >= len ||
toIndex > len || toIndex < fromIndex)
throw new IndexOutOfBoundsException();
int numMoved = len - toIndex;
int newlen = len - (toIndex-fromIndex);
E[] newArray = (E[]) new Object[newlen];
System.arraycopy(array_, 0, newArray, 0, fromIndex);
System.arraycopy(array_, toIndex, newArray, fromIndex, numMoved);
array_ = newArray;
}
/**
* Append the element if not present.
* This operation can be used to obtain Set semantics
* for lists.
* @param element element to be added to this Collection, if absent.
* @return true if added
**/
public synchronized boolean addIfAbsent(E element) {
// Copy while checking if already present.
// This wins in the most common case where it is not present
int len = array_.length;
E[] newArray = (E[]) new Object[len + 1];
for (int i = 0; i < len; ++i) {
if (element == array_[i] ||
(element != null && element.equals(array_[i])))
return false; // exit, throwing away copy
else
newArray[i] = array_[i];
}
newArray[len] = element;
array_ = newArray;
return true;
}
/**
* Returns true if this Collection contains all of the elements in the
* specified Collection.
* e1
and e2
are
* equal if (e1==null ? e2==null : e1.equals(e2))
.)
* In other words, two Lists are defined to be equal if they contain the
* same elements in the same order.
* remove
method.
*/
public Iteratorremove
, set
,
* or add
methods.
*
*/
public ListIterator