java.lang.Object
is.codion.common.value.AbstractValue<T>
- Type Parameters:
T
- the value type
- All Implemented Interfaces:
EventObserver<T>
,Value<T>
,ValueObserver<T>
,Consumer<T>
,Supplier<T>
- Direct Known Subclasses:
AbstractComponentValue
An abstract
The constructor parameter
Value
implementation handling everything except the value itself.The constructor parameter
notify
specifies whether this Value
instance should automatically call
notifyListeners()
when the value is set or changed via set(Object)
.
Some implementations may want to handle this manually.-
Nested Class Summary
Nested classes/interfaces inherited from interface is.codion.common.value.Value
Value.Notify, Value.Validator<T>
-
Constructor Summary
ModifierConstructorDescriptionprotected
protected
AbstractValue
(T nullValue) Creates anAbstractValue
instance, which does not notify listeners.protected
AbstractValue
(T nullValue, Value.Notify notify) Creates anAbstractValue
instance. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
Sets the valuefinal boolean
addDataListener
(Consumer<? super T> listener) Addslistener
to thisEventObserver
.final boolean
addListener
(Runnable listener) Addslistener
to thisEventObserver
.final boolean
addValidator
(Value.Validator<T> validator) Adds a validator to thisValue
.final boolean
addWeakDataListener
(Consumer<? super T> listener) Uses aWeakReference
, addinglistener
does not prevent it from being garbage collected.final boolean
addWeakListener
(Runnable listener) Uses aWeakReference
, addinglistener
does not prevent it from being garbage collected.final void
Creates a bidirectional link between this and the given original value, so that changes in one are reflected in the other.final void
link
(ValueObserver<T> originalValue) Creates a unidirectional link between this value and the given original value observer, so that changes in the original value are reflected in this one.final boolean
Sets a new value mapped from the current value.protected final void
Notifies listeners that the underlying value has changed or at least that it may have changedfinal boolean
nullable()
If false then get() is guaranteed to never return null.final ValueObserver<T>
observer()
Returns aValueObserver
notified each time this value changes.final boolean
removeDataListener
(Consumer<? super T> listener) Removeslistener
from thisEventObserver
final boolean
removeListener
(Runnable listener) Removeslistener
from thisEventObserver
final boolean
removeValidator
(Value.Validator<T> validator) Removes the given validator from this valuefinal boolean
removeWeakDataListener
(Consumer<? super T> listener) Removeslistener
from thisEventObserver
.final boolean
removeWeakListener
(Runnable listener) Removeslistener
from thisEventObserver
final boolean
Sets the value.protected abstract void
Sets the actual internal value.final void
Unlinks this value from the given original valuefinal void
unlink
(ValueObserver<T> originalValue) Unlinks this value from the given original value observerfinal void
Validate the given value using all validatorsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface is.codion.common.value.ValueObserver
equalTo, isNotNull, isNull, notEqualTo, optional
-
Constructor Details
-
AbstractValue
protected AbstractValue() -
AbstractValue
Creates anAbstractValue
instance, which does not notify listeners.- Parameters:
nullValue
- the value to use instead of null
-
AbstractValue
Creates anAbstractValue
instance.- Parameters:
nullValue
- the value to use instead of nullnotify
- specifies when to notify listeners
-
-
Method Details
-
set
Description copied from interface:Value
Sets the value. Note that if the value is equal to the current value according toObjects.equals(java.lang.Object, java.lang.Object)
the underlying value is still set, but no change event is triggered. -
map
Description copied from interface:Value
Sets a new value mapped from the current value.Value<Integer> value = Value.value(0); // increment the value by one value.map(currentValue -> currentValue + 1);
-
observer
Description copied from interface:Value
Returns aValueObserver
notified each time this value changes.- Specified by:
observer
in interfaceValue<T>
- Returns:
- a
ValueObserver
for this value
-
nullable
public final boolean nullable()Description copied from interface:ValueObserver
If false then get() is guaranteed to never return null.- Specified by:
nullable
in interfaceValueObserver<T>
- Returns:
- true if this value can be null
-
accept
Description copied from interface:Value
Sets the value -
addListener
Description copied from interface:EventObserver
Addslistener
to thisEventObserver
. Adding the same listener a second time has no effect.- Specified by:
addListener
in interfaceEventObserver<T>
- Parameters:
listener
- the listener to add- Returns:
- true if this observer did not already contain the specified listener
-
removeListener
Description copied from interface:EventObserver
Removeslistener
from thisEventObserver
- Specified by:
removeListener
in interfaceEventObserver<T>
- Parameters:
listener
- the listener to remove- Returns:
- true if this observer contained the specified listener
-
addDataListener
Description copied from interface:EventObserver
Addslistener
to thisEventObserver
. Adding the same listener a second time has no effect.- Specified by:
addDataListener
in interfaceEventObserver<T>
- Parameters:
listener
- the listener to add- Returns:
- true if this observer did not already contain the specified listener
-
removeDataListener
Description copied from interface:EventObserver
Removeslistener
from thisEventObserver
- Specified by:
removeDataListener
in interfaceEventObserver<T>
- Parameters:
listener
- the listener to remove- Returns:
- true if this observer contained the specified listener
-
addWeakListener
Description copied from interface:EventObserver
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 interfaceEventObserver<T>
- Parameters:
listener
- the listener- Returns:
- true if this observer did not already contain the specified listener
-
removeWeakListener
Description copied from interface:EventObserver
Removeslistener
from thisEventObserver
- Specified by:
removeWeakListener
in interfaceEventObserver<T>
- Parameters:
listener
- the listener to remove- Returns:
- true if this observer contained the specified listener
-
addWeakDataListener
Description copied from interface:EventObserver
Uses aWeakReference
, addinglistener
does not prevent it from being garbage collected. Adding the same listener a second time has no effect.- Specified by:
addWeakDataListener
in interfaceEventObserver<T>
- Parameters:
listener
- the listener- Returns:
- true if this observer did not already contain the specified listener
-
removeWeakDataListener
Description copied from interface:EventObserver
Removeslistener
from thisEventObserver
.- Specified by:
removeWeakDataListener
in interfaceEventObserver<T>
- Parameters:
listener
- the listener to remove- Returns:
- true if this observer contained the specified listener
-
link
Description copied from interface:Value
Creates a bidirectional link between this and the given original value, so that changes in one are reflected in the other. Note that after a call to this method this value is the same asoriginalValue
. -
unlink
Description copied from interface:Value
Unlinks this value from the given original value -
link
Description copied from interface:Value
Creates a unidirectional link between this value and the given original value observer, so that changes in the original value are reflected in this one. Note that after a call to this method the value of this value is the same as the original value. -
unlink
Description copied from interface:Value
Unlinks this value from the given original value observer -
addValidator
Description copied from interface:Value
Adds a validator to thisValue
. Adding the same validator again has no effect.- Specified by:
addValidator
in interfaceValue<T>
- Parameters:
validator
- the validator- Returns:
- true if this value did not already contain the specified validator
-
removeValidator
Description copied from interface:Value
Removes the given validator from this value- Specified by:
removeValidator
in interfaceValue<T>
- Parameters:
validator
- the validator- Returns:
- true if this value contained the specified validator
-
validate
Description copied from interface:Value
Validate the given value using all validators -
setValue
Sets the actual internal value.- Parameters:
value
- the value
-
notifyListeners
protected final void notifyListeners()Notifies listeners that the underlying value has changed or at least that it may have changed
-