Interface Sensor
- All Known Subinterfaces:
CalendarPanel.CalendarDate, CalendarPanel.CalendarDateTime, CalendarPanel.CalendarZonedDateTime, ChangeObserver<T>, ComponentValue<C,T>, EntityConditions.ConditionValue, EntityConditions.Modified, EntityEditor.EditorEntity, EntityEditor.EditorValue<T>, EntityEditor.Exists, EntityEditor.Modified, EntityEditor.Present, EntityTableTreeModel.UnreachableItems, Event, FilterModel.ExcludedItems<T>, FilterModel.IncludedItems<T>, FilterModel.IncludePredicate<T>, FilterTableColumnModel.ColumnSelection.ColumnIndex, FilterTableColumnModel.HiddenColumns<C>, FilterTableColumnModel.VisibleColumns<C>, FilterTableSearchModel.Results, FilterTableSearchModel.Results.CurrentResult, FilterTreeModel.VisibleNodes<T>, ImagePane.ImageValue, MultiSelection.Indexes, MultiSelection.Items<R>, Observable<T>, ObservableState, ObservableValueCollection<T, C>, ObservableValueList<T>, ObservableValueSet<T>, Observer<T>, PersistenceEvents.Deleted, PersistenceEvents.Inserted, PersistenceEvents.Updated, Signal<T>, State, Value<T>, ValueCollection<T, C>, ValueList<T>, ValueSet<T>
- All Known Implementing Classes:
AbstractComponentValue, AbstractObserver, AbstractSensor, AbstractTextComponentValue, AbstractValue, PropertyValue
An Observer is a sensor as well, its listeners notified along with its consumers, so anything that only
needs to know that something happened can listen to an event, a signal, a value or a state alike.
This interface provides default method implementations that delegate to sensor().
There are two ways to implement this interface:
- Extend
AbstractSensor- provides concrete implementations wheresensor()returnsthis - Implement
sensor()to return a delegate - useful for wrapper types
Listeners are notified in the order they were added. An exception thrown by one ends the notification, the listeners after it not notified, and propagates to whatever triggered it.
Thread Safety: Adding and removing listeners is thread-safe in every implementation, and supports concurrent access. Whether triggering a notification is thread-safe depends on the implementation.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault booleanaddListener(Runnable listener) Addslistenerto thisSensor.default booleanaddWeakListener(Runnable listener) Uses aWeakReference, addinglistenerdoes not prevent it from being garbage collected.default booleanremoveListener(Runnable listener) Removeslistenerfrom thisSensor, whether it was added viaaddListener(Runnable)oraddWeakListener(Runnable).default booleanremoveWeakListener(Runnable listener) Removeslistenerfrom thisSensor, whether it was added viaaddListener(Runnable)oraddWeakListener(Runnable).sensor()Returns the underlyingSensorto which listener operations are delegated.
-
Method Details
-
addListener
Addslistenerto thisSensor. Adding the same listener a second time has no effect.Note that if the listener is already registered (whether strongly or via
addWeakListener(Runnable)) this returns false and retains the existing registration kind; it does not upgrade a weak registration to strong.- Parameters:
listener- the listener to add- Returns:
- true if this sensor did not already contain the specified listener
- Throws:
NullPointerException- in case listener is null
-
removeListener
Removeslistenerfrom thisSensor, whether it was added viaaddListener(Runnable)oraddWeakListener(Runnable).- Parameters:
listener- the listener to remove- Returns:
- true if this sensor contained the specified listener
-
addWeakListener
Uses 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
removeWeakListener(Runnable)with any non-existing listener:// Clean up dead weak references sensor.removeWeakListener(() -> {});- Parameters:
listener- the listener- Returns:
- true if this sensor did not already contain the specified listener
-
removeWeakListener
Removeslistenerfrom thisSensor, whether it was added viaaddListener(Runnable)oraddWeakListener(Runnable).- Parameters:
listener- the listener to remove- Returns:
- true if this sensor contained the specified listener
-
sensor
Sensor sensor()Returns 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.- Returns:
- the
Sensorhandling listener management
-