Class ResultExtensions
java.lang.Object
com.dudko.tools.safejavastreams.extensions.ResultExtensions
Utility methods to convert from other types to
Result
.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
E> Result<T, E> fromChecked
(Supplier<T> supplier, Function<Throwable, ? extends E> errorMapper) Wraps a supplier that may throw, producing aResult
.static <T,
E> Result<T, E> fromFuture
(CompletableFuture<T> future, Function<Throwable, ? extends E> errorMapper) Converts aCompletableFuture
into aResult
.static <T,
E> Result<T, E> fromOptional
(Optional<T> optional, Supplier<? extends E> errorSupplier) static <T,
E> Result<T, E> fromSupplier
(Supplier<T> supplier, Function<Throwable, ? extends E> errorMapper) Wraps a supplier that may throw, producing aResult
.
-
Constructor Details
-
ResultExtensions
private ResultExtensions()
-
-
Method Details
-
fromOptional
public static <T,E> Result<T,E> fromOptional(Optional<T> optional, Supplier<? extends E> errorSupplier) - Type Parameters:
T
- type of valueE
- type of error- Parameters:
optional
- the optional valueerrorSupplier
- supplier for error if value is empty- Returns:
- Result with value or error
-
fromFuture
public static <T,E> Result<T,E> fromFuture(CompletableFuture<T> future, Function<Throwable, ? extends E> errorMapper) Converts aCompletableFuture
into aResult
. Blocks the current thread.- Type Parameters:
T
- type of valueE
- type of error- Parameters:
future
- the futureerrorMapper
- mapper from Throwable to error type- Returns:
- Result from future
-
fromSupplier
public static <T,E> Result<T,E> fromSupplier(Supplier<T> supplier, Function<Throwable, ? extends E> errorMapper) Wraps a supplier that may throw, producing aResult
. -
fromChecked
public static <T,E> Result<T,E> fromChecked(Supplier<T> supplier, Function<Throwable, ? extends E> errorMapper) Wraps a supplier that may throw, producing aResult
. Alias forfromSupplier(Supplier, Function)
.- Type Parameters:
T
- type of valueE
- type of error- Parameters:
supplier
- the suppliererrorMapper
- mapper from Throwable to error type- Returns:
- Result with value or error
-