Class AbstractObserver<T>

java.lang.Object
is.codion.common.reactive.observer.AbstractObserver<T>
Type Parameters:
T - the type of data propagated to listeners
All Implemented Interfaces:
Observer<T>
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).

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

  • Method Details

    • addListener

      public final boolean addListener(Runnable runnable)
      Description copied from interface: Observer
      Adds listener to this Observer. Adding the same listener a second time has no effect.
      Specified by:
      addListener in interface Observer<T>
      Parameters:
      runnable - the listener to add
      Returns:
      true if this observer did not already contain the specified listener
    • removeListener

      public final boolean removeListener(Runnable runnable)
      Description copied from interface: Observer
      Removes listener from this Observer
      Specified by:
      removeListener in interface Observer<T>
      Parameters:
      runnable - the listener to remove
      Returns:
      true if this observer 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
      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 runnable)
      Description copied from interface: Observer
      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 Observer.removeWeakListener(Runnable) with any non-existing listener:

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

      public final boolean removeWeakListener(Runnable runnable)
      Description copied from interface: Observer
      Removes listener from this Observer
      Specified by:
      removeWeakListener in interface Observer<T>
      Parameters:
      runnable - the listener to remove
      Returns:
      true if this observer 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 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:
      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.
      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
      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 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
      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 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 management
    • notifyListeners

      protected final void notifyListeners(@Nullable T data)
      Notifies all consumers and listeners
      Parameters:
      data - the data to propagate to consumers