java.lang.Object
io.lettuce.core.Value<V>
- Type Parameters:
V- Value type.
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
GeoValue,KeyValue,ScoredValue
public class Value<V> extends Object implements Serializable
A value container object which may or may not contain a non-null value. If a value is present,
isPresent() will
return true and get() will return the value.
Additional methods that depend on the presence or absence of a contained value are provided, such as
getValueOrElse() (return a default value if value not present).
- Author:
- Mark Paluch
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Modifier Constructor Description protectedValue()Serializableconstructor.protectedValue(V value) -
Method Summary
Modifier and Type Method Description static <V> Value<V>empty()Returns an emptyValueinstance.booleanequals(Object o)static <T extends V, V>
Value<V>from(Optional<T> optional)static <T extends V, V>
Value<V>fromNullable(T value)Creates aValuefrom avalue.VgetValue()If a value is present in thisValue, returns the value, otherwise throwsNoSuchElementException.VgetValueOrElse(V other)Return the value if present, otherwise returnother.VgetValueOrElseGet(Supplier<V> otherSupplier)Return the value if present, otherwise invokeotherand return the result of that invocation.<X extends Throwable>
VgetValueOrElseThrow(Supplier<? extends X> exceptionSupplier)Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.inthashCode()booleanhasValue()Returntrueif there is a value present, otherwisefalse.voidifEmpty(Runnable runnable)If no value is present, invoke the specifiedRunnable, otherwise do nothing.voidifHasValue(Consumer<? super V> consumer)If a value is present, invoke the specifiedConsumerwith the value, otherwise do nothing.voidifHasValueOrElse(Consumer<? super V> consumer, Runnable emptyAction)If a value is present, invoke the specifiedConsumerwith the value, otherwise callemptyAction.booleanisEmpty()Returntrueif there is no value present, otherwisefalse.static <T extends V, V>
Value<V>just(T value)Creates aValuefrom avalue.<R> Value<R>map(Function<? super V,? extends R> mapper)Returns aValueconsisting of the results of applying the given function to the value of this element.Optional<V>optional()Returns anOptionalwrapper for the value.Stream<V>stream()Returns aStreamwrapper for the value.StringtoString()
-
Constructor Details
-
Value
protected Value()Serializableconstructor. -
Value
- Parameters:
value- the value, may benull.
-
-
Method Details
-
from
Creates aValuefrom anOptional. The resulting value contains the value from theOptionalif a value is present. Value is empty if theOptionalis empty.- Parameters:
optional- the optional. May be empty but nevernull.- Returns:
- the
Value.
-
fromNullable
- Parameters:
value- the value. May benull.- Returns:
- the
Value.
-
empty
Returns an emptyValueinstance. No value is present for this instance.- Returns:
- the
Value.
-
just
Creates aValuefrom avalue. The resulting value contains the value.- Parameters:
value- the value. Must not benull.- Returns:
- the
Value.
-
equals
-
hashCode
public int hashCode() -
toString
-
getValue
If a value is present in thisValue, returns the value, otherwise throwsNoSuchElementException.- Returns:
- the non-null value held by this
Optional. - Throws:
NoSuchElementException- if there is no value present.- See Also:
hasValue()
-
hasValue
public boolean hasValue()Returntrueif there is a value present, otherwisefalse.- Returns:
trueif there is a value present, otherwisefalse.
-
isEmpty
public boolean isEmpty()Returntrueif there is no value present, otherwisefalse.- Returns:
trueif there is no value present, otherwisefalse.- Since:
- 6.1
-
getValueOrElseGet
Return the value if present, otherwise invokeotherand return the result of that invocation.- Parameters:
otherSupplier- aSupplierwhose result is returned if no value is present. Must not benull.- Returns:
- the value if present otherwise the result of
other.get(). - Throws:
NullPointerException- if value is not present andotheris null.
-
getValueOrElse
Return the value if present, otherwise returnother.- Parameters:
other- the value to be returned if there is no value present, may be null.- Returns:
- the value, if present, otherwise
other.
-
getValueOrElseThrow
public <X extends Throwable> V getValueOrElseThrow(Supplier<? extends X> exceptionSupplier) throws X extends ThrowableReturn the contained value, if present, otherwise throw an exception to be created by the provided supplier.- Type Parameters:
X- Type of the exception to be thrown- Parameters:
exceptionSupplier- The supplier which will return the exception to be thrown, must not benull.- Returns:
- the present value
- Throws:
X- if there is no value present
-
map
Returns aValueconsisting of the results of applying the given function to the value of this element. Mapping is performed only if avalue is present.- Type Parameters:
R- The element type of the new value.- Parameters:
mapper- a stateless function to apply to each element.- Returns:
- the new
Value.
-
ifHasValue
If a value is present, invoke the specifiedConsumerwith the value, otherwise do nothing.- Parameters:
consumer- block to be executed if a value is present, must not benull.
-
ifHasValueOrElse
If a value is present, invoke the specifiedConsumerwith the value, otherwise callemptyAction.- Parameters:
consumer- block to be executed if a value is present, must not benull.emptyAction- block to be executed if a value is absent, must not benull.- Since:
- 6.1
-
ifEmpty
If no value is present, invoke the specifiedRunnable, otherwise do nothing.- Parameters:
runnable- block to be executed if no value value is present, must not benull.
-
optional
Returns anOptionalwrapper for the value.- Returns:
Optionalwrapper for the value.
-
stream
Returns aStreamwrapper for the value. The resulting stream contains either the value if a this valuehas a valueor it is empty if the value is empty.- Returns:
Streamwrapper for the value.
-