- Type Parameters:
T- the value type
- All Superinterfaces:
Observer<T>
- All Known Subinterfaces:
CalendarPanel.CalendarDate,CalendarPanel.CalendarDateTime,ComponentValue<C,,T> EntityEditModel.EditorValue<T>,EntityEditModel.EntityEditor,EntityQueryModel.AdditionalCondition,FilterModel.IncludedItems<T>,FilterModel.IncludedPredicate<T>,FilterTableColumnModel.ColumnSelection.ColumnIndex,FilterTableColumnModel.HiddenColumns<C>,FilterTableColumnModel.VisibleColumns<C>,FilterTableSearchModel.Results,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
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 Summary
Modifier and TypeMethodDescriptiondefault booleanaddConsumer(Consumer<? super T> consumer) Addsconsumerto thisObserver.default booleanaddListener(Runnable listener) Addslistenerto thisObserver.default booleanaddWeakConsumer(Consumer<? super T> consumer) Uses aWeakReference, addingconsumerdoes not prevent it from being garbage collected.default booleanaddWeakListener(Runnable listener) Uses aWeakReference, addinglistenerdoes not prevent it from being garbage collected.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.observer()optional()default booleanremoveConsumer(Consumer<? super T> consumer) Removesconsumerfrom thisObserverdefault booleanremoveListener(Runnable listener) Removeslistenerfrom thisObserverdefault booleanremoveWeakConsumer(Consumer<? super T> consumer) Removesconsumerfrom thisObserver.default booleanremoveWeakListener(Runnable listener) Removeslistenerfrom thisObserver
-
Method Details
-
get
- Returns:
- the value
-
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
- Returns:
- an
Optionalwrapping 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
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
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
-
observer
- Returns:
- an
Observernotified each time the observed value may have changed
-
addListener
Description copied from interface:ObserverAddslistenerto thisObserver. Adding the same listener a second time has no effect.- Specified by:
addListenerin interfaceObserver<T>- Parameters:
listener- the listener to add- Returns:
- true if this observer did not already contain the specified listener
-
removeListener
Description copied from interface:ObserverRemoveslistenerfrom thisObserver- Specified by:
removeListenerin interfaceObserver<T>- Parameters:
listener- the listener to remove- Returns:
- true if this observer contained the specified listener
-
addConsumer
Description copied from interface:ObserverAddsconsumerto thisObserver. Adding the same consumer a second time has no effect.- Specified by:
addConsumerin interfaceObserver<T>- Parameters:
consumer- the consumer to add- Returns:
- true if this observer did not already contain the specified consumer
-
removeConsumer
Description copied from interface:ObserverRemovesconsumerfrom thisObserver- Specified by:
removeConsumerin interfaceObserver<T>- Parameters:
consumer- the consumer to remove- Returns:
- true if this observer contained the specified consumer
-
addWeakListener
Description copied from interface:ObserverUses aWeakReference, addinglistenerdoes 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
Observer.removeWeakListener(Runnable)with any non-existing listener:// Clean up dead weak references observer.removeWeakListener(() -> {});- Specified by:
addWeakListenerin interfaceObserver<T>- Parameters:
listener- the listener- Returns:
- true if this observer did not already contain the specified listener
-
removeWeakListener
Description copied from interface:ObserverRemoveslistenerfrom thisObserver- Specified by:
removeWeakListenerin interfaceObserver<T>- Parameters:
listener- the listener to remove- Returns:
- true if this observer contained the specified listener
-
addWeakConsumer
Description copied from interface:ObserverUses aWeakReference, addingconsumerdoes 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
Observer.removeWeakConsumer(Consumer)with any non-existing consumer:// Clean up dead weak references observer.removeWeakConsumer(data -> {});- Specified by:
addWeakConsumerin interfaceObserver<T>- Parameters:
consumer- the consumer- Returns:
- true if this observer did not already contain the specified consumer
-
removeWeakConsumer
Description copied from interface:ObserverRemovesconsumerfrom thisObserver.- Specified by:
removeWeakConsumerin interfaceObserver<T>- Parameters:
consumer- the consumer to remove- Returns:
- true if this observer contained the specified consumer
-