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

java.lang.Object
  extended by java.util.AbstractCollection<T>
      extended by edu.rice.cs.plt.collect.DelegatingCollection<T>
          extended by edu.rice.cs.plt.collect.DelegatingSet<E>
              extended by edu.rice.cs.plt.collect.SnapshotSynchronizedSet<E>
All Implemented Interfaces:
PredicateSet<E>, SizedIterable<E>, Predicate<java.lang.Object>, Composite, java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>

public class SnapshotSynchronizedSet<E>
extends DelegatingSet<E>

A synchronized set like Collections.synchronizedSet(java.util.Set), but one that returns a snapshot of the set contents on invocations of iterator(). In contrast to CopyOnWriteArraySet, copies are only made when needed for iteration; other operations use locking to support concurrency. The snapshot strategy has the following advantages over Collections.synchronizedSet(java.util.Set): 1) Thread safety during iteration is guaranteed; 2) the set is interchangeable with other types of sets, even in contexts that perform iteration; 3) concurrent access to the set is not blocked during iteration; and 4) the set 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 set cannot be blocked by synchronizing on the set itself. To support these differences, the implementation must make a copy whenever iterator() is invoked after the set has been mutated; that copy is cached with the set (optimizing the performance of subsequent calls, but doubling the set's memory footprint).

See Also:
Serialized Form

Field Summary
 
Fields inherited from class edu.rice.cs.plt.collect.DelegatingCollection
_delegate
 
Constructor Summary
SnapshotSynchronizedSet(java.util.Set<E> delegate)
           
 
Method Summary
 boolean add(E o)
           
 boolean addAll(java.util.Collection<? extends E> c)
           
 void clear()
           
 void discardSnapshot()
          Discard the cached copy of the set, if it exists.
static
<T> Thunk<java.util.Set<T>>
factory(Thunk<? extends java.util.Set<T>> delegateFactory)
          Get a thunk that invokes the constructor with sets produced by the given factory.
 java.util.Iterator<E> iterator()
           
static
<T> SnapshotSynchronizedSet<T>
make(java.util.Set<T> delegate)
          Call the constructor (allows T to be inferred).
static
<T> SnapshotSynchronizedSet<T>
makeHash()
          Call the constructor with an empty HashSet.
static
<T> SnapshotSynchronizedSet<T>
makeLinkedHash()
          Call the constructor with an empty LinkedHashSet.
static
<T> SnapshotSynchronizedSet<T>
makeTree()
          Call the constructor with an empty TreeSet.
 boolean remove(java.lang.Object o)
           
 boolean removeAll(java.util.Collection<?> c)
           
 boolean retainAll(java.util.Collection<?> c)
           
 
Methods inherited from class edu.rice.cs.plt.collect.DelegatingSet
equals, hashCode
 
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.Set
contains, containsAll, isEmpty, size, toArray, toArray
 
Methods inherited from interface edu.rice.cs.plt.lambda.Predicate
contains
 
Methods inherited from interface edu.rice.cs.plt.iter.SizedIterable
hasFixedSize, isEmpty, isInfinite, isStatic, size, size
 

Constructor Detail

SnapshotSynchronizedSet

public SnapshotSynchronizedSet(java.util.Set<E> delegate)
Method Detail

discardSnapshot

public void discardSnapshot()
Discard the cached copy of the set, if it exists. This minimizes this set'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.Set<E>
Overrides:
iterator in class DelegatingCollection<E>

add

public boolean add(E o)
Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.Set<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.Set<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.Set<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.Set<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.Set<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.Set<E>
Overrides:
retainAll in class DelegatingCollection<E>

factory

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


make

public static <T> SnapshotSynchronizedSet<T> make(java.util.Set<T> delegate)
Call the constructor (allows T to be inferred).


makeHash

public static <T> SnapshotSynchronizedSet<T> makeHash()
Call the constructor with an empty HashSet.


makeLinkedHash

public static <T> SnapshotSynchronizedSet<T> makeLinkedHash()
Call the constructor with an empty LinkedHashSet.


makeTree

public static <T> SnapshotSynchronizedSet<T> makeTree()
Call the constructor with an empty TreeSet.