Interface Observer<T>

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

public interface Observer<T>
Manages event listeners. Implemented by Event.

All implementations are thread-safe and support concurrent access.

See Also:
  • Method Details

    • addListener

      boolean addListener(Runnable listener)
      Adds listener to this Observer. Adding the same listener a second time has no effect.
      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

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

      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

      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

      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

      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

      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

      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 Conditional.OnCondition<T> when(T value)
      Returns an Conditional.OnCondition instance for adding conditional listeners to this observer. This provides a fluent API for reacting to specific values or conditions.
       // React to a specific value
       viewState
           .when(View.VISIBLE)
           .run(this::onVisible);
      
       // Chain multiple conditions
       value
           .when(1).run(() -> System.out.println("one"))
           .when(2).run(() -> System.out.println("two"))
           .when(v -> v > 10).accept(this::handleLarge);
      
      Returns:
      a new Conditional.OnCondition
    • when

      default Conditional.OnCondition<T> when(Predicate<? super T> predicate)
      Returns an Conditional.OnCondition for adding conditional listeners to the given observer. This provides a fluent API for reacting to specific conditions.
       selection.item()
           .when(Objects::nonNull)
           .accept(this::handleSelectedItem)
           .when(Objects::isNull)
           .run(this::onEmptySelection);
      
      Returns:
      a new Conditional