edu.rice.cs.plt.collect
Class SnapshotSynchronizedList<E>

java.lang.Object
  extended by java.util.AbstractCollection<T>
      extended by edu.rice.cs.plt.collect.DelegatingCollection<E>
          extended by edu.rice.cs.plt.collect.DelegatingList<E>
              extended by 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

Field Summary
 
Fields inherited from class edu.rice.cs.plt.collect.DelegatingList
_delegate
 
Constructor Summary
SnapshotSynchronizedList(List<E> delegate)
           
 
Method Summary
 boolean add(E o)
           
 void add(int index, E element)
           
 boolean addAll(Collection<? extends E> c)
           
 boolean addAll(int index, Collection<? extends E> c)
           
 void clear()
           
 void discardSnapshot()
          Discard the cached copy of the list, if it exists.
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.
 Iterator<E> iterator()
           
 ListIterator<E> listIterator()
           
 ListIterator<E> listIterator(int index)
           
 E remove(int index)
           
 boolean remove(Object o)
           
 boolean removeAll(Collection<?> c)
           
 boolean retainAll(Collection<?> c)
           
 E set(int index, E element)
           
 List<E> subList(int from, int to)
           
 
Methods inherited from class edu.rice.cs.plt.collect.DelegatingList
equals, get, hashCode, indexOf, lastIndexOf
 
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
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
contains, containsAll, isEmpty, size, toArray, toArray
 

Constructor Detail

SnapshotSynchronizedList

public SnapshotSynchronizedList(List<E> delegate)
Method Detail

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.