Module hw06

Class ADispatcher<TMsg>

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

public abstract class ADispatcher<TMsg> extends Object implements IDispatcher<TMsg>
Mid-level abstraction of a dispatcher that establishes the ability to hold and manage observers in a thread-safe manner.
Author:
swong
  • Field Details

    • comparator

      private Comparator<IObserver<TMsg>> 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

      private ConcurrentSkipListSet<IObserver<TMsg>> 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 Set<IObserver<TMsg>> 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

      public boolean addObserver(IObserver<TMsg> obs)
      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 interface IDispatcher<TMsg>
      Parameters:
      obs - The IObserver to add
      Returns:
      true if the given observer was not already in the dispatcher, false otherwise.
    • removeObserver

      public IObserver<TMsg> removeObserver(IObserver<TMsg> obs)
      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 interface IDispatcher<TMsg>
      Parameters:
      obs - The IObserver to add
      Returns:
      The observer that was removed or null if it was not found.
    • getAllObservers

      public Set<IObserver<TMsg>> 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 interface IDispatcher<TMsg>
      Returns:
      A set of IObservers
    • removeAllObservers

      public Set<IObserver<TMsg>> 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 interface IDispatcher<TMsg>
      Returns:
      A COPY of the set of IObservers in the dispatcher before they were all removed.