Class Value<T>

java.lang.Object
org.apache.juneau.Value<T>
Type Parameters:
T - The value type.

public class Value<T> extends Object
Represents a simple settable value.

Similar to an Optional but mutable.

Notes:
  • This class is not thread safe.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor.
    Value(T t)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Value<T>
    Static creator.
    get()
    Returns the value.
    Returns the value and then unsets it.
    static Type
    Returns the generic parameter type of the Value type.
    void
    ifPresent(Consumer<? super T> consumer)
    If a value is present, invoke the specified consumer with the value, otherwise do nothing.
    boolean
    Returns true if the value is empty.
    boolean
    Returns true if the value is set.
    static boolean
    Convenience method for checking if the specified type is this class.
    Adds a listener for this value.
    <T2> Value<T2>
    map(Function<? super T,T2> mapper)
    Applies a mapping function against the contents of this value.
    static <T> Value<T>
    of(T object)
    Static creator.
    orElse(T def)
    Returns the contents of this value or the default value if null.
    orElseGet(Supplier<? extends T> other)
    Return the value if present, otherwise invoke other and return the result of that invocation.
    <X extends Throwable>
    T
    orElseThrow(Supplier<? extends X> exceptionSupplier)
    Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.
    set(T t)
    Sets the value.
    Sets the value if it's not already set.
     
    static Type
    Returns the unwrapped type.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Value

      public Value()
      Constructor.
    • Value

      public Value(T t)
      Constructor.
      Parameters:
      t - Initial value.
  • Method Details

    • of

      public static <T> Value<T> of(T object)
      Static creator.
      Type Parameters:
      T - The value type.
      Parameters:
      object - The object being wrapped.
      Returns:
      A new Value object.
    • empty

      public static <T> Value<T> empty()
      Static creator.
      Type Parameters:
      T - The value type.
      Returns:
      An empty Value object.
    • getParameterType

      public static Type getParameterType(Type t)
      Returns the generic parameter type of the Value type.
      Parameters:
      t - The type to find the parameter type of.
      Returns:
      The parameter type of the value, or null if the type is not a subclass of Value.
    • unwrap

      public static Type unwrap(Type t)
      Returns the unwrapped type.
      Parameters:
      t - The type to unwrap.
      Returns:
      The unwrapped type, or the same type if the type isn't Value.
    • isType

      public static boolean isType(Type t)
      Convenience method for checking if the specified type is this class.
      Parameters:
      t - The type to check.
      Returns:
      true if the specified type is this class.
    • listener

      public Value<T> listener(ValueListener<T> listener)
      Adds a listener for this value.
      Parameters:
      listener - The new listener for this value.
      Returns:
      This object.
    • set

      public Value<T> set(T t)
      Sets the value.
      Parameters:
      t - The new value.
      Returns:
      This object.
    • setIfEmpty

      public Value<T> setIfEmpty(T t)
      Sets the value if it's not already set.
      Parameters:
      t - The new value.
      Returns:
      This object.
    • get

      public T get()
      Returns the value.
      Returns:
      The value, or null if it is not set.
    • getAndUnset

      public T getAndUnset()
      Returns the value and then unsets it.
      Returns:
      The value before it was unset.
    • isPresent

      public boolean isPresent()
      Returns true if the value is set.
      Returns:
      true if the value is set.
    • ifPresent

      public void ifPresent(Consumer<? super T> 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.
    • map

      public <T2> Value<T2> map(Function<? super T,T2> mapper)
      Applies a mapping function against the contents of this value.
      Type Parameters:
      T2 - The mapped value type.
      Parameters:
      mapper - The mapping function.
      Returns:
      The mapped value.
    • orElse

      public T orElse(T def)
      Returns the contents of this value or the default value if null.
      Parameters:
      def - The default value.
      Returns:
      The contents of this value or the default value if null.
    • isEmpty

      public boolean isEmpty()
      Returns true if the value is empty.
      Returns:
      true if the value is empty.
    • orElseGet

      public T orElseGet(Supplier<? extends T> other)
      Return the value if present, otherwise invoke other and return the result of that invocation.
      Parameters:
      other - a Supplier whose result is returned if no value is present
      Returns:
      the value if present otherwise the result of other.get()
      Throws:
      NullPointerException - if value is not present and other is null
    • orElseThrow

      public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
      Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.
      Type Parameters:
      X - The exception type.
      Parameters:
      exceptionSupplier - The supplier which will return the exception to be thrown
      Returns:
      the present value
      Throws:
      X - if there is no value present
      NullPointerException - if no value is present and exceptionSupplier is null
    • toString

      public String toString()
      Overrides:
      toString in class Object