Class Watchable<T>
java.lang.Object
org.apache.bookkeeper.common.util.Watchable<T>
- All Implemented Interfaces:
Recyclable
- Direct Known Subclasses:
FileInfo,TransientLedgerInfo
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 Summary
ConstructorsConstructorDescriptionWatchable(RecyclableArrayList.Recycler<Watcher<T>> recycler) Construct an Watchable with zero watchers. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddWatcher(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.booleandeleteWatcher(Watcher<T> w) Deletes an watcher from the set of watcher of this object.voidClears the watcher list so that this object no longer has any watchers.(package private) int<R> voidnotifyWatchers(Function<R, T> valueFn, R value) Notify the watchers with the update value.voidrecycle()Recycle the instance.
-
Constructor Details
-
Watchable
Construct an Watchable with zero watchers.
-
-
Method Details
-
getNumWatchers
int getNumWatchers() -
addWatcher
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
Deletes an watcher from the set of watcher of this object. Passingnullto this method will have no effect.- Parameters:
w- the watcher to be deleted.
-
notifyWatchers
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:RecyclableRecycle the instance.- Specified by:
recyclein interfaceRecyclable
-