- Type Parameters:
T
- the type of the wrapped value
- All Superinterfaces:
Observable<T>
,Observer<T>
- All Known Subinterfaces:
ComponentValue<T,
,C> EntityEditModel.ValueEditor<T>
,EntityQueryModel.AdditionalCondition
,PropertyValue<T>
,State
,ValueList<T>
,Values<T,
,C> ValueSet<T>
- All Known Implementing Classes:
AbstractComponentValue
,AbstractTextComponentValue
,AbstractValue
An observable wrapper for a value.
Nullable integer based Value:
Value<Integer> value = Value.nullable();
value.set(42);
value.addConsumer(this::onValueChange);
value.isNullable(); // true
Non-null boolean based Value, using 'false' as a null substitute:
Value<Boolean> value = Value.nonNull(false);
value.set(true);
value.set(null);
value.get(); // false
value.isNullable(); // false
Non-null String based Value, using "none" as a null substitute:
Value<String> value = Value.builder()
.nonNull("none")
.value("hello") // the initial value
.notify(Notify.WHEN_SET) // notifies listeners when set
.validator(this::validateString) // using a validator
.listener(this::onStringSet) // and a listener
.build();
value.isNullable();// false
value.set("hey");
value.set(null); // reverts to the null substitute: "none"
A factory for Value
instances.
- See Also:
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Value.Builder<T,
B extends Value.Builder<T, B>> Builds aValue
instance.static interface
ProvidesValue.Builder
instances for nullable or non-nullable values.static enum
Specifies when a Value instance notifies its listeners.static interface
AValue.Validator
forValue
s. -
Method Summary
Modifier and TypeMethodDescriptionboolean
addValidator
(Value.Validator<? super T> validator) Adds a validator to thisValue
.static Value.BuilderFactory
builder()
void
clear()
Clears this value, by setting it to null or the null value in case this is a non-null value.void
link
(Observable<T> observable) Creates a unidirectional link between this value and the given observable, so that changes in the observable are reflected in this one.void
Creates a bidirectional link between this and the given original value, so that changes in one are reflected in the other.default boolean
Sets a new value mapped from the current value.static <T> Value<T>
nonNull
(T nullValue) Creates a new non-nullValue
instance, using the given value as a null-substitute, usingValue.Notify.WHEN_CHANGED
.static <T> Value<T>
nullable()
Creates a new nullableValue
instance, wrapping a null initial value, usingValue.Notify.WHEN_CHANGED
.static <T> Value<T>
Creates a new nullableValue
instance, wrapping the given initial value, usingValue.Notify.WHEN_CHANGED
.boolean
removeValidator
(Value.Validator<? super T> validator) Removes the given validator from this valueboolean
Sets the value.void
unlink
(Observable<T> observable) Unlinks this value from the given observablevoid
Unlinks this value from the given original valuevoid
Validate the given value using all validatorsMethods inherited from interface is.codion.common.observable.Observable
addConsumer, addListener, addWeakConsumer, addWeakListener, get, getOrThrow, getOrThrow, isEqualTo, isNotEqualTo, isNull, isNullable, observer, optional, removeConsumer, removeListener, removeWeakConsumer, removeWeakListener
-
Method Details
-
set
Sets the value. Note that change listener notifications depend on theValue.Notify
policy associated with this value.- Parameters:
value
- the value- Returns:
- true if the underlying value changed
- Throws:
IllegalArgumentException
- in case the given value is invalid- See Also:
-
clear
void clear()Clears this value, by setting it to null or the null value in case this is a non-null value. -
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 casemapper
is null
-
observable
Observable<T> observable()- Returns:
- a read-only
Observable
instance for 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 linked or if a cycle is detectedIllegalArgumentException
- 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 observable, so that changes in the observable are reflected in this one. Note that after a call to this method the value of this value is the same as the observable.- Parameters:
observable
- the observable to link this value to- Throws:
IllegalArgumentException
- in case the observable is not valid according to this values validators
-
unlink
Unlinks this value from the given observable- Parameters:
observable
- the observable 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
-
nullable
Creates a new nullableValue
instance, wrapping a null initial value, usingValue.Notify.WHEN_CHANGED
.- Type Parameters:
T
- the value type- Returns:
- a nullable Value
-
nullable
Creates a new nullableValue
instance, wrapping the given initial value, usingValue.Notify.WHEN_CHANGED
.- Type Parameters:
T
- the value type- Parameters:
value
- the initial value- Returns:
- a nullable Value
-
nonNull
Creates a new non-nullValue
instance, using the given value as a null-substitute, usingValue.Notify.WHEN_CHANGED
.- Type Parameters:
T
- the value type- Parameters:
nullValue
- the null value substitute- Returns:
- a non-null Value
-
builder
- Returns:
- a new
Value.BuilderFactory
instance
-