Package org.reactfx
Class ObservableBase<O,T>
- java.lang.Object
-
- org.reactfx.ObservableBase<O,T>
-
- Type Parameters:
O
- type of the observerT
- type of produced values
- All Implemented Interfaces:
Observable<O>
,ProperObservable<O,T>
- Direct Known Subclasses:
EventStreamBase
,LiveListBase
,SuspendableBase
,SuspenderBase
,ValBase
public abstract class ObservableBase<O,T> extends java.lang.Object implements ProperObservable<O,T>
Base class for observable objects. This abstract class implements:- Observer management: adding and removing observers.
- Lazy binding to inputs. An observable has 0 or more inputs, most commonly, but not necessarily, other observables. Lazy binding to inputs means that the observable observes its inputs only when it is itself being observed.
- Observer notification.
-
-
Field Summary
Fields Modifier and Type Field Description private Subscription
inputSubscription
private ListHelper<O>
observers
private NotificationAccumulator<O,T,?>
pendingNotifications
-
Constructor Summary
Constructors Modifier Constructor Description protected
ObservableBase()
protected
ObservableBase(NotificationAccumulator<O,T,?> pendingNotificationsImpl)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addObserver(O observer)
protected void
enqueueNotifications(T event)
boolean
equals(java.lang.Object o)
protected void
forEachObserver(java.util.function.Consumer<O> action)
Executes action for each observer, regardless of recursion state.protected int
getObserverCount()
int
hashCode()
boolean
isObservingInputs()
protected void
newObserver(O observer)
Called for each new observer.protected void
notifyObservers()
void
notifyObservers(T event)
Subscription
observe(O observer)
protected abstract Subscription
observeInputs()
Starts observing this observable's input(s), if any.void
removeObserver(O observer)
java.lang.String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.reactfx.ProperObservable
defaultEquals, defaultHashCode, defaultNotificationAccumulator, defaultToString
-
-
-
-
Field Detail
-
observers
private ListHelper<O> observers
-
inputSubscription
private Subscription inputSubscription
-
pendingNotifications
private final NotificationAccumulator<O,T,?> pendingNotifications
-
-
Constructor Detail
-
ObservableBase
protected ObservableBase(NotificationAccumulator<O,T,?> pendingNotificationsImpl)
-
ObservableBase
protected ObservableBase()
-
-
Method Detail
-
observeInputs
protected abstract Subscription observeInputs()
Starts observing this observable's input(s), if any. This method is called when the number of observers goes from 0 to 1. This method is called beforenewObserver(Object)
is called for the first observer.- Returns:
- subscription used to stop observing inputs. The subscription is unsubscribed (i.e. input observation stops) when the number of observers goes down to 0.
-
isObservingInputs
public final boolean isObservingInputs()
-
getObserverCount
protected final int getObserverCount()
-
notifyObservers
public final void notifyObservers(T event)
- Specified by:
notifyObservers
in interfaceProperObservable<O,T>
-
enqueueNotifications
protected final void enqueueNotifications(T event)
-
notifyObservers
protected final void notifyObservers()
-
forEachObserver
protected final void forEachObserver(java.util.function.Consumer<O> action)
Executes action for each observer, regardless of recursion state. Ifaction
throws an exception for one observer, it will not be called for any subsequent observers and the exception will be propagated by this method.- Parameters:
action
- action to execute for each observer.
-
newObserver
protected void newObserver(O observer)
Called for each new observer. Overriding this method is a convenient way for subclasses to handle this event, for example to publish some initial events.This method is called after the
observeInputs()
method.
-
observe
public final Subscription observe(O observer)
- Specified by:
observe
in interfaceObservable<O>
-
addObserver
public final void addObserver(O observer)
- Specified by:
addObserver
in interfaceObservable<O>
-
removeObserver
public final void removeObserver(O observer)
- Specified by:
removeObserver
in interfaceObservable<O>
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-