Class CheckedAdapters
java.lang.Object
com.dudko.tools.safejavastreams.adapter.CheckedAdapters
Utility class for working with lambdas and stream functions that throw checked exceptions.
This enables using Stream API and functional interfaces in a safer and cleaner way without wrapping try/catch manually in every lambda.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static RuntimeException
Propagates a checked exception as unchecked RuntimeException.safeFunctionEither
(ThrowingFunction<T, R> function) Converts a ThrowingFunction into one that returnsEither<Exception, R>
.safeFunctionOptional
(ThrowingFunction<T, R> function) Converts a ThrowingFunction into one that returns Optional.static <T,
U> BiConsumer<T, U> wrapBiConsumer
(ThrowingBiConsumer<T, U> consumer) Wraps a ThrowingBiConsumer into a regular BiConsumer.static <T,
U, R> BiFunction<T, U, R> wrapBiFunction
(ThrowingBiFunction<T, U, R> function) Wraps a ThrowingBiFunction and rethrows as unchecked RuntimeException.static <T> Consumer<T>
wrapConsumer
(ThrowingConsumer<T> consumer) Wraps a ThrowingConsumer into a regular Consumer.static <T,
R> Function<T, R> wrapFunction
(ThrowingFunction<T, R> function) Wraps a ThrowingFunction and rethrows as unchecked RuntimeException.static Runnable
wrapRunnable
(ThrowingRunnable runnable) Wraps a ThrowingRunnable into a regular Runnable.static <T> Supplier<T>
wrapSupplier
(ThrowingSupplier<T> supplier) Wraps a ThrowingSupplier into a regular Supplier.
-
Constructor Details
-
CheckedAdapters
public CheckedAdapters()
-
-
Method Details
-
wrapFunction
Wraps a ThrowingFunction and rethrows as unchecked RuntimeException.- Type Parameters:
T
- input typeR
- result type- Parameters:
function
- lambda with checked exception- Returns:
- safe Function
-
wrapBiFunction
Wraps a ThrowingBiFunction and rethrows as unchecked RuntimeException.- Type Parameters:
T
- first inputU
- second inputR
- result type- Parameters:
function
- lambda with checked exception- Returns:
- safe BiFunction
-
wrapConsumer
Wraps a ThrowingConsumer into a regular Consumer.- Type Parameters:
T
- consumed type- Parameters:
consumer
- lambda with checked exception- Returns:
- safe Consumer
-
wrapBiConsumer
Wraps a ThrowingBiConsumer into a regular BiConsumer.- Type Parameters:
T
- first inputU
- second input- Parameters:
consumer
- lambda with checked exception- Returns:
- safe BiConsumer
-
wrapSupplier
Wraps a ThrowingSupplier into a regular Supplier.- Type Parameters:
T
- result type- Parameters:
supplier
- lambda with checked exception- Returns:
- safe Supplier
-
wrapRunnable
Wraps a ThrowingRunnable into a regular Runnable.- Parameters:
runnable
- lambda with checked exception- Returns:
- safe Runnable
-
safeFunctionOptional
Converts a ThrowingFunction into one that returns Optional. Returns Optional.empty() in case of exception.- Type Parameters:
T
- input typeR
- result type- Parameters:
function
- function that may throw- Returns:
- Optional-producing safe function
-
safeFunctionEither
public static <T,R> Function<T,Either<Exception, safeFunctionEitherR>> (ThrowingFunction<T, R> function) Converts a ThrowingFunction into one that returnsEither<Exception, R>
.- Type Parameters:
T
- input typeR
- result type- Parameters:
function
- function that may throw- Returns:
- Either-producing function
-
propagate
Propagates a checked exception as unchecked RuntimeException.- Parameters:
e
- exception to propagate- Returns:
- RuntimeException
-