Class EitherExtensions
java.lang.Object
com.dudko.tools.safejavastreams.extensions.EitherExtensions
Utility methods to convert from other types to
Either
.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <L,
R> Either<L, R> fromChecked
(Supplier<R> supplier, Function<Throwable, ? extends L> errorMapper) static <L,
R> Either<L, R> fromFuture
(CompletableFuture<R> future, Function<Throwable, ? extends L> errorMapper) Converts aCompletableFuture
to anEither
.static <L,
R> Either<L, R> fromOptional
(Optional<R> optional, Supplier<? extends L> leftSupplier) static <L,
R> Either<L, R> fromSupplier
(Supplier<R> supplier, Function<Throwable, ? extends L> errorMapper) Wraps a supplier that may throw, producing anEither
.
-
Constructor Details
-
EitherExtensions
private EitherExtensions()
-
-
Method Details
-
fromOptional
public static <L,R> Either<L,R> fromOptional(Optional<R> optional, Supplier<? extends L> leftSupplier) - Type Parameters:
L
- type of Left (error)R
- type of Right (success)- Parameters:
optional
- the optional valueleftSupplier
- supplier for the Left value if Optional is empty- Returns:
- Either with Right if value is present, Left otherwise
-
fromFuture
public static <L,R> Either<L,R> fromFuture(CompletableFuture<R> future, Function<Throwable, ? extends L> errorMapper) Converts aCompletableFuture
to anEither
. Blocks the current thread.- Type Parameters:
L
- type of Left (error)R
- type of Right (success)- Parameters:
future
- the futureerrorMapper
- function to convert exceptions to Left- Returns:
- Either with result or mapped error
-
fromChecked
public static <L,R> Either<L,R> fromChecked(Supplier<R> supplier, Function<Throwable, ? extends L> errorMapper) - Type Parameters:
L
- type of Left (error)R
- type of Right (success)- Parameters:
supplier
- the suppliererrorMapper
- mapper from exception to Left value- Returns:
- Either with result or mapped error
-
fromSupplier
public static <L,R> Either<L,R> fromSupplier(Supplier<R> supplier, Function<Throwable, ? extends L> errorMapper) Wraps a supplier that may throw, producing anEither
.
-