- Type Parameters:
T
- the value type
- All Superinterfaces:
Observer<T>
- All Known Subinterfaces:
CalendarPanel.CalendarDate
,CalendarPanel.CalendarDateTime
,ComponentValue<T,
,C> EntityEditModel.EntityEditor
,EntityEditModel.EntityEditor.Exists
,EntityEditModel.EntityEditor.Modified
,EntityEditModel.ValueEditor<T>
,EntityQueryModel.AdditionalCondition
,FilterModel.FilteredItems<T>
,FilterModel.VisibleItems<T>
,FilterTableColumnModel.HiddenColumns<C>
,FilterTableColumnModel.VisibleColumns<C>
,FilterTableSearchModel.Results
,MultiSelection.Index
,MultiSelection.Indexes
,MultiSelection.Items<R>
,ObservableState
,ObservableValueList<T>
,ObservableValues<T,
,C> ObservableValueSet<T>
,PropertyValue<T>
,SingleSelection.Item<T>
,State
,State.Combination
,Value<T>
,ValueList<T>
,Values<T,
,C> ValueSet<T>
- All Known Implementing Classes:
AbstractComponentValue
,AbstractTextComponentValue
,AbstractValue
,NullableToggleButtonModel.ToggleState
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 Summary
Modifier and TypeMethodDescriptiondefault boolean
addConsumer
(Consumer<? super T> consumer) Addsconsumer
to thisObserver
.default boolean
addListener
(Runnable listener) Addslistener
to thisObserver
.default boolean
addWeakConsumer
(Consumer<? super T> consumer) Uses aWeakReference
, addingconsumer
does not prevent it from being garbage collected.default boolean
addWeakListener
(Runnable listener) Uses aWeakReference
, addinglistener
does not prevent it from being garbage collected.get()
default T
default T
getOrThrow
(String message) default boolean
Returns true if the underlying value is equal to the given one.default boolean
isNotEqualTo
(@Nullable T value) Returns true if the underlying value is NOT equal to the given one.default boolean
isNull()
default boolean
If false then get() is guaranteed to never return null.observer()
optional()
default boolean
removeConsumer
(Consumer<? super T> consumer) Removesconsumer
from thisObserver
default boolean
removeListener
(Runnable listener) Removeslistener
from thisObserver
default boolean
removeWeakConsumer
(Consumer<? super T> consumer) Removesconsumer
from thisObserver
.default boolean
removeWeakListener
(Runnable listener) Removeslistener
from 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
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
-
isEqualTo
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
-
isNotEqualTo
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
Observer
notified each time the observed value may have changed
-
addListener
Description copied from interface:Observer
Addslistener
to thisObserver
. Adding the same listener a second time has no effect.- Specified by:
addListener
in 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:Observer
Removeslistener
from thisObserver
- Specified by:
removeListener
in interfaceObserver<T>
- Parameters:
listener
- the listener to remove- Returns:
- true if this observer contained the specified listener
-
addConsumer
Description copied from interface:Observer
Addsconsumer
to thisObserver
. Adding the same consumer a second time has no effect.- Specified by:
addConsumer
in 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:Observer
Removesconsumer
from thisObserver
- Specified by:
removeConsumer
in interfaceObserver<T>
- Parameters:
consumer
- the consumer to remove- Returns:
- true if this observer contained the specified consumer
-
addWeakListener
Description copied from interface:Observer
Uses aWeakReference
, addinglistener
does not prevent it from being garbage collected. Adding the same listener a second time has no effect.- Specified by:
addWeakListener
in interfaceObserver<T>
- Parameters:
listener
- the listener- Returns:
- true if this observer did not already contain the specified listener
-
removeWeakListener
Description copied from interface:Observer
Removeslistener
from thisObserver
- Specified by:
removeWeakListener
in interfaceObserver<T>
- Parameters:
listener
- the listener to remove- Returns:
- true if this observer contained the specified listener
-
addWeakConsumer
Description copied from interface:Observer
Uses aWeakReference
, addingconsumer
does not prevent it from being garbage collected. Adding the same consumer a second time has no effect.- Specified by:
addWeakConsumer
in interfaceObserver<T>
- Parameters:
consumer
- the consumer- Returns:
- true if this observer did not already contain the specified consumer
-
removeWeakConsumer
Description copied from interface:Observer
Removesconsumer
from thisObserver
.- Specified by:
removeWeakConsumer
in interfaceObserver<T>
- Parameters:
consumer
- the consumer to remove- Returns:
- true if this observer contained the specified consumer
-