|
| 1 | +package com.dudko.tools.safejavastreams.extensions; |
| 2 | + |
| 3 | +import com.dudko.tools.safejavastreams.core.Either; |
| 4 | + |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.concurrent.CompletableFuture; |
| 7 | +import java.util.function.Function; |
| 8 | +import java.util.function.Supplier; |
| 9 | + |
| 10 | +/** |
| 11 | + * Utility methods to convert from other types to {@link Either}. |
| 12 | + */ |
| 13 | +public final class EitherExtensions { |
| 14 | + |
| 15 | + private EitherExtensions() { |
| 16 | + throw new AssertionError("No instances for you!"); |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Converts an {@link Optional} to an {@link Either}. |
| 21 | + * |
| 22 | + * @param optional the optional value |
| 23 | + * @param leftSupplier supplier for the Left value if Optional is empty |
| 24 | + * @param <L> type of Left (error) |
| 25 | + * @param <R> type of Right (success) |
| 26 | + * @return Either with Right if value is present, Left otherwise |
| 27 | + */ |
| 28 | + public static <L, R> Either<L, R> fromOptional(Optional<R> optional, Supplier<? extends L> leftSupplier) { |
| 29 | + return optional.map(Either::<L, R>right) |
| 30 | + .orElseGet(() -> Either.left(leftSupplier.get())); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Converts a {@link CompletableFuture} to an {@link Either}. Blocks the current thread. |
| 35 | + * |
| 36 | + * @param future the future |
| 37 | + * @param errorMapper function to convert exceptions to Left |
| 38 | + * @param <L> type of Left (error) |
| 39 | + * @param <R> type of Right (success) |
| 40 | + * @return Either with result or mapped error |
| 41 | + */ |
| 42 | + public static <L, R> Either<L, R> fromFuture( |
| 43 | + CompletableFuture<R> future, |
| 44 | + Function<Throwable, ? extends L> errorMapper) { |
| 45 | + try { |
| 46 | + return Either.right(future.get()); |
| 47 | + } catch (Throwable t) { |
| 48 | + Throwable root = t.getCause() != null ? t.getCause() : t; |
| 49 | + return Either.left(errorMapper.apply(root)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Wraps a {@link Supplier} that may throw, producing an {@link Either}. |
| 55 | + * |
| 56 | + * @param supplier the supplier |
| 57 | + * @param errorMapper mapper from exception to Left value |
| 58 | + * @param <L> type of Left (error) |
| 59 | + * @param <R> type of Right (success) |
| 60 | + * @return Either with result or mapped error |
| 61 | + */ |
| 62 | + public static <L, R> Either<L, R> fromChecked(Supplier<R> supplier, Function<Throwable, ? extends L> errorMapper) { |
| 63 | + try { |
| 64 | + return Either.right(supplier.get()); |
| 65 | + } catch (Throwable t) { |
| 66 | + return Either.left(errorMapper.apply(t)); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Wraps a supplier that may throw, producing an {@link Either}. |
| 72 | + * |
| 73 | + * @param supplier the supplier to execute |
| 74 | + * @param errorMapper a function mapping a {@link Throwable} to an error value |
| 75 | + * @param <L> the error type (Left) |
| 76 | + * @param <R> the result type (Right) |
| 77 | + * @return a successful {@link Either} with the supplier's value, or a failed {@link Either} with the mapped error |
| 78 | + */ |
| 79 | + public static <L, R> Either<L, R> fromSupplier(Supplier<R> supplier, Function<Throwable, ? extends L> errorMapper) { |
| 80 | + try { |
| 81 | + return Either.right(supplier.get()); |
| 82 | + } catch (Throwable t) { |
| 83 | + return Either.left(errorMapper.apply(t)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments