Interface Observable<T>
- Type Parameters:
T- the value type
- 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
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));
-
Nested Class Summary
Nested classes/interfaces inherited from interface Observer
Observer.Builder<T,B> -
Method Summary
Modifier and TypeMethodDescriptiondefault ChangeObserver<T> changed()The default implementation returns a newChangeObserverinstance on each call, but subclasses are free to return the same instance.get()default Tdefault TgetOrThrow(String message) default booleanReturns true if the underlying value is equal to the given one.default booleanReturns true if the underlying value is NOT equal to the given one.default booleanisNull()default booleanIf false then get() is guaranteed to never return null.optional()Methods inherited from interface Observer
addConsumer, addWeakConsumer, observer, removeConsumer, removeWeakConsumer, sensor, when, whenMethods inherited from interface Sensor
addListener, addWeakListener, removeListener, removeWeakListener
-
Method Details
-
get
-
getOrThrow
- Returns:
- the value
- Throws:
NoSuchElementException- if no value is present
-
getOrThrow
- Parameters:
message- the error message to use when throwing- Returns:
- the value
- Throws:
NoSuchElementException- if no value is present
-
optional
-
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
-
isNot
-
changed
The default implementation returns a newChangeObserverinstance 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 itsChangeObserver.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 theChange.previous()of the next change.- Returns:
- a
ChangeObserverinstance notified when this observable has changed
-