edu.rice.cs.plt.collect
Class SnapshotSynchronizedList<E>
java.lang.Object
java.util.AbstractCollection<T>
edu.rice.cs.plt.collect.DelegatingCollection<E>
edu.rice.cs.plt.collect.DelegatingList<E>
edu.rice.cs.plt.collect.SnapshotSynchronizedList<E>
- All Implemented Interfaces:
- SizedIterable<E>, Composite, Serializable, Iterable<E>, Collection<E>, List<E>
public class SnapshotSynchronizedList<E>
- extends DelegatingList<E>
A synchronized list like Collections.synchronizedList(java.util.List), but one that returns a snapshot of
the list contents on invocations of iterator(). In contrast to
CopyOnWriteArrayList, copies are only made when needed for iteration; other
operations use locking to support concurrency. The snapshot strategy has the following advantages over
Collections.synchronizedList(java.util.List): 1) Thread safety during iteration is guaranteed; 2) the list is
interchangeable with other types of lists, even in contexts that perform iteration; 3) concurrent access to
the list is not blocked during iteration; and 4) the list can be directly mutated by the iteration loop
(on the other hand, removing elements via the iterator is not supported). Note, also, that operations on
this list cannot be blocked by synchronizing on the list itself. To support these differences, the
implementation must make a copy whenever iterator() is invoked after the list has been mutated;
that copy is cached with the list (optimizing the performance of subsequent calls, but doubling the
list's memory footprint).
- See Also:
- Serialized Form
| Methods inherited from class edu.rice.cs.plt.collect.DelegatingCollection |
abstractCollectionAddAll, abstractCollectionClear, abstractCollectionContains, abstractCollectionContainsAll, abstractCollectionIsEmpty, abstractCollectionRemove, abstractCollectionRemoveAll, abstractCollectionRetainAll, abstractCollectionToArray, abstractCollectionToArray, compositeHeight, compositeSize, contains, containsAll, hasFixedSize, isEmpty, isInfinite, isStatic, size, size, toArray, toArray, toString |
SnapshotSynchronizedList
public SnapshotSynchronizedList(List<E> delegate)
discardSnapshot
public void discardSnapshot()
- Discard the cached copy of the list, if it exists. This minimizes this list's memory footprint, but
forces the copy to be recalculated when
iterator() is next invoked. Has no effect if
iterator() has not been invoked since the last mutating operation.
iterator
public Iterator<E> iterator()
- Specified by:
iterator in interface Iterable<E>- Specified by:
iterator in interface Collection<E>- Specified by:
iterator in interface List<E>- Overrides:
iterator in class DelegatingCollection<E>
listIterator
public ListIterator<E> listIterator()
- Specified by:
listIterator in interface List<E>- Overrides:
listIterator in class DelegatingList<E>
listIterator
public ListIterator<E> listIterator(int index)
- Specified by:
listIterator in interface List<E>- Overrides:
listIterator in class DelegatingList<E>
add
public boolean add(E o)
- Specified by:
add in interface Collection<E>- Specified by:
add in interface List<E>- Overrides:
add in class DelegatingCollection<E>
addAll
public boolean addAll(Collection<? extends E> c)
- Specified by:
addAll in interface Collection<E>- Specified by:
addAll in interface List<E>- Overrides:
addAll in class DelegatingCollection<E>
clear
public void clear()
- Specified by:
clear in interface Collection<E>- Specified by:
clear in interface List<E>- Overrides:
clear in class DelegatingCollection<E>
remove
public boolean remove(Object o)
- Specified by:
remove in interface Collection<E>- Specified by:
remove in interface List<E>- Overrides:
remove in class DelegatingCollection<E>
removeAll
public boolean removeAll(Collection<?> c)
- Specified by:
removeAll in interface Collection<E>- Specified by:
removeAll in interface List<E>- Overrides:
removeAll in class DelegatingCollection<E>
retainAll
public boolean retainAll(Collection<?> c)
- Specified by:
retainAll in interface Collection<E>- Specified by:
retainAll in interface List<E>- Overrides:
retainAll in class DelegatingCollection<E>
add
public void add(int index,
E element)
- Specified by:
add in interface List<E>- Overrides:
add in class DelegatingList<E>
addAll
public boolean addAll(int index,
Collection<? extends E> c)
- Specified by:
addAll in interface List<E>- Overrides:
addAll in class DelegatingList<E>
set
public E set(int index,
E element)
- Specified by:
set in interface List<E>- Overrides:
set in class DelegatingList<E>
remove
public E remove(int index)
- Specified by:
remove in interface List<E>- Overrides:
remove in class DelegatingList<E>
subList
public List<E> subList(int from,
int to)
- Specified by:
subList in interface List<E>- Overrides:
subList in class DelegatingList<E>
factory
public static <T> Thunk<List<T>> factory(Thunk<? extends List<T>> delegateFactory)
- Get a thunk that invokes the constructor with sets produced by the given factory.