Interface ThrowingSupplier<T,X extends Throwable>

Type Parameters:
T - the type of results supplied by this supplier
X - the type of the exception thrown by the function
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,X extends Throwable> extends Supplier<T>
An extension of Supplier allowing throwing calls in its body.
  • Method Summary

    Modifier and Type
    Method
    Description
    default T
    get()
     
    Gets a result.
    static <T, @Any X extends Throwable>
    @NotNull ThrowingSupplier<T,X>
    returning(T value)
    Creates throwing supplier which always returns the provided value.
    static <@Any T, X extends Throwable>
    @NotNull ThrowingSupplier<T,X>
    throwing(@NonNull Supplier<? extends @NotNull X> exceptionFactory)
    Creates a throwing supplier which always throws an exception produced by using the factory.
  • Method Details

    • getChecked

      T getChecked() throws X
      Gets a result.
      Returns:
      a result
      Throws:
      X - if an exception happens
    • get

      default T get()
      Specified by:
      get in interface Supplier<T>
    • returning

      @NotNull static <T, @Any X extends Throwable> @NotNull ThrowingSupplier<T,X> returning(T value)
      Creates throwing supplier which always returns the provided value.
      Type Parameters:
      T - the type of the supplied result
      X - any formal type of the (never thrown) exception
      Parameters:
      value - the value always returned by this factory
      Returns:
      created throwing supplier
    • throwing

      @NotNull static <@Any T, X extends Throwable> @NotNull ThrowingSupplier<T,X> throwing(@NonNull @NonNull Supplier<? extends @NotNull X> exceptionFactory)
      Creates a throwing supplier which always throws an exception produced by using the factory.
      Type Parameters:
      T - any formal type of the (never returned) value
      X - the type of the exception thrown by the function
      Parameters:
      exceptionFactory - factory used for creation of the exception
      Returns:
      throwing runnable which always throws X by creating it via the provided factory
      Throws:
      NullPointerException - if exceptionFactory is null
      API note
      if the exceptionFactory produces null then NullPointerException will be thrown when attempting to throw the expected exception