edu.rice.cs.plt.collect
Class ListenerSet<T>

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<Runnable1<? super T>>
              extended by edu.rice.cs.plt.collect.ListenerSet<T>
All Implemented Interfaces:
PredicateSet<Runnable1<? super T>>, SizedIterable<Runnable1<? super T>>, Predicate<java.lang.Object>, Runnable1<T>, Composite, java.io.Serializable, java.lang.Iterable<Runnable1<? super T>>, java.util.Collection<Runnable1<? super T>>, java.util.Set<Runnable1<? super T>>

public class ListenerSet<T>
extends DelegatingSet<Runnable1<? super T>>
implements Runnable1<T>

A mutable set of listeners. Each listener is a Runnable1 and receives an argument of type T. In typical usage, a class supporting listeners has some number of ListenerSet<T> fields and associated getters returning ListenerSet<T>.Sinks (one for each different kind of event to be responded to). Clients use expressions like foo.listeners().add(...) to add listeners; the listener class then calls _listeners.run(...) when an event occurs.

See Also:
Serialized Form

Nested Class Summary
 class ListenerSet.Sink
          A write-only view of the set.
 
Field Summary
 
Fields inherited from class edu.rice.cs.plt.collect.DelegatingCollection
_delegate
 
Constructor Summary
ListenerSet()
          Create a ListenerSet backed by a CopyOnWriteArraySet (thread-safe and efficient for frequent traversal, but slow when frequently mutated).
ListenerSet(java.util.Set<Runnable1<? super T>> delegate)
          Create a ListenerSet backed by the given set.
 
Method Summary
static
<T> ListenerSet<T>
make(java.util.Set<Runnable1<? super T>> delegate)
          Call the constructor (allows T to be inferred).
 void run(T arg)
          Pass the given value to each of the listeners in turn.
 ListenerSet.Sink sink()
          Get a write-only sink view of the set.
 
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, add, addAll, clear, compositeHeight, compositeSize, contains, containsAll, hasFixedSize, isEmpty, isInfinite, isStatic, iterator, remove, removeAll, retainAll, 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
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, 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

ListenerSet

public ListenerSet()
Create a ListenerSet backed by a CopyOnWriteArraySet (thread-safe and efficient for frequent traversal, but slow when frequently mutated).


ListenerSet

public ListenerSet(java.util.Set<Runnable1<? super T>> delegate)
Create a ListenerSet backed by the given set.

Method Detail

run

public void run(T arg)
Pass the given value to each of the listeners in turn. Execution order is determined by the backing set. If an exception occurs in a listener, that exception is set aside until all listeners can be run. Upon completion, the first exception to occur (if any) is thrown.

Specified by:
run in interface Runnable1<T>

sink

public ListenerSet.Sink sink()
Get a write-only sink view of the set. Clients should generally use this interface to register listeners.


make

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