java.lang.Object
com.github.nullterminated.trylambda.Try
A utility class for handling try blocks using lambda expressions.
- Author:
- Ramsey Gurley
-
Method Summary
Modifier and TypeMethodDescriptioneither(CheckedSupplier<R> supplier) This method exists to allow usage ofeitherwith checked suppliers without casting or needing to declare the checked supplier explicitly.Wraps the call togetin a try block.static <T extends AutoCloseable,R>
CheckedSupplier<R> trys(CheckedSupplier<T> supplier, Function<Exception, CheckedSupplier<R>> errorHandler, Function<T, CheckedSupplier<R>> function) A method to use auto closing try with resources with lambda parameters.static <T extends AutoCloseable,R>
CheckedSupplier<R> trys(CheckedSupplier<T> supplier, Function<T, CheckedSupplier<R>> function) Calls the three argument form of this method with an error handler which simply wraps any exception in aWrappedException.
-
Method Details
-
trys
public static <T extends AutoCloseable,R> CheckedSupplier<R> trys(CheckedSupplier<T> supplier, Function<Exception, CheckedSupplier<R>> errorHandler, Function<T, CheckedSupplier<R>> function) A method to use auto closing try with resources with lambda parameters. The supplier argument supplies the AutoCloseable resource of the try block, which is passed to the function argument in the body of the try block. The result of the function argument is rewrapped in a new supplier so that calls to this method may be chained. If aWrappedExceptionis thrown by the supplier or function arguments, it is rethrown. All other exceptions are passed to the errorHandler argument for handling.- Type Parameters:
T- the supplied AutoCloseable typeR- the supplied result type- Parameters:
supplier- the AutoCloseable suppliererrorHandler- a function to handle exceptions thrown by the supplier or function argumentsfunction- a function that accepts the supplied AutoCloseable type and returns a supplier of the result type- Returns:
- a supplier of the result type
-
trys
public static <T extends AutoCloseable,R> CheckedSupplier<R> trys(CheckedSupplier<T> supplier, Function<T, CheckedSupplier<R>> function) Calls the three argument form of this method with an error handler which simply wraps any exception in aWrappedException.- Type Parameters:
T- the supplied AutoCloseable typeR- the supplied result type- Parameters:
supplier- the AutoCloseable supplierfunction- a function that accepts the supplied AutoCloseable type and returns a supplier of the result type- Returns:
- a supplier of the result type
-
either
Wraps the call togetin a try block. Either an exception thrown by the supplier or the supplied value is returned. If aWrappedExceptionis thrown, its cause is returned.- Type Parameters:
R- the supplied value type- Parameters:
supplier- the value supplier- Returns:
- either an exception thrown by the supplier or the supplied value
-
either
This method exists to allow usage ofeitherwith checked suppliers without casting or needing to declare the checked supplier explicitly.- Type Parameters:
R- the supplied value type- Parameters:
supplier- the supplier function- Returns:
- either an exception or the supplied value
-