Module lettuce.core

Class Value<V>

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 Details

    • Value

      protected Value()
      Serializable constructor.
    • Value

      protected Value​(V value)
      Parameters:
      value - the value, may be null.
  • Method Details

    • from

      public static <T extends V,​ V> Value<V> from​(Optional<T> optional)
      Creates a Value from an Optional. The resulting value contains the value from the Optional if a value is present. Value is empty if the Optional is empty.
      Parameters:
      optional - the optional. May be empty but never null.
      Returns:
      the Value.
    • fromNullable

      public static <T extends V,​ V> Value<V> fromNullable​(T value)
      Creates a Value from a value. The resulting value contains the value if the value is not null.
      Parameters:
      value - the value. May be null.
      Returns:
      the Value.
    • empty

      public static <V> Value<V> empty()
      Returns an empty Value instance. No value is present for this instance.
      Returns:
      the Value.
    • just

      public static <T extends V,​ V> Value<V> just​(T value)
      Creates a Value from a value. The resulting value contains the value.
      Parameters:
      value - the value. Must not be null.
      Returns:
      the Value.
    • equals

      public boolean equals​(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getValue

      public V getValue()
      If a value is present in this Value, returns the value, otherwise throws NoSuchElementException.
      Returns:
      the non-null value held by this Optional.
      Throws:
      NoSuchElementException - if there is no value present.
      See Also:
      hasValue()
    • hasValue

      public boolean hasValue()
      Return true if there is a value present, otherwise false.
      Returns:
      true if there is a value present, otherwise false.
    • isEmpty

      public boolean isEmpty()
      Return true if there is no value present, otherwise false.
      Returns:
      true if there is no value present, otherwise false.
      Since:
      6.1
    • getValueOrElseGet

      public V getValueOrElseGet​(Supplier<V> otherSupplier)
      Return the value if present, otherwise invoke other and return the result of that invocation.
      Parameters:
      otherSupplier - a Supplier whose result is returned if no value is present. Must not be null.
      Returns:
      the value if present otherwise the result of other.get().
      Throws:
      NullPointerException - if value is not present and other is null.
    • getValueOrElse

      public V getValueOrElse​(V other)
      Return the value if present, otherwise return other.
      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 Throwable
      Return 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 be null.
      Returns:
      the present value
      Throws:
      X - if there is no value present
    • map

      public <R> Value<R> map​(Function<? super V,​? extends R> mapper)
      Returns a Value consisting of the results of applying the given function to the value of this element. Mapping is performed only if a value 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

      public void ifHasValue​(Consumer<? super V> consumer)
      If a value is present, invoke the specified Consumer with the value, otherwise do nothing.
      Parameters:
      consumer - block to be executed if a value is present, must not be null.
    • ifHasValueOrElse

      public void ifHasValueOrElse​(Consumer<? super V> consumer, Runnable emptyAction)
      If a value is present, invoke the specified Consumer with the value, otherwise call emptyAction.
      Parameters:
      consumer - block to be executed if a value is present, must not be null.
      emptyAction - block to be executed if a value is absent, must not be null.
      Since:
      6.1
    • ifEmpty

      public void ifEmpty​(Runnable runnable)
      If no value is present, invoke the specified Runnable, otherwise do nothing.
      Parameters:
      runnable - block to be executed if no value value is present, must not be null.
    • optional

      public Optional<V> optional()
      Returns an Optional wrapper for the value.
      Returns:
      Optional wrapper for the value.
    • stream

      public Stream<V> stream()
      Returns a Stream wrapper for the value. The resulting stream contains either the value if a this value has a value or it is empty if the value is empty.
      Returns:
      Stream wrapper for the value.