java.lang.Object
is.codion.common.value.AbstractValue<T>
- Type Parameters:
T
- the value type
- All Implemented Interfaces:
Observer<T>
,Value<T>
,ValueObserver<T>
,Consumer<T>
- Direct Known Subclasses:
AbstractComponentValue
An abstract
Value
implementation handling everything except the value itself.
The constructor parameter notify
specifies whether this Value
instance should call
notifyListeners()
when the value is set or changed via set(Object)
.
Implementations that want to handle notifications manually should use the
AbstractValue()
or AbstractValue(Object)
constructors.
-
Nested Class Summary
Nested classes/interfaces inherited from interface is.codion.common.value.Value
Value.Builder<T,
B extends Value.Builder<T, B>>, Value.BuilderFactory, Value.Notify, Value.Validator<T> -
Constructor Summary
ModifierConstructorDescriptionprotected
Creates aAbstractValue
instance, which does not notify listeners.protected
AbstractValue
(T nullValue) Creates aAbstractValue
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 thisObserver
.final boolean
addListener
(Runnable listener) Addslistener
to thisObserver
.final boolean
addValidator
(Value.Validator<? super 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 ValueObserver<T>
final T
get()
protected abstract T
getValue()
Returns the actual internal value.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.observer()
Returns aValueObserver
notified each time this value changes.final boolean
removeConsumer
(Consumer<? super T> consumer) Removesconsumer
from thisObserver
final boolean
removeListener
(Runnable listener) Removeslistener
from thisObserver
final boolean
removeValidator
(Value.Validator<? super T> validator) Removes the given validator from this valuefinal boolean
removeWeakConsumer
(Consumer<? super T> consumer) Removesconsumer
from thisObserver
.final boolean
removeWeakListener
(Runnable listener) Removeslistener
from thisObserver
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()Creates aAbstractValue
instance, which does not notify listeners. -
AbstractValue
Creates aAbstractValue
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
-
get
- Specified by:
get
in interfaceValueObserver<T>
- Returns:
- the value
-
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
-
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. Note that change listener notifications depend on theValue.Notify
policy associated with this value. -
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
-
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 -
getValue
Returns the actual internal value.- Returns:
- the value
-
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 -
createObserver
- Returns:
- a new
ValueObserver
instance representing this value
-