Class Watchable<T>

java.lang.Object
org.apache.bookkeeper.common.util.Watchable<T>
All Implemented Interfaces:
Recyclable
Direct Known Subclasses:
FileInfo, TransientLedgerInfo

public class Watchable<T> extends Object implements Recyclable
This class represents an watchable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have watched.

An watchable object can have one or more watchers. An watcher may be any object that implements interface Watcher. After an watchable instance changes, an application calling the Watchable's notifyWatchers method causes all of its watchers to be notified of the change by a call to their update method.

A watcher is automatically removed from the watchers list once an event is fired to the watcher.

Note that this notification mechanism has nothing to do with threads and is completely separate from the wait and notify mechanism of class Object.

When an watchable object is newly created, its set of watchers is empty. If a same watcher is added multiple times to this watchable, it will receive the notifications multiple times.

  • Constructor Details

  • Method Details

    • getNumWatchers

      int getNumWatchers()
    • addWatcher

      public boolean addWatcher(Watcher<T> w)
      Adds an watcher to the set of watchers for this object, provided that it is not the same as some watcher already in the set. The order in which notifications will be delivered to multiple watchers is not specified. See the class comment.
      Parameters:
      w - an watcher to be added.
      Returns:
      true if a watcher is added to the list successfully, otherwise false.
      Throws:
      NullPointerException - if the parameter o is null.
    • deleteWatcher

      public boolean deleteWatcher(Watcher<T> w)
      Deletes an watcher from the set of watcher of this object. Passing null to this method will have no effect.
      Parameters:
      w - the watcher to be deleted.
    • notifyWatchers

      public <R> void notifyWatchers(Function<R,T> valueFn, R value)
      Notify the watchers with the update value.
      Parameters:
      value - value to notify
    • deleteWatchers

      public void deleteWatchers()
      Clears the watcher list so that this object no longer has any watchers.
    • recycle

      public void recycle()
      Description copied from interface: Recyclable
      Recycle the instance.
      Specified by:
      recycle in interface Recyclable