Class AbstractObserver<T>
- Type Parameters:
T- the type of data propagated to listeners
- All Implemented Interfaces:
Observer<T>
- Direct Known Subclasses:
AbstractValue
Observer.
This class provides the canonical way to implement Observer. Subclasses
trigger notifications by calling notifyListeners(Object).
The observer() method returns this, so the default methods
in Observer resolve directly to the concrete implementations here.
All listener management operations are thread-safe. Dead weak references are cleaned up during add/remove operations.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAbstractObserver.AbstractBuilder<T,B extends Observer.Builder<T, B>> An abstract base class for anObserverbuilderNested classes/interfaces inherited from interface is.codion.common.reactive.observer.Observer
Observer.Builder<T,B extends Observer.Builder<T, B>> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstantiates a newAbstractObserverprotectedAbstractObserver(AbstractObserver.AbstractBuilder<T, ?> builder) Instantiates a newAbstractObserver -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanaddConsumer(Consumer<? super T> consumer) Addsconsumerto thisObserver.final booleanaddListener(Runnable listener) Addslistenerto thisObserver.final booleanaddWeakConsumer(Consumer<? super T> consumer) Uses aWeakReference, addingconsumerdoes not prevent it from being garbage collected.final booleanaddWeakListener(Runnable listener) Uses aWeakReference, addinglistenerdoes not prevent it from being garbage collected.protected final voidnotifyListeners(@Nullable T data) Notifies all consumers and listenersobserver()Returns the underlyingObserverto which listener operations are delegated.final booleanremoveConsumer(Consumer<? super T> consumer) Removesconsumerfrom thisObserverfinal booleanremoveListener(Runnable listener) Removeslistenerfrom thisObserverfinal booleanremoveWeakConsumer(Consumer<? super T> consumer) Removesconsumerfrom thisObserver.final booleanremoveWeakListener(Runnable listener) Removeslistenerfrom thisObserverReturns a new conditionalObservernotified when this observer instance is triggered with the given valueReturns a new conditionalObservernotified when this observer instance is triggered with a value satisfying the given predicate.
-
Constructor Details
-
AbstractObserver
protected AbstractObserver()Instantiates a newAbstractObserver -
AbstractObserver
Instantiates a newAbstractObserver- Parameters:
builder- the builder which listeners to add
-
-
Method Details
-
addListener
Description copied from interface:ObserverAddslistenerto thisObserver. Adding the same listener a second time has no effect.Note that if the listener is already registered (whether strongly or via
Observer.addWeakListener(java.lang.Runnable)) this returns false and retains the existing registration kind; it does not upgrade a weak registration to strong.- Specified by:
addListenerin interfaceObserver<T>- Parameters:
listener- the listener to add- Returns:
- true if this observer did not already contain the specified listener
-
removeListener
Description copied from interface:ObserverRemoveslistenerfrom thisObserverNote that this matches by referent regardless of how the listener was registered, so a listener added as a consumer may be removed via this method if it is the same instance.
- Specified by:
removeListenerin interfaceObserver<T>- Parameters:
listener- the listener to remove- Returns:
- true if this observer contained the specified listener
-
addConsumer
Description copied from interface:ObserverAddsconsumerto thisObserver. Adding the same consumer a second time has no effect.- Specified by:
addConsumerin interfaceObserver<T>- Parameters:
consumer- the consumer to add- Returns:
- true if this observer did not already contain the specified consumer
-
removeConsumer
Description copied from interface:ObserverRemovesconsumerfrom thisObserver- Specified by:
removeConsumerin interfaceObserver<T>- Parameters:
consumer- the consumer to remove- Returns:
- true if this observer contained the specified consumer
-
addWeakListener
Description copied from interface:ObserverUses aWeakReference, addinglistenerdoes not prevent it from being garbage collected. Adding the same listener a second time has no effect.Note: Dead weak references accumulate until cleaned up, which happens automatically when listeners are added or removed. To trigger cleanup manually without modifying the listener set, call
Observer.removeWeakListener(Runnable)with any non-existing listener:// Clean up dead weak references observer.removeWeakListener(() -> {});- Specified by:
addWeakListenerin interfaceObserver<T>- Parameters:
listener- the listener- Returns:
- true if this observer did not already contain the specified listener
-
removeWeakListener
Description copied from interface:ObserverRemoveslistenerfrom thisObserver- Specified by:
removeWeakListenerin interfaceObserver<T>- Parameters:
listener- the listener to remove- Returns:
- true if this observer contained the specified listener
-
addWeakConsumer
Description copied from interface:ObserverUses aWeakReference, addingconsumerdoes not prevent it from being garbage collected. Adding the same consumer a second time has no effect.Note: Dead weak references accumulate until cleaned up, which happens automatically when listeners are added or removed. To trigger cleanup manually without modifying the listener set, call
Observer.removeWeakConsumer(Consumer)with any non-existing consumer:// Clean up dead weak references observer.removeWeakConsumer(data -> {});- Specified by:
addWeakConsumerin interfaceObserver<T>- Parameters:
consumer- the consumer- Returns:
- true if this observer did not already contain the specified consumer
-
removeWeakConsumer
Description copied from interface:ObserverRemovesconsumerfrom thisObserver.- Specified by:
removeWeakConsumerin interfaceObserver<T>- Parameters:
consumer- the consumer to remove- Returns:
- true if this observer contained the specified consumer
-
when
Description copied from interface:ObserverReturns a new conditionalObservernotified when this observer instance is triggered with the given valueNote that each call registers a new conditional observer on this observer with no removal path, so avoid calling this repeatedly (for example per row or per component) on a long-lived observer.
-
when
Description copied from interface:ObserverReturns a new conditionalObservernotified when this observer instance is triggered with a value satisfying the given predicate.The predicate is tested with each triggering value, including null, and must tolerate null input.
-
observer
Description copied from interface:ObserverReturns the underlyingObserverto which listener operations are delegated.For
AbstractObserversubclasses this method returnsthis. For wrapper types, this returns the delegate observer. -
notifyListeners
Notifies all consumers and listeners- Parameters:
data- the data to propagate to consumers
-