Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
ArrayList
org.apache.commons.collections.FastArrayList
public class FastArrayList
extends ArrayList
java.util.ArrayList
designed
to operate in a multithreaded environment where the large majority of
method calls are read-only, instead of structural changes. When operating
in "fast" mode, read calls are non-synchronized and write calls perform the
following steps:
setFast(true)
) after initialization
is complete.
NOTE: If you are creating and accessing an
ArrayList
only within a single thread, you should use
java.util.ArrayList
directly (with no synchronization), for
maximum performance.
NOTE: This class is not cross-platform.
Using it may cause unexpected failures on some architectures.
It suffers from the same problems as the double-checked locking idiom.
In particular, the instruction that clones the internal collection and the
instruction that sets the internal reference to the clone can be executed
or perceived out-of-order. This means that any read operation might fail
unexpectedly, as it may be reading the state of the internal collection
before the internal collection is fully formed.
For more information on the double-checked locking idiom, see the
Double-Checked Locking Idiom Is Broken Declaration.
Field Summary | |
protected boolean |
|
protected ArrayList |
|
Constructor Summary | |
| |
| |
|
Method Summary | |
boolean |
|
void |
|
boolean |
|
boolean |
|
void |
|
Object |
|
boolean |
|
boolean |
|
void |
|
boolean |
|
Object |
|
boolean |
|
int |
|
int |
|
boolean |
|
Iterator |
|
int |
|
ListIterator |
|
ListIterator |
|
boolean |
|
Object |
|
boolean |
|
boolean |
|
Object |
|
void |
|
int |
|
List |
|
Object[] |
|
Object[] |
|
String |
|
void |
|
protected boolean fast
Are we operating in "fast" mode?
protected ArrayList list
The underlying list we are managing.
public FastArrayList()
Construct a an empty list.
public FastArrayList(Collection collection)
Construct a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
- Parameters:
collection
- The collection whose elements initialize the contents of this list
public FastArrayList(int capacity)
Construct an empty list with the specified capacity.
- Parameters:
capacity
- The initial capacity of the empty list
public boolean add(Object element)
Appends the specified element to the end of this list.
- Parameters:
element
- The element to be appended
public void add(int index, Object element)
Insert the specified element at the specified position in this list, and shift all remaining elements up one position.
- Parameters:
index
- Index at which to insert this elementelement
- The element to be inserted
public boolean addAll(Collection collection)
Append 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.
- Parameters:
collection
- The collection to be appended
public boolean addAll(int index, Collection collection)
Insert all of the elements in the specified Collection at the specified position in this list, and shift any previous elements upwards as needed.
- Parameters:
index
- Index at which insertion takes placecollection
- The collection to be added
public void clear()
Remove all of the elements from this list. The list will be empty after this call returns.
public Object clone()
Return a shallow copy of thisFastArrayList
instance. The elements themselves are not copied.
public boolean contains(Object element)
Returntrue
if this list contains the specified element.
- Parameters:
element
- The element to test for
public boolean containsAll(Collection collection)
Returntrue
if this list contains all of the elements in the specified Collection.
- Parameters:
collection
- Collection whose elements are to be checked
public void ensureCapacity(int capacity)
Increase the capacity of thisArrayList
instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
- Parameters:
capacity
- The new minimum capacity
public boolean equals(Object o)
Compare the specified object with this list for equality. This implementation uses exactly the code that is used to define the list equals function in the documentation for theList.equals
method.
- Parameters:
o
- Object to be compared to this list
public Object get(int index)
Return the element at the specified position in the list.
- Parameters:
index
- The index of the element to return
public boolean getFast()
Returns true if this list is operating in fast mode.
- Returns:
- true if this list is operating in fast mode
public int hashCode()
Return the hash code value for this list. This implementation uses exactly the code that is used to define the list hash function in the documentation for theList.hashCode
method.
public int indexOf(Object element)
Search for the first occurrence of the given argument, testing for equality using theequals()
method, and return the corresponding index, or -1 if the object is not found.
- Parameters:
element
- The element to search for
public boolean isEmpty()
Test if this list has no elements.
public Iterator iterator()
Return an iterator over the elements in this list in proper sequence.
IMPLEMENTATION NOTE - If the list is operating in fast mode, an Iterator is returned, and a structural modification to the list is made, then the Iterator will continue over the previous contents of the list (at the time that the Iterator was created), rather than failing due to concurrent modifications.
public int lastIndexOf(Object element)
Search for the last occurrence of the given argument, testing for equality using theequals()
method, and return the corresponding index, or -1 if the object is not found.
- Parameters:
element
- The element to search for
public ListIterator listIterator()
Return an iterator of the elements of this list, in proper sequence. See the implementation note oniterator()
.
public ListIterator listIterator(int index)
Return an iterator of the elements of this list, in proper sequence, starting at the specified position. See the implementation note oniterator()
.
- Parameters:
index
- The starting position of the iterator to return
public boolean remove(Object element)
Remove the first occurrence of the specified element from the list, and shift any subsequent elements down one position.
- Parameters:
element
- Element to be removed
public Object remove(int index)
Remove the element at the specified position in the list, and shift any subsequent elements down one position.
- Parameters:
index
- Index of the element to be removed
public boolean removeAll(Collection collection)
Remove from this collection all of its elements that are contained in the specified collection.
- Parameters:
collection
- Collection containing elements to be removed
public boolean retainAll(Collection collection)
Remove from this collection all of its elements except those that are contained in the specified collection.
- Parameters:
collection
- Collection containing elements to be retained
public Object set(int index, Object element)
Replace the element at the specified position in this list with the specified element. Returns the previous object at that position.
IMPLEMENTATION NOTE - This operation is specifically documented to not be a structural change, so it is safe to be performed without cloning.
- Parameters:
index
- Index of the element to replaceelement
- The new element to be stored
public void setFast(boolean fast)
Sets whether this list will operate in fast mode.
- Parameters:
fast
- true if the list should operate in fast mode
public int size()
Return the number of elements in this list.
public List subList(int fromIndex, int toIndex)
Return a view of the portion of this list between fromIndex (inclusive) and toIndex (exclusive). The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list. The returned list supports all of the optional list operations supported by this list.
- Parameters:
fromIndex
- The starting index of the sublist viewtoIndex
- The index after the end of the sublist view
public Object[] toArray()
Return an array containing all of the elements in this list in the correct order.
public Object[] toArray(array[] )
Return an array containing all of the elements in this list in the correct order. The runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array, and the size of this list.
- Parameters:
public String toString()
Return a String representation of this object.
public void trimToSize()
Trim the capacity of thisArrayList
instance to be the list's current size. An application can use this operation to minimize the storage of anArrayList
instance.