public class Watchable<T> extends java.lang.Object implements Recyclable
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 and Description |
|---|
Watchable(RecyclableArrayList.Recycler<Watcher<T>> recycler)
Construct an Watchable with zero watchers.
|
| Modifier and Type | Method and 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.
|
public Watchable(RecyclableArrayList.Recycler<Watcher<T>> recycler)
int getNumWatchers()
public boolean addWatcher(Watcher<T> w)
w - an watcher to be added.java.lang.NullPointerException - if the parameter o is null.public boolean deleteWatcher(Watcher<T> w)
null to this method will have no effect.w - the watcher to be deleted.public <R> void notifyWatchers(java.util.function.Function<R,T> valueFn, R value)
value - value to notifypublic void deleteWatchers()
public void recycle()
Recyclablerecycle in interface RecyclableCopyright © 2011–2022 The Apache Software Foundation. All rights reserved.