- Type Parameters:
T
- the value type
- All Superinterfaces:
Observer<T>
- All Known Subinterfaces:
EntityEditModel.EditableEntity
,FilterComboBoxModel.ComboBoxItems<T>
,FilterComboBoxModel.NullItem<T>
,FilterModel.FilteredItems<T>
,FilterModel.Items<T>
,FilterModel.VisibleItems<T>
,MultiItemSelection.Indexes
,MultiItemSelection.Items<R>
,Mutable<T>
- All Known Implementing Classes:
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()
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
T get()- Returns:
- the value
-
optional
- Returns:
- an Optional based on the current value
-
observer
- Returns:
- an
Observer
notified each time the 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
-