Interface Observable<T>

Type Parameters:
T - the value type
All Superinterfaces:
Observer<T>
All Known Subinterfaces:
EntityEditModel.EditableEntity, FilterComboBoxModel.ComboBoxItems<T>, FilterComboBoxModel.NullItem<T>, FilterModel.FilteredItems<T>, FilterModel.Items<T>, FilterModel.VisibleItems<T>, MultiItemSelection.Indexes, MultiItemSelection.Items<R>, Mutable<T>
All Known Implementing Classes:
NullableToggleButtonModel.ToggleState

public interface Observable<T> extends Observer<T>
A wrapper for a value, providing a change observer.
 
   class Person {
       private final Event<String> nameChanged = Event.event();

       private String name;

       public String getName() {
           return name;
       }

       public void setName(String name) {
           this.name = name;
           nameChanged.accept(name);
      }
   }

   Person person = new Person();

   Observable<String> observableName = new Observable<>() {
       @Override
       public String get() {
           return person.getName();
       }

       @Override
       public Observer<String> observer() {
           return person.nameChanged.observer();
       }
  };

  observableName.addConsumer(newName ->
          System.out.println("Name changed to " + newName));
 
 
  • Method Details

    • get

      T get()
      Returns:
      the value
    • optional

      default Optional<T> optional()
      Returns:
      an Optional based on the current value
    • observer

      Observer<T> observer()
      Returns:
      an Observer notified each time the value may have changed
    • addListener

      default boolean addListener(Runnable listener)
      Description copied from interface: Observer
      Adds listener to this Observer. Adding the same listener a second time has no effect.
      Specified by:
      addListener in interface Observer<T>
      Parameters:
      listener - the listener to add
      Returns:
      true if this observer did not already contain the specified listener
    • removeListener

      default boolean removeListener(Runnable listener)
      Description copied from interface: Observer
      Removes listener from this Observer
      Specified by:
      removeListener in interface Observer<T>
      Parameters:
      listener - the listener to remove
      Returns:
      true if this observer contained the specified listener
    • addConsumer

      default boolean addConsumer(Consumer<? super T> consumer)
      Description copied from interface: Observer
      Adds consumer to this Observer. Adding the same consumer a second time has no effect.
      Specified by:
      addConsumer in interface Observer<T>
      Parameters:
      consumer - the consumer to add
      Returns:
      true if this observer did not already contain the specified consumer
    • removeConsumer

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

      default boolean addWeakListener(Runnable listener)
      Description copied from interface: Observer
      Uses a WeakReference, adding listener does not prevent it from being garbage collected. Adding the same listener a second time has no effect.
      Specified by:
      addWeakListener in interface Observer<T>
      Parameters:
      listener - the listener
      Returns:
      true if this observer did not already contain the specified listener
    • removeWeakListener

      default boolean removeWeakListener(Runnable listener)
      Description copied from interface: Observer
      Removes listener from this Observer
      Specified by:
      removeWeakListener in interface Observer<T>
      Parameters:
      listener - the listener to remove
      Returns:
      true if this observer contained the specified listener
    • addWeakConsumer

      default boolean addWeakConsumer(Consumer<? super T> consumer)
      Description copied from interface: Observer
      Uses a WeakReference, adding consumer does not prevent it from being garbage collected. Adding the same consumer a second time has no effect.
      Specified by:
      addWeakConsumer in interface Observer<T>
      Parameters:
      consumer - the consumer
      Returns:
      true if this observer did not already contain the specified consumer
    • removeWeakConsumer

      default boolean removeWeakConsumer(Consumer<? super T> consumer)
      Description copied from interface: Observer
      Removes consumer from this Observer.
      Specified by:
      removeWeakConsumer in interface Observer<T>
      Parameters:
      consumer - the consumer to remove
      Returns:
      true if this observer contained the specified consumer