Class AbstractObserver<T>

java.lang.Object
is.codion.common.reactive.observer.AbstractObserver<T>
Type Parameters:
T - the type of data propagated to consumers
All Implemented Interfaces:
Observer<T>, Sensor
Direct Known Subclasses:
AbstractValue

public abstract class AbstractObserver<T> extends Object implements Observer<T>
Thread-safe base implementation of Observer.

This class provides the canonical way to implement Observer. Subclasses trigger notifications by calling notifyListeners(Object), which notifies the listeners and consumers in the order they were added.

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:
  • Constructor Details

  • Method Details

    • addListener

      public final boolean addListener(Runnable listener)
      Description copied from interface: Sensor
      Adds listener to this Sensor. Adding the same listener a second time has no effect.

      Note that if the listener is already registered (whether strongly or via Sensor.addWeakListener(java.lang.Runnable)) this returns false and retains the existing registration kind; it does not upgrade a weak registration to strong.

      Specified by:
      addListener in interface Sensor
      Parameters:
      listener - the listener to add
      Returns:
      true if this sensor did not already contain the specified listener
    • removeListener

      public final boolean removeListener(Runnable listener)
      Description copied from interface: Sensor
      Removes listener from this Sensor, whether it was added via Sensor.addListener(Runnable) or Sensor.addWeakListener(Runnable).
      Specified by:
      removeListener in interface Sensor
      Parameters:
      listener - the listener to remove
      Returns:
      true if this sensor contained the specified listener
    • addConsumer

      public final boolean addConsumer(Consumer<? super T> consumer)
      Description copied from interface: Observer
      Adds consumer to this Observer. Adding the same consumer a second time has no effect.
      Specified by:
      addConsumer in interface Observer<T>
      Parameters:
      consumer - the consumer to add
      Returns:
      true if this observer did not already contain the specified consumer
    • removeConsumer

      public final boolean removeConsumer(Consumer<? super T> consumer)
      Description copied from interface: Observer
      Removes consumer from this Observer, whether it was added via Observer.addConsumer(Consumer) or Observer.addWeakConsumer(Consumer).
      Specified by:
      removeConsumer in interface Observer<T>
      Parameters:
      consumer - the consumer to remove
      Returns:
      true if this observer contained the specified consumer
    • addWeakListener

      public final boolean addWeakListener(Runnable listener)
      Description copied from interface: Sensor
      Uses a WeakReference, adding listener does 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 Sensor.removeWeakListener(Runnable) with any non-existing listener:

      // Clean up dead weak references
      sensor.removeWeakListener(() -> {});
      Specified by:
      addWeakListener in interface Sensor
      Parameters:
      listener - the listener
      Returns:
      true if this sensor did not already contain the specified listener
    • removeWeakListener

      public final boolean removeWeakListener(Runnable listener)
      Description copied from interface: Sensor
      Removes listener from this Sensor, whether it was added via Sensor.addListener(Runnable) or Sensor.addWeakListener(Runnable).
      Specified by:
      removeWeakListener in interface Sensor
      Parameters:
      listener - the listener to remove
      Returns:
      true if this sensor contained the specified listener
    • addWeakConsumer

      public final boolean addWeakConsumer(Consumer<? super T> consumer)
      Description copied from interface: Observer
      Uses a WeakReference, adding consumer does 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 or consumers are added or removed. To trigger cleanup manually without modifying them, call Observer.removeWeakConsumer(Consumer) with any non-existing consumer:

      // Clean up dead weak references
      observer.removeWeakConsumer(data -> {});
      Specified by:
      addWeakConsumer in interface Observer<T>
      Parameters:
      consumer - the consumer
      Returns:
      true if this observer did not already contain the specified consumer
    • removeWeakConsumer

      public final boolean removeWeakConsumer(Consumer<? super T> consumer)
      Description copied from interface: Observer
      Removes consumer from this Observer, whether it was added via Observer.addConsumer(Consumer) or Observer.addWeakConsumer(Consumer).
      Specified by:
      removeWeakConsumer in interface Observer<T>
      Parameters:
      consumer - the consumer to remove
      Returns:
      true if this observer contained the specified consumer
    • when

      public final Observer<T> when(@Nullable T value)
      Description copied from interface: Observer
      Returns a new conditional Observer notified when this observer instance is triggered with the given value

      The conditional observer subscribes to this one only while it has listeners or consumers of its own, attaching on the first and detaching on the last, so one that is never listened to - or whose listeners and consumers are all removed again - leaves nothing behind on this observer. Removing the last of them is therefore how a conditional observer is disposed of; there is nothing else to release.

      Specified by:
      when in interface Observer<T>
      Parameters:
      value - the value on which to trigger the observer
      Returns:
      a new conditional Observer
    • when

      public final Observer<T> when(Predicate<? super @Nullable T> predicate)
      Description copied from interface: Observer
      Returns a new conditional Observer notified 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.

      Subscribes to this observer only while it has listeners or consumers of its own, see Observer.when(Object).

      Specified by:
      when in interface Observer<T>
      Parameters:
      predicate - the predicate on which to trigger the observer
      Returns:
      a new conditional Observer
    • observer

      public final Observer<T> observer()
      Description copied from interface: Observer
      Returns the underlying Observer to which listener and consumer operations are delegated.

      For AbstractObserver subclasses this method returns this. For wrapper types, this returns the delegate observer.

      Specified by:
      observer in interface Observer<T>
      Returns:
      the Observer handling listener and consumer management
    • sensor

      public final Sensor sensor()
      Description copied from interface: Observer
      An observer is its own sensor, via Observer.observer().
      Specified by:
      sensor in interface Observer<T>
      Specified by:
      sensor in interface Sensor
      Returns:
      the Sensor handling listener management, this observer's Observer.observer()
    • notifyListeners

      protected final void notifyListeners(@Nullable T data)
      Notifies the listeners and consumers, in the order they were added
      Parameters:
      data - the data to propagate to consumers