- Type Parameters:
T- the type of the wrapped value
- All Superinterfaces:
Consumer<T>,EventObserver<T>,Supplier<T>,ValueObserver<T>
- All Known Subinterfaces:
PropertyValue<T>,State,ValueSet<T>
- All Known Implementing Classes:
AbstractValue
An observable wrapper for a value.
A factory class for
Value instances.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumSpecifies when a Value instance notifies its listeners.static interfaceAValue.ValidatorforValues. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidSets the valuebooleanaddValidator(Value.Validator<T> validator) Adds a validator to thisValue.voidCreates a bidirectional link between this and the given original value, so that changes in one are reflected in the other.voidlink(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.default booleanSets a new value mapped from the current value.default booleanSets a new value in case the current value is null.observer()Returns aValueObservernotified each time this value changes.booleanremoveValidator(Value.Validator<T> validator) Removes the given validator from this valuebooleanSets the value.voidUnlinks this value from the given original valuevoidunlink(ValueObserver<T> originalValue) Unlinks this value from the given original value observervoidValidate the given value using all validatorsstatic <T> Value<T>value()Creates a newValueinstance, wrapping a null initial value, usingValue.Notify.WHEN_CHANGED.static <T> Value<T>value(Value.Notify notify) Creates a newValueinstance, wrapping a null initial valuestatic <T> Value<T>value(T initialValue) Creates a newValueinstance, usingValue.Notify.WHEN_CHANGED.static <T> Value<T>value(T initialValue, Value.Notify notify) Creates a newValueinstancestatic <T> Value<T>value(T initialValue, T nullValue) Creates a newValueinstance, usingValue.Notify.WHEN_CHANGED.static <T> Value<T>value(T initialValue, T nullValue, Value.Notify notify) Creates a newValueinstanceMethods inherited from interface is.codion.common.event.EventObserver
addDataListener, addListener, addWeakDataListener, addWeakListener, removeDataListener, removeListener, removeWeakDataListener, removeWeakListenerMethods inherited from interface is.codion.common.value.ValueObserver
isEqualTo, isNotEqualTo, isNotNull, isNull, nullable, optional
-
Method Details
-
accept
Sets the value- Specified by:
acceptin interfaceConsumer<T>- Parameters:
value- the value- Throws:
IllegalArgumentException- in case the given value is invalid- See Also:
-
set
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.- Parameters:
value- the value- Returns:
- true if the underlying value changed
- Throws:
IllegalArgumentException- in case the given value is invalid- See Also:
-
map
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);- Parameters:
mapper- maps from the current value to a new value- Returns:
- true if the underlying value changed
- Throws:
NullPointerException- in casemapperis null
-
mapNull
Sets a new value in case the current value is null.Value<Integer> value = Value.value(null); // replace null with 1 value.mapNull(() -> 1); // has no effect since the value is non-null value.mapNull(() -> 2);- Parameters:
supplier- supplies the value to use as replacement for null- Returns:
- true if the underlying value changed
- Throws:
NullPointerException- in casesupplieris null
-
observer
ValueObserver<T> observer()Returns aValueObservernotified each time this value changes.- Returns:
- a
ValueObserverfor this value
-
link
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.- Parameters:
originalValue- the original value to link this value to- Throws:
IllegalStateException- in case the values are already linkedIllegalArgumentException- in case the original value is not valid according to this values validators
-
unlink
Unlinks this value from the given original value- Parameters:
originalValue- the original value to unlink from this one- Throws:
IllegalStateException- in case the values are not linked
-
link
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.- Parameters:
originalValue- the original value to link this value to- Throws:
IllegalArgumentException- in case the original value is not valid according to this values validators
-
unlink
Unlinks this value from the given original value observer- Parameters:
originalValue- the original value to unlink
-
addValidator
Adds a validator to thisValue. Adding the same validator again has no effect.- Parameters:
validator- the validator- Returns:
- true if this value did not already contain the specified validator
- Throws:
IllegalArgumentException- in case the current value is invalid according to the validator
-
removeValidator
Removes the given validator from this value- Parameters:
validator- the validator- Returns:
- true if this value contained the specified validator
-
validate
Validate the given value using all validators- Parameters:
value- the value to validate- Throws:
IllegalArgumentException- in case the given value is invalid according to a validator
-
value
Creates a newValueinstance, wrapping a null initial value, usingValue.Notify.WHEN_CHANGED.- Type Parameters:
T- the value type- Returns:
- a Value for the given type
-
value
Creates a newValueinstance, wrapping a null initial value- Type Parameters:
T- the value type- Parameters:
notify- specifies when this value notifies its listeners- Returns:
- a Value for the given type
-
value
Creates a newValueinstance, usingValue.Notify.WHEN_CHANGED.- Type Parameters:
T- the value type- Parameters:
initialValue- the initial value- Returns:
- a
Valuewith given initial value
-
value
Creates a newValueinstance- Type Parameters:
T- the value type- Parameters:
initialValue- the initial valuenotify- specifies when this value notifies its listeners- Returns:
- a
Valuewith given initial value
-
value
Creates a newValueinstance, usingValue.Notify.WHEN_CHANGED.- Type Parameters:
T- the value type- Parameters:
initialValue- the initial valuenullValue- the actual value to use when the value is set to null- Returns:
- a
Valuewith given initial value - Throws:
NullPointerException- in casenullValueis null
-
value
Creates a newValueinstance- Type Parameters:
T- the value type- Parameters:
initialValue- the initial valuenullValue- the actual value to use when the value is set to nullnotify- specifies when this value notifies its listeners- Returns:
- a
Valuewith given initial value - Throws:
NullPointerException- in casenullValueis null
-