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.
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 TypeMethodDescriptionCreates a new lazily loadedContainerusingContainer.lazy(Supplier)with this supplier as the loader function.attempt()Try to issue the value usingissue()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 Tget()Get the value by calling theissue()method, and sneakily rethrow any errors to eliminate athrows ...declaration.issue()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 aSupplier.default TTry to issue the value usingissue(), but catch any errors and return null instead of rethrowing them.
-
Method Details
-
of
Helper method to avoid casting when trying to provide a throwing supplier to a method taking in aSupplier. Example:lazy(ThrowingSupplier.of(() -> ...))instead oflazy((ThrowingSupplier)() ->- Type Parameters:
T- The value type.- Parameters:
supplier- The supplier.- Returns:
- The supplier.
-
constant
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
The throwing supplier method.- Returns:
- The value.
- Throws:
Throwable- Any errors may be thrown.
-
get
Get the value by calling theissue()method, and sneakily rethrow any errors to eliminate athrows ...declaration. This method is overridden fromSupplier.get(), which means this class can be used in exchange withSupplier. -
tryOrNull
Try to issue the value usingissue(), but catch any errors and return null instead of rethrowing them.- Returns:
- The value or null if an error occurred.
-
attempt
Try to issue the value usingissue()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
Creates a new lazily loadedContainerusingContainer.lazy(Supplier)with this supplier as the loader function.- Returns:
- The container instance.
-