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, java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.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(java.util.List<E> delegate)
           
 
Method Summary
 boolean add(E o)
           
 void add(int index, E element)
           
 boolean addAll(java.util.Collection<? extends E> c)
           
 boolean addAll(int index, java.util.Collection<? extends E> c)
           
 void clear()
           
 void discardSnapshot()
          Discard the cached copy of the list, if it exists.
static
<T> Thunk<java.util.List<T>>
factory(Thunk<? extends java.util.List<T>> delegateFactory)
          Get a thunk that invokes the constructor with sets produced by the given factory.
 java.util.Iterator<E> iterator()
           
 java.util.ListIterator<E> listIterator()
           
 java.util.ListIterator<E> listIterator(int index)
           
 E remove(int index)
           
 boolean remove(java.lang.Object o)
           
 boolean removeAll(java.util.Collection<?> c)
           
 boolean retainAll(java.util.Collection<?> c)
           
 E set(int index, E element)
           
 java.util.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(java.util.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 java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface java.util.List<E>
Overrides:
iterator in class DelegatingCollection<E>

listIterator

public java.util.ListIterator<E> listIterator()
Specified by:
listIterator in interface java.util.List<E>
Overrides:
listIterator in class DelegatingList<E>

listIterator

public java.util.ListIterator<E> listIterator(int index)
Specified by:
listIterator in interface java.util.List<E>
Overrides:
listIterator in class DelegatingList<E>

add

public boolean add(E o)
Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.List<E>
Overrides:
add in class DelegatingCollection<E>

addAll

public boolean addAll(java.util.Collection<? extends E> c)
Specified by:
addAll in interface java.util.Collection<E>
Specified by:
addAll in interface java.util.List<E>
Overrides:
addAll in class DelegatingCollection<E>

clear

public void clear()
Specified by:
clear in interface java.util.Collection<E>
Specified by:
clear in interface java.util.List<E>
Overrides:
clear in class DelegatingCollection<E>

remove

public boolean remove(java.lang.Object o)
Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.List<E>
Overrides:
remove in class DelegatingCollection<E>

removeAll

public boolean removeAll(java.util.Collection<?> c)
Specified by:
removeAll in interface java.util.Collection<E>
Specified by:
removeAll in interface java.util.List<E>
Overrides:
removeAll in class DelegatingCollection<E>

retainAll

public boolean retainAll(java.util.Collection<?> c)
Specified by:
retainAll in interface java.util.Collection<E>
Specified by:
retainAll in interface java.util.List<E>
Overrides:
retainAll in class DelegatingCollection<E>

add

public void add(int index,
                E element)
Specified by:
add in interface java.util.List<E>
Overrides:
add in class DelegatingList<E>

addAll

public boolean addAll(int index,
                      java.util.Collection<? extends E> c)
Specified by:
addAll in interface java.util.List<E>
Overrides:
addAll in class DelegatingList<E>

set

public E set(int index,
             E element)
Specified by:
set in interface java.util.List<E>
Overrides:
set in class DelegatingList<E>

remove

public E remove(int index)
Specified by:
remove in interface java.util.List<E>
Overrides:
remove in class DelegatingList<E>

subList

public java.util.List<E> subList(int from,
                                 int to)
Specified by:
subList in interface java.util.List<E>
Overrides:
subList in class DelegatingList<E>

factory

public static <T> Thunk<java.util.List<T>> factory(Thunk<? extends java.util.List<T>> delegateFactory)
Get a thunk that invokes the constructor with sets produced by the given factory.