Interface Observable<T>

Type Parameters:
T - the value type
All Superinterfaces:
Observer<T>
All Known Subinterfaces:
CalendarPanel.CalendarDate, CalendarPanel.CalendarDateTime, CalendarPanel.CalendarZonedDateTime, ComponentValue<C,T>, EntityConditions.ConditionValue, EntityEditor.EditorEntity, EntityEditor.EditorValue<T>, FilterModel.FilteredItems<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 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

      @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 Observer<Change<T>> changed()
      The default implementation returns a new Observer 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.

      Returns:
      an Observer instance notified when this observable has changed