Interface ValueContainer<T>

Type Parameters:
T - type of stored value
All Superinterfaces:
Supplier<T>
All Known Implementing Classes:
ValueContainer.Containing, ValueContainer.Empty, ValueContainer.OfNull, ValueContainer.Supplying

public interface ValueContainer<T> extends Supplier<T>
A value which may be present (which includes null) or not-present.

This differs from Optional as presence of a null value does not make this value container empty.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    A simple value-container which is meant to keep one unchanged value.
    static final class 
    Empty value-container.
    static class 
    An exception thrown whenever get() is called on an empty value container.
    static final class 
    Non-empty value-container containing null.
    static final class 
    Value-container which provides new values for each request.
  • Method Summary

    Modifier and Type
    Method
    Description
    <E> @NotNull Result<T,@Nullable E>
    Converts this value container into a null-error result.
    <E> @NotNull Result<T,E>
    asResult(Supplier<E> errorSupplier)
    Converts this value container into a Result.
    static <T> @NotNull ValueContainer<T>
    Gets a value container being empty.
    get()
    Gets the value stored in this value container.
    boolean
    Checks whether the object is present ot not.
    static <T> @NotNull ValueContainer<T>
    nonnullOrEmpty(T value)
    Gets a value container containing the specified value or an empty one if the values is null.
    static <T> @NotNull ValueContainer<T>
    of(T value)
    Gets a non-empty value container containing the specified value.
    static <T> @NotNull ValueContainer<T>
    Gets a non-empty value container containing null.
    orElse(T value)
    Gets the value if it is present otherwise using the specified one.
    orElseGet(@NonNull Supplier<T> defaultValueSupplier)
    Gets the value if it is present otherwise using the one got from the specified supplier.
    default <X extends Throwable>
    T
    orElseSneakyThrow(@NonNull Supplier<X> exceptionSupplier)
    Gets the value if it is present otherwise throwing an exception.
    <X extends Throwable>
    T
    orElseThrow(@NonNull Supplier<X> exceptionSupplier)
    Gets the value if it is present otherwise throwing an exception.
  • Method Details

    • empty

      @NotNull static <T> @NotNull ValueContainer<T> empty()
      Gets a value container being empty.
      Type Parameters:
      T - type of stored value
      Returns:
      empty value container
    • ofNull

      @NotNull static <T> @NotNull ValueContainer<T> ofNull()
      Gets a non-empty value container containing null.
      Type Parameters:
      T - type of stored value
      Returns:
      value container containing null
    • of

      @NotNull static <T> @NotNull ValueContainer<T> of(@Nullable T value)
      Gets a non-empty value container containing the specified value.
      Type Parameters:
      T - type of stored value
      Parameters:
      value - nullable value stored in the container
      Returns:
      non-empty value container containing the specified value
    • nonnullOrEmpty

      @NotNull static <T> @NotNull ValueContainer<T> nonnullOrEmpty(@Nullable T value)
      Gets a value container containing the specified value or an empty one if the values is null.
      Type Parameters:
      T - type of stored value
      Parameters:
      value - value stored in the container
      Returns:
      non-empty value container containing the specified value if it is not null and an empty one otherwise
    • get

      T get()
      Gets the value stored in this value container.
      Specified by:
      get in interface Supplier<T>
      Returns:
      stored value if it is present
      Throws:
      ValueContainer.EmptyValueException - if this value container is empty
    • isPresent

      boolean isPresent()
      Checks whether the object is present ot not.
      Returns:
      true if the value is present (might be null) and false otherwise
    • orElse

      @Nullable T orElse(@Nullable T value)
      Gets the value if it is present otherwise using the specified one.
      Parameters:
      value - value to use if this value container is empty
      Returns:
      this value container's value if it is not empty or the specified value otherwise
    • orElseGet

      @Nullable T orElseGet(@NonNull @NonNull Supplier<T> defaultValueSupplier)
      Gets the value if it is present otherwise using the one got from the specified supplier.
      Parameters:
      defaultValueSupplier - supplier to be used for getting the value if this value container is empty
      Returns:
      this value container's value if it is not empty or the one got from the supplier otherwise
    • orElseThrow

      @Nullable <X extends Throwable> T orElseThrow(@NonNull @NonNull Supplier<X> exceptionSupplier) throws X
      Gets the value if it is present otherwise throwing an exception.
      Type Parameters:
      X - type of an exception thrown if this value container is empty
      Parameters:
      exceptionSupplier - supplier to be used for getting the exception if this value container is empty
      Returns:
      this value container's value if it is not empty
      Throws:
      X - if this value container is empty
    • orElseSneakyThrow

      @Nullable default <X extends Throwable> T orElseSneakyThrow(@NonNull @NonNull Supplier<X> exceptionSupplier)
      Gets the value if it is present otherwise throwing an exception. Throws X if this value container is empty.
      Type Parameters:
      X - type of an exception thrown if this value container is empty
      Parameters:
      exceptionSupplier - supplier to be used for getting the exception if this value container is empty
      Returns:
      this value container's value if it is not empty
    • asResult

      @NotNull <E> @NotNull Result<T,@Nullable E> asResult()
      Converts this value container into a null-error result.
      Type Parameters:
      E - type of the result error
      Returns:
      successful result containing the value contained by this value container if is is present or a null-error result otherwise
      See Also:
    • asResult

      @NotNull <E> @NotNull Result<T,E> asResult(Supplier<E> errorSupplier)
      Converts this value container into a Result.
      Type Parameters:
      E - type of the result error
      Parameters:
      errorSupplier - supplier of the error in case of this value container being empty
      Returns:
      successful result containing the value contained by this value container if is is present or an error result with the error got by using the specified supplier
      See Also: