Class AbstractSensor

java.lang.Object
is.codion.common.reactive.sensor.AbstractSensor
All Implemented Interfaces:
Sensor

public abstract class AbstractSensor extends Object implements Sensor
Thread-safe base implementation of Sensor.

Subclasses trigger notifications by calling notifyListeners(), which notifies the listeners in the order they were added.

The sensor() method returns this, so the default methods in Sensor 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

    • AbstractSensor

      protected AbstractSensor()
      Instantiates a new AbstractSensor
  • 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
    • 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
    • sensor

      public final Sensor sensor()
      Description copied from interface: Sensor
      Returns the underlying Sensor to which listener operations are delegated.

      For AbstractSensor and AbstractObserver subclasses this method returns this. For wrapper types, this returns the delegate sensor, and for an Observer implemented via Observer.observer(), its observer.

      Specified by:
      sensor in interface Sensor
      Returns:
      the Sensor handling listener management
    • notifyListeners

      protected final void notifyListeners()
      Notifies the listeners, in the order they were added