/* * @(#)CopyOnWriteArrayList.java 1.8 04/04/14 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util.concurrent; import java.util.*; /** * A thread-safe 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 may be more efficient * than alternatives when traversal operations vastly outnumber * mutations, and is useful when you cannot or don't want to * synchronize traversals, yet need to preclude interference among * concurrent threads. The "snapshot" style 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. Element-changing * operations on iterators themselves (remove, set, and add) are not * supported. These methods throw * UnsupportedOperationException. * *
This class is a member of the
*
* Java Collections Framework.
*
* @since 1.5
* @author Doug Lea
* @param
* 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
*
* @param o element to be removed from this list, if present.
* @return true if the list contained the specified element.
*/
public synchronized boolean remove(Object o) {
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 (o == array[i] ||
(o != null && o.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 (o == array[newlen] ||
(o != null && o.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.
* @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.
*
* 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.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 extends E> e = c.iterator();
while (e.hasNext()) {
E element = 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 extends E> 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 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 the definition in {@link
* List#hashCode}.
* @return the hash code
*/
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 Listtrue
if the specified element is present;
* false
otherwise.
*/
public boolean contains(Object elem) {
E[] elementData = array();
int len = elementData.length;
return indexOf(elem, elementData, len) >= 0;
}
/**
* Searches for the first occurrence 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 occurrence 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 element.
* @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 element.
* @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();
CopyOnWriteArrayList