- All Implemented Interfaces:
Sensor
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanaddListener(Runnable listener) Addslistenerto thisSensor.final booleanaddWeakListener(Runnable listener) Uses aWeakReference, addinglistenerdoes not prevent it from being garbage collected.protected final voidNotifies the listeners, in the order they were addedfinal booleanremoveListener(Runnable listener) Removeslistenerfrom thisSensor, whether it was added viaSensor.addListener(Runnable)orSensor.addWeakListener(Runnable).final booleanremoveWeakListener(Runnable listener) Removeslistenerfrom thisSensor, whether it was added viaSensor.addListener(Runnable)orSensor.addWeakListener(Runnable).final Sensorsensor()Returns the underlyingSensorto which listener operations are delegated.
-
Constructor Details
-
AbstractSensor
protected AbstractSensor()Instantiates a newAbstractSensor
-
-
Method Details
-
addListener
Description copied from interface:SensorAddslistenerto thisSensor. 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:
addListenerin interfaceSensor- Parameters:
listener- the listener to add- Returns:
- true if this sensor did not already contain the specified listener
-
removeListener
Description copied from interface:SensorRemoveslistenerfrom thisSensor, whether it was added viaSensor.addListener(Runnable)orSensor.addWeakListener(Runnable).- Specified by:
removeListenerin interfaceSensor- Parameters:
listener- the listener to remove- Returns:
- true if this sensor contained the specified listener
-
addWeakListener
Description copied from interface:SensorUses 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
Sensor.removeWeakListener(Runnable)with any non-existing listener:// Clean up dead weak references sensor.removeWeakListener(() -> {});- Specified by:
addWeakListenerin interfaceSensor- Parameters:
listener- the listener- Returns:
- true if this sensor did not already contain the specified listener
-
removeWeakListener
Description copied from interface:SensorRemoveslistenerfrom thisSensor, whether it was added viaSensor.addListener(Runnable)orSensor.addWeakListener(Runnable).- Specified by:
removeWeakListenerin interfaceSensor- Parameters:
listener- the listener to remove- Returns:
- true if this sensor contained the specified listener
-
sensor
Description copied from interface:SensorReturns the underlyingSensorto which listener operations are delegated.For
AbstractSensorandAbstractObserversubclasses this method returnsthis. For wrapper types, this returns the delegate sensor, and for anObserverimplemented viaObserver.observer(), its observer. -
notifyListeners
protected final void notifyListeners()Notifies the listeners, in the order they were added
-