Interface Observer<T>

Type Parameters:
T - the type of data propagated to listeners
All Known Subinterfaces:
CalendarPanel.CalendarDate, CalendarPanel.CalendarDateTime, CalendarPanel.CalendarZonedDateTime, ComponentValue<C,T>, EntityConditionModel.ConditionValue, EntityConditionModel.Modified, EntityEditor.EditorEntity, EntityEditor.EditorValue<T>, EntityEditor.Exists, EntityEditor.Modified, EntityEditor.Present, Event<T>, FilterModel.FilteredItems<T>, FilterModel.IncludedItems<T>, FilterModel.IncludePredicate<T>, FilterTableColumnModel.ColumnSelection.ColumnIndex, FilterTableColumnModel.HiddenColumns<C>, FilterTableColumnModel.VisibleColumns<C>, FilterTableSearchModel.Results, FilterTableSearchModel.Results.CurrentResult, ImagePane.ImageValue, MultiSelection.Indexes, MultiSelection.Items<R>, Observable<T>, ObservableState, ObservableValueCollection<T,C>, ObservableValueList<T>, ObservableValueSet<T>, PersistenceEvents.Deleted, PersistenceEvents.Inserted, PersistenceEvents.Updated, State, Value<T>, ValueCollection<T,C>, ValueList<T>, ValueSet<T>
All Known Implementing Classes:
AbstractComponentValue, AbstractObserver, AbstractTextComponentValue, AbstractValue, PropertyValue

public interface Observer<T>
Manages listeners and consumers for observable values and events.

This interface provides default method implementations that delegate to observer(). There are two ways to implement this interface:

  • Extend AbstractObserver - provides concrete implementations where observer() returns this
  • Implement observer() to return a delegate - useful for wrapper types (see Observable for an example)

All implementations are thread-safe and support concurrent access.

See Also:
  • Method Details

    • addListener

      default boolean addListener(Runnable listener)
      Adds listener to this Observer. Adding the same listener a second time has no effect.

      Note that if the listener is already registered (whether strongly or via addWeakListener(java.lang.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 observer did not already contain the specified listener
      Throws:
      NullPointerException - in case listener is null
    • removeListener

      default boolean removeListener(Runnable listener)
      Removes listener from this Observer

      Note that this matches by referent regardless of how the listener was registered, so a listener added as a consumer may be removed via this method if it is the same instance.

      Parameters:
      listener - the listener to remove
      Returns:
      true if this observer contained the specified listener
    • addConsumer

      default boolean addConsumer(Consumer<? super T> consumer)
      Adds consumer to this Observer. Adding the same consumer a second time has no effect.
      Parameters:
      consumer - the consumer to add
      Returns:
      true if this observer did not already contain the specified consumer
      Throws:
      NullPointerException - in case consumer is null
    • removeConsumer

      default boolean removeConsumer(Consumer<? super T> consumer)
      Removes consumer from this Observer
      Parameters:
      consumer - the consumer to remove
      Returns:
      true if this observer contained the specified consumer
    • addWeakListener

      default boolean addWeakListener(Runnable listener)
      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 removeWeakListener(Runnable) with any non-existing listener:

       // Clean up dead weak references
       observer.removeWeakListener(() -> {});
      
      Parameters:
      listener - the listener
      Returns:
      true if this observer did not already contain the specified listener
    • removeWeakListener

      default boolean removeWeakListener(Runnable listener)
      Removes listener from this Observer
      Parameters:
      listener - the listener to remove
      Returns:
      true if this observer contained the specified listener
    • addWeakConsumer

      default boolean addWeakConsumer(Consumer<? super T> consumer)
      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 removeWeakConsumer(Consumer) with any non-existing consumer:

       // Clean up dead weak references
       observer.removeWeakConsumer(data -> {});
      
      Parameters:
      consumer - the consumer
      Returns:
      true if this observer did not already contain the specified consumer
    • removeWeakConsumer

      default boolean removeWeakConsumer(Consumer<? super T> consumer)
      Removes consumer from this Observer.
      Parameters:
      consumer - the consumer to remove
      Returns:
      true if this observer contained the specified consumer
    • when

      default Observer<T> when(@Nullable T value)
      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 of its own, attaching on the first and detaching on the last, so one that is never listened to - or whose listeners are all removed again - leaves nothing behind on this observer. Removing the last listener is therefore how a conditional observer is disposed of; there is nothing else to release.

      Parameters:
      value - the value on which to trigger the observer
      Returns:
      a new conditional Observer
    • when

      default Observer<T> when(Predicate<? super @Nullable T> predicate)
      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 of its own, see when(Object).

      Parameters:
      predicate - the predicate on which to trigger the observer
      Returns:
      a new conditional Observer
    • observer

      Observer<T> 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.

      Returns:
      the Observer handling listener management