java.lang.Object
provided.utils.dispatcher.impl.ADispatcher<TMsg>
- Type Parameters:
TMsg
- The type of messages that will be sent out to the observers.
- All Implemented Interfaces:
IDispatcher<TMsg>
- Direct Known Subclasses:
ParallelDispatcher
,SequentialDispatcher
Mid-level abstraction of a dispatcher that establishes the ability to hold and manage
observers in a thread-safe manner.
- Author:
- swong
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Comparator<IObserver<TMsg>>
Comparator used to order the IObservers in the ConcurrentSkipListSet.private ConcurrentSkipListSet<IObserver<TMsg>>
The internal data storage of observers. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
addObserver(IObserver<TMsg> obs)
Add an observer to the dispatcher.Get a COPY of the set of all the observers currently in the dispatcher.Protected method to allow implementing subclasses access to the set of observers.Removes all the observers currently in the dispatcherremoveObserver(IObserver<TMsg> obs)
Remove an observer from the dispatcher.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface provided.utils.dispatcher.IDispatcher
updateAll
-
Field Details
-
comparator
Comparator used to order the IObservers in the ConcurrentSkipListSet. This is needed because IObservers are not naturally Comparable. Returns zero if the two IObservables are equal, otherwise returns -1 if the first object's hashcode is less than or equal to the second object's hashcode or +1 if the first object's hashcode is greater than the second object's hashcode. -
observers
The internal data storage of observers. Needs to be thread-safe. For systems that have few mutations of the set, a CopyOnWriteArraySet could be used for better read performance and smaller data size.
-
-
Constructor Details
-
ADispatcher
public ADispatcher()
-
-
Method Details
-
getObserverSet
Protected method to allow implementing subclasses access to the set of observers. The actual set of observers is returned and is thus mutable.- Returns:
- The set of observers currently in use. This is NOT a copy.
-
addObserver
Description copied from interface:IDispatcher
Add an observer to the dispatcher. If the observer is already in the dispatcher, as determined by the comparison (equals()) process, the dispatcher is NOT mutated and false is returned.- Specified by:
addObserver
in interfaceIDispatcher<TMsg>
- Parameters:
obs
- The IObserver to add- Returns:
- true if the given observer was not already in the dispatcher, false otherwise.
-
removeObserver
Remove an observer from the dispatcher. The dispatcher does not make any assumptions that the observer being removed is identically the same object as that it was requested to remove via the input parameter. The returned object is the object that was internally held by the dispatcher.
Implementation: Returns the actual object in the internal collection that is equal to the given object.- Specified by:
removeObserver
in interfaceIDispatcher<TMsg>
- Parameters:
obs
- The IObserver to add- Returns:
- The observer that was removed or null if it was not found.
-
getAllObservers
Get a COPY of the set of all the observers currently in the dispatcher. This is a shallow copy, so the observers themselves are not copied.
Implementation: Returns a copy by cloning the internal collection.- Specified by:
getAllObservers
in interfaceIDispatcher<TMsg>
- Returns:
- A set of IObservers
-
removeAllObservers
Removes all the observers currently in the dispatcher
Implementation: Returns a clone of the original internal collection from before it was cleared.- Specified by:
removeAllObservers
in interfaceIDispatcher<TMsg>
- Returns:
- A COPY of the set of IObservers in the dispatcher before they were all removed.
-