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.Builder<T,
B extends Value.Builder<T, B>>, 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 value.final boolean
addConsumer
(Consumer<? super T> consumer) Addsconsumer
to thisEventObserver
.final boolean
addListener
(Runnable listener) Addslistener
to thisEventObserver
.final boolean
addValidator
(Value.Validator<T> validator) Adds a validator to thisValue
.final boolean
addWeakConsumer
(Consumer<? super T> consumer) Uses aWeakReference
, addingconsumer
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
clear()
Clears this value, by setting it to null or the null value in case this is a non-null value.protected void
Clears this value by setting it to null.protected ValueObserver<T>
final boolean
If false then get() is guaranteed to never return null.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 changedobserver()
Returns aValueObserver
notified each time this value changes.final boolean
removeConsumer
(Consumer<? super T> consumer) Removesconsumer
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
removeWeakConsumer
(Consumer<? super T> consumer) Removesconsumer
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
isEqualTo, isNotEqualTo, isNotNull, isNull, 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 change listener notifications depend on theValue.Notify
policy associated with this value. -
clear
public final void clear()Description copied from interface:Value
Clears this value, by setting it to null or the null value in case this is a non-null value. -
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
-
isNullable
public final boolean isNullable()Description copied from interface:ValueObserver
If false then get() is guaranteed to never return null.- Specified by:
isNullable
in interfaceValueObserver<T>
- Returns:
- true if this value can be null
-
accept
Description copied from interface:Value
Sets the value. Note that change listener notifications depend on theValue.Notify
policy associated with this 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
-
addConsumer
Description copied from interface:EventObserver
Addsconsumer
to thisEventObserver
. Adding the same consumer a second time has no effect.- Specified by:
addConsumer
in interfaceEventObserver<T>
- Parameters:
consumer
- the consumer to add- Returns:
- true if this observer did not already contain the specified consumer
-
removeConsumer
Description copied from interface:EventObserver
Removesconsumer
from thisEventObserver
- Specified by:
removeConsumer
in interfaceEventObserver<T>
- Parameters:
consumer
- the consumer to remove- Returns:
- true if this observer contained the specified consumer
-
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
-
addWeakConsumer
Description copied from interface:EventObserver
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 interfaceEventObserver<T>
- Parameters:
consumer
- the consumer- Returns:
- true if this observer did not already contain the specified consumer
-
removeWeakConsumer
Description copied from interface:EventObserver
Removesconsumer
from thisEventObserver
.- Specified by:
removeWeakConsumer
in interfaceEventObserver<T>
- Parameters:
consumer
- the consumer to remove- Returns:
- true if this observer contained the specified consumer
-
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
-
clearValue
protected void clearValue()Clears this value by setting it to null. -
notifyListeners
protected final void notifyListeners()Notifies listeners that the underlying value has changed or at least that it may have changed -
createObserver
- Returns:
- a new
ValueObserver
instance representing this value
-