Interface ThrowingSupplier<T>

Type Parameters:
T - The value type.
All Superinterfaces:
Supplier<T>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface ThrowingSupplier<T> extends Supplier<T>
A supplier of a value, but declaring any throwables that might be thrown. This class extends Supplier for inter-op. When Supplier.get() is called,
  • Method Summary

    Modifier and Type
    Method
    Description
    default Container<T>
    Creates a new lazily loaded Container using Container.lazy(Supplier) with this supplier as the loader function.
    default Result<T>
    Try to issue the value using issue() and return a successful result when successful, but catch any errors and return a failed result capturing them instead of rethrowing them.
    static <T> ThrowingSupplier<T>
    constant(T value)
    Create a new throwing supplier, which just always returns the provided value.
    default T
    get()
    Get the value by calling the issue() method, and sneakily rethrow any errors to eliminate a throws ... declaration.
    The throwing supplier method.
    static <T> ThrowingSupplier<T>
    of(ThrowingSupplier<T> supplier)
    Helper method to avoid casting when trying to provide a throwing supplier to a method taking in a Supplier.
    default T
    Try to issue the value using issue(), but catch any errors and return null instead of rethrowing them.
  • Method Details

    • of

      static <T> ThrowingSupplier<T> of(ThrowingSupplier<T> supplier)
      Helper method to avoid casting when trying to provide a throwing supplier to a method taking in a Supplier. Example: lazy(ThrowingSupplier.of(() -> ...)) instead of lazy((ThrowingSupplier)() ->
      Type Parameters:
      T - The value type.
      Parameters:
      supplier - The supplier.
      Returns:
      The supplier.
    • constant

      static <T> ThrowingSupplier<T> constant(T value)
      Create a new throwing supplier, which just always returns the provided value. This supplier will never throw an error.
      Type Parameters:
      T - The value type.
      Parameters:
      value - The constant value to supply.
      Returns:
      The supplier instance.
    • issue

      T issue() throws Throwable
      The throwing supplier method.
      Returns:
      The value.
      Throws:
      Throwable - Any errors may be thrown.
    • get

      default T get()
      Get the value by calling the issue() method, and sneakily rethrow any errors to eliminate a throws ... declaration. This method is overridden from Supplier.get(), which means this class can be used in exchange with Supplier.
      Specified by:
      get in interface Supplier<T>
      Returns:
      The value.
    • tryOrNull

      default T tryOrNull()
      Try to issue the value using issue(), but catch any errors and return null instead of rethrowing them.
      Returns:
      The value or null if an error occurred.
    • attempt

      default Result<T> attempt()
      Try to issue the value using issue() and return a successful result when successful, but catch any errors and return a failed result capturing them instead of rethrowing them.
      Returns:
      The result.
    • asContainer

      default Container<T> asContainer()
      Creates a new lazily loaded Container using Container.lazy(Supplier) with this supplier as the loader function.
      Returns:
      The container instance.