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 java.lang.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
'snotifyWatchers
method causes all of its watchers to be notified of the change by a call to theirupdate
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
Constructors Constructor Description Watchable(RecyclableArrayList.Recycler<Watcher<T>> recycler)
Construct an Watchable with zero watchers.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.boolean
deleteWatcher(Watcher<T> w)
Deletes an watcher from the set of watcher of this object.void
deleteWatchers()
Clears the watcher list so that this object no longer has any watchers.(package private) int
getNumWatchers()
<R> void
notifyWatchers(java.util.function.Function<R,T> valueFn, R value)
Notify the watchers with the update value.void
recycle()
Recycle the instance.
-
-
-
Constructor Detail
-
Watchable
public Watchable(RecyclableArrayList.Recycler<Watcher<T>> recycler)
Construct an Watchable with zero watchers.
-
-
Method Detail
-
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:
java.lang.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. Passingnull
to this method will have no effect.- Parameters:
w
- the watcher to be deleted.
-
notifyWatchers
public <R> void notifyWatchers(java.util.function.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 interfaceRecyclable
-
-