Interface Observable<T>

Type Parameters:
T - the value type
All Superinterfaces:
Observer<T>, Sensor
All Known Subinterfaces:
CalendarPanel.CalendarDate, CalendarPanel.CalendarDateTime, CalendarPanel.CalendarZonedDateTime, ComponentValue<C,T>, EntityConditions.ConditionValue, EntityEditor.EditorEntity, EntityEditor.EditorValue<T>, EntityTableTreeModel.UnreachableItems, 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>, ObservableValueCollection<T,C>, ObservableValueList<T>, ObservableValueSet<T>, Value<T>, ValueCollection<T,C>, ValueList<T>, ValueSet<T>
All Known Implementing Classes:
AbstractComponentValue, AbstractTextComponentValue, AbstractValue, PropertyValue

public interface Observable<T> extends Observer<T>
A wrapper for a value, providing a change observer.

Thread Safety: Listener management is thread-safe, value access thread-safety is implementation dependent.

class Person {
  private final Signal<String> nameChanged = Signal.signal();

  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

      @Nullable T get()
      Returns:
      the value
    • getOrThrow

      default T getOrThrow()
      Returns:
      the value
      Throws:
      NoSuchElementException - if no value is present
    • getOrThrow

      default T getOrThrow(String message)
      Parameters:
      message - the error message to use when throwing
      Returns:
      the value
      Throws:
      NoSuchElementException - if no value is present
    • optional

      default Optional<T> optional()
      Returns:
      an Optional wrapping this value.
    • isNull

      default boolean isNull()
      Returns:
      true if the underlying value is null.
    • isNullable

      default boolean isNullable()
      If false then get() is guaranteed to never return null.
      Returns:
      true if this observable can be null
    • is

      default boolean is(@Nullable T value)
      Returns true if the underlying value is equal to the given one. Note that null == null.
      Parameters:
      value - the value
      Returns:
      true if the underlying value is equal to the given one
    • isNot

      default boolean isNot(@Nullable T value)
      Returns true if the underlying value is NOT equal to the given one. Note that null == null.
      Parameters:
      value - the value
      Returns:
      true if the underlying value is NOT equal to the given one
    • changed

      default ChangeObserver<T> changed()
      The default implementation returns a new ChangeObserver instance on each call, but subclasses are free to return the same instance.

      Note that when a new instance is returned per call, a listener added through one changed() instance can only be removed through that same instance; retain it if the listener must later be removed.

      The instance subscribes to this observable only while it, its ChangeObserver.previous() or its ChangeObserver.current() has listeners or consumers, so one never listened to costs nothing, and one whose listeners and consumers are all removed is left to be collected. One whose weak listeners and consumers have all been collected unsubscribes at the next change.

      The observer is notified when this observable notifies a value differing from the one before, by Objects.deepEquals(Object, Object). An equal value, a refreshed instance for example, isn't reported, but replaces the one before as the Change.previous() of the next change.

      Returns:
      a ChangeObserver instance notified when this observable has changed