|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.AbstractCollection<E> java.util.AbstractList<E> java.util.ArrayList<E> org.apache.commons.collections.FastArrayList<E>
public class FastArrayList<E>
A customized implementation of 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:
When first created, objects of this class default to "slow" mode, where
all accesses of any type are synchronized but no cloning takes place. This
is appropriate for initially populating the collection, followed by a switch
to "fast" mode (by calling 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 |
fast
Are we operating in "fast" mode? |
protected ArrayList<E> |
list
The underlying list we are managing. |
Fields inherited from class java.util.AbstractList |
---|
modCount |
Constructor Summary | |
---|---|
FastArrayList()
Construct a an empty list. |
|
FastArrayList(Collection<E> collection)
Construct a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. |
|
FastArrayList(int capacity)
Construct an empty list with the specified capacity. |
Method Summary | ||
---|---|---|
boolean |
add(E element)
Appends the specified element to the end of this list. |
|
void |
add(int index,
E element)
Insert the specified element at the specified position in this list, and shift all remaining elements up one position. |
|
boolean |
addAll(Collection<? extends E> 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. |
|
boolean |
addAll(int index,
Collection<? extends E> 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. |
|
void |
clear()
Remove all of the elements from this list. |
|
Object |
clone()
Return a shallow copy of this FastArrayList instance. |
|
boolean |
contains(Object element)
Return true if this list contains the specified element. |
|
boolean |
containsAll(Collection<?> collection)
Return true if this list contains all of the elements
in the specified Collection. |
|
void |
ensureCapacity(int capacity)
Increase the capacity of this ArrayList instance, if
necessary, to ensure that it can hold at least the number of elements
specified by the minimum capacity argument. |
|
boolean |
equals(Object o)
Compare the specified object with this list for equality. |
|
E |
get(int index)
Return the element at the specified position in the list. |
|
boolean |
getFast()
Returns true if this list is operating in fast mode. |
|
int |
hashCode()
Return the hash code value for this list. |
|
int |
indexOf(Object element)
Search for the first occurrence of the given argument, testing for equality using the equals() method, and return
the corresponding index, or -1 if the object is not found. |
|
boolean |
isEmpty()
Test if this list has no elements. |
|
Iterator<E> |
iterator()
Return an iterator over the elements in this list in proper sequence. |
|
int |
lastIndexOf(Object element)
Search for the last occurrence of the given argument, testing for equality using the equals() method, and return
the corresponding index, or -1 if the object is not found. |
|
ListIterator<E> |
listIterator()
Return an iterator of the elements of this list, in proper sequence. |
|
ListIterator<E> |
listIterator(int index)
Return an iterator of the elements of this list, in proper sequence, starting at the specified position. |
|
E |
remove(int index)
Remove the element at the specified position in the list, and shift any subsequent elements down one position. |
|
boolean |
remove(Object element)
Remove the first occurrence of the specified element from the list, and shift any subsequent elements down one position. |
|
boolean |
removeAll(Collection<?> collection)
Remove from this collection all of its elements that are contained in the specified collection. |
|
boolean |
retainAll(Collection<?> collection)
Remove from this collection all of its elements except those that are contained in the specified collection. |
|
E |
set(int index,
E element)
Replace the element at the specified position in this list with the specified element. |
|
void |
setFast(boolean fast)
Sets whether this list will operate in fast mode. |
|
int |
size()
Return the number of elements in this list. |
|
List<E> |
subList(int fromIndex,
int toIndex)
Return a view of the portion of this list between fromIndex (inclusive) and toIndex (exclusive). |
|
Object[] |
toArray()
Return an array containing all of the elements in this list in the correct order. |
|
|
toArray(T[] array)
Return an array containing all of the elements in this list in the correct order. |
|
String |
toString()
Return a String representation of this object. |
|
void |
trimToSize()
Trim the capacity of this ArrayList instance to be the
list's current size. |
Methods inherited from class java.util.ArrayList |
---|
removeRange |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected ArrayList<E> list
protected boolean fast
Constructor Detail |
---|
public FastArrayList()
public FastArrayList(int capacity)
capacity
- The initial capacity of the empty listpublic FastArrayList(Collection<E> collection)
collection
- The collection whose elements initialize the contents
of this listMethod Detail |
---|
public boolean getFast()
public void setFast(boolean fast)
fast
- true if the list should operate in fast modepublic boolean add(E element)
add
in interface Collection<E>
add
in interface List<E>
add
in class ArrayList<E>
element
- The element to be appendedpublic void add(int index, E element)
add
in interface List<E>
add
in class ArrayList<E>
index
- Index at which to insert this elementelement
- The element to be inserted
IndexOutOfBoundsException
- if the index is out of rangepublic boolean addAll(Collection<? extends E> collection)
addAll
in interface Collection<E>
addAll
in interface List<E>
addAll
in class ArrayList<E>
collection
- The collection to be appendedpublic boolean addAll(int index, Collection<? extends E> collection)
addAll
in interface List<E>
addAll
in class ArrayList<E>
index
- Index at which insertion takes placecollection
- The collection to be added
IndexOutOfBoundsException
- if the index is out of rangepublic void clear()
clear
in interface Collection<E>
clear
in interface List<E>
clear
in class ArrayList<E>
UnsupportedOperationException
- if clear()
is not supported by this listpublic Object clone()
FastArrayList
instance.
The elements themselves are not copied.
clone
in class ArrayList<E>
public boolean contains(Object element)
true
if this list contains the specified element.
contains
in interface Collection<E>
contains
in interface List<E>
contains
in class ArrayList<E>
element
- The element to test forpublic boolean containsAll(Collection<?> collection)
true
if this list contains all of the elements
in the specified Collection.
containsAll
in interface Collection<E>
containsAll
in interface List<E>
containsAll
in class AbstractCollection<E>
collection
- Collection whose elements are to be checkedpublic void ensureCapacity(int capacity)
ArrayList
instance, if
necessary, to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.
ensureCapacity
in class ArrayList<E>
capacity
- The new minimum capacitypublic boolean equals(Object o)
List.equals
method.
equals
in interface Collection<E>
equals
in interface List<E>
equals
in class AbstractList<E>
o
- Object to be compared to this listpublic E get(int index)
get
in interface List<E>
get
in class ArrayList<E>
index
- The index of the element to return
IndexOutOfBoundsException
- if the index is out of rangepublic int hashCode()
List.hashCode
method.
hashCode
in interface Collection<E>
hashCode
in interface List<E>
hashCode
in class AbstractList<E>
public int indexOf(Object element)
equals()
method, and return
the corresponding index, or -1 if the object is not found.
indexOf
in interface List<E>
indexOf
in class ArrayList<E>
element
- The element to search forpublic boolean isEmpty()
isEmpty
in interface Collection<E>
isEmpty
in interface List<E>
isEmpty
in class ArrayList<E>
public Iterator<E> iterator()
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in interface List<E>
iterator
in class AbstractList<E>
public int lastIndexOf(Object element)
equals()
method, and return
the corresponding index, or -1 if the object is not found.
lastIndexOf
in interface List<E>
lastIndexOf
in class ArrayList<E>
element
- The element to search forpublic ListIterator<E> listIterator()
iterator()
.
listIterator
in interface List<E>
listIterator
in class AbstractList<E>
public ListIterator<E> listIterator(int index)
iterator()
.
listIterator
in interface List<E>
listIterator
in class AbstractList<E>
index
- The starting position of the iterator to return
IndexOutOfBoundsException
- if the index is out of rangepublic E remove(int index)
remove
in interface List<E>
remove
in class ArrayList<E>
index
- Index of the element to be removed
IndexOutOfBoundsException
- if the index is out of rangepublic boolean remove(Object element)
remove
in interface Collection<E>
remove
in interface List<E>
remove
in class ArrayList<E>
element
- Element to be removedpublic boolean removeAll(Collection<?> collection)
removeAll
in interface Collection<E>
removeAll
in interface List<E>
removeAll
in class AbstractCollection<E>
collection
- Collection containing elements to be removed
UnsupportedOperationException
- if this optional operation
is not supported by this listpublic boolean retainAll(Collection<?> collection)
retainAll
in interface Collection<E>
retainAll
in interface List<E>
retainAll
in class AbstractCollection<E>
collection
- Collection containing elements to be retained
UnsupportedOperationException
- if this optional operation
is not supported by this listpublic E set(int index, E element)
set
in interface List<E>
set
in class ArrayList<E>
index
- Index of the element to replaceelement
- The new element to be stored
IndexOutOfBoundsException
- if the index is out of rangepublic int size()
size
in interface Collection<E>
size
in interface List<E>
size
in class ArrayList<E>
public List<E> subList(int fromIndex, int toIndex)
subList
in interface List<E>
subList
in class AbstractList<E>
fromIndex
- The starting index of the sublist viewtoIndex
- The index after the end of the sublist view
IndexOutOfBoundsException
- if an index is out of rangepublic Object[] toArray()
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class ArrayList<E>
public <T> T[] toArray(T[] array)
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class ArrayList<E>
array
- Array defining the element type of the returned list
ArrayStoreException
- if the runtime type of array
is not a supertype of the runtime type of every element in this listpublic String toString()
toString
in class AbstractCollection<E>
public void trimToSize()
ArrayList
instance to be the
list's current size. An application can use this operation to minimize
the storage of an ArrayList
instance.
trimToSize
in class ArrayList<E>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |