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 synchronized using an internal lock. 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 runnable) Addslistenerto thisObserver.final booleanaddWeakConsumer(Consumer<? super T> consumer) Uses aWeakReference, addingconsumerdoes not prevent it from being garbage collected.final booleanaddWeakListener(Runnable runnable) 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 runnable) Removeslistenerfrom thisObserverfinal booleanremoveWeakConsumer(Consumer<? super T> consumer) Removesconsumerfrom thisObserver.final booleanremoveWeakListener(Runnable runnable) 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.- Specified by:
addListenerin interfaceObserver<T>- Parameters:
runnable- the listener to add- Returns:
- true if this observer did not already contain the specified listener
-
removeListener
Description copied from interface:ObserverRemoveslistenerfrom thisObserver- Specified by:
removeListenerin interfaceObserver<T>- Parameters:
runnable- 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:
runnable- 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:
runnable- 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 value -
when
Description copied from interface:ObserverReturns a new conditionalObservernotified when this observer instance is triggered with a value satisfying the given predicate -
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
-