Class SafeArrayList<E>
- All Implemented Interfaces:
Cloneable
,Iterable<E>
,Collection<E>
,List<E>
Provides a list with similar modification semantics to java.util.concurrent's CopyOnWriteArrayList except that it is not concurrent and also provides direct access to the current array. This List allows modification of the contents while iterating as any iterators will be looking at a snapshot of the list at the time they were created. Similarly, access the raw internal array is only presenting a snap shot and so can be safely iterated while the list is changing.
All modifications, including set() operations will cause a copy of the data to be created that replaces the old version. Because this list is not designed for threading concurrency it further optimizes the "many modifications" case by buffering them as a normal ArrayList until the next time the contents are accessed.
Normal list modification performance should be equal to ArrayList in a many situations and always better than CopyOnWriteArrayList. Optimum usage is when modifications are done infrequently or in batches... as is often the case in a scene graph. Read operations perform superior to all other methods as the array can be accessed directly.
Important caveats over normal java.util.Lists:
- Even though this class supports modifying the list, the subList() method returns a read-only list. This technically breaks the List contract.
- The ListIterators returned by this class only support the remove() modification method. add() and set() are not supported on the iterator. Even after ListIterator.remove() or Iterator.remove() is called, this change is not reflected in the iterator instance as it is still referring to its original snapshot.
-
Nested Class Summary
-
Constructor Summary
ConstructorDescriptionSafeArrayList
(Class<E> elementType) SafeArrayList
(Class<E> elementType, int capacity) SafeArrayList
(Class<E> elementType, Collection<? extends E> collection) -
Method Summary
Modifier and TypeMethodDescriptionvoid
boolean
boolean
addAll
(int index, Collection<? extends E> c) boolean
addAll
(Collection<? extends E> c) void
clear()
clone()
boolean
boolean
containsAll
(Collection<?> c) protected final E[]
createArray
(int size) protected final <T> T[]
createArray
(Class<T> type, int size) boolean
final E
get
(int index) final E[]
getArray()
Returns a current snapshot of this List's backing array that is guaranteed not to change through further List manipulation.int
hashCode()
int
final boolean
isEmpty()
iterator()
int
listIterator
(int index) remove
(int index) boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) final int
size()
subList
(int fromIndex, int toIndex) Object[]
toArray()
<T> T[]
toArray
(T[] a) toString()
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
replaceAll, sort, spliterator
-
Constructor Details
-
SafeArrayList
-
SafeArrayList
-
SafeArrayList
-
-
Method Details
-
clone
-
createArray
-
createArray
-
getArray
Returns a current snapshot of this List's backing array that is guaranteed not to change through further List manipulation. Changes to this array may or may not be reflected in the list and should be avoided.- Returns:
- either the pre-existing array or a new one
-
getBuffer
-
size
public final int size() -
isEmpty
public final boolean isEmpty() -
contains
-
iterator
-
toArray
-
toArray
public <T> T[] toArray(T[] a) -
add
-
remove
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceList<E>
-
addAll
-
addAll
-
removeAll
-
retainAll
-
clear
public void clear() -
equals
-
hashCode
public int hashCode() -
get
-
set
-
add
-
remove
-
indexOf
-
lastIndexOf
- Specified by:
lastIndexOf
in interfaceList<E>
-
listIterator
- Specified by:
listIterator
in interfaceList<E>
-
listIterator
- Specified by:
listIterator
in interfaceList<E>
-
subList
-
toString
-