Class EitherExtensions

java.lang.Object
com.dudko.tools.safejavastreams.extensions.EitherExtensions

public final class EitherExtensions extends Object
Utility methods to convert from other types to Either.
  • Constructor Details

    • EitherExtensions

      private EitherExtensions()
  • Method Details

    • fromOptional

      public static <L, R> Either<L,R> fromOptional(Optional<R> optional, Supplier<? extends L> leftSupplier)
      Converts an Optional to an Either.
      Type Parameters:
      L - type of Left (error)
      R - type of Right (success)
      Parameters:
      optional - the optional value
      leftSupplier - 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 a CompletableFuture to an Either. Blocks the current thread.
      Type Parameters:
      L - type of Left (error)
      R - type of Right (success)
      Parameters:
      future - the future
      errorMapper - 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)
      Wraps a Supplier that may throw, producing an Either.
      Type Parameters:
      L - type of Left (error)
      R - type of Right (success)
      Parameters:
      supplier - the supplier
      errorMapper - 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 an Either.
      Type Parameters:
      L - the error type (Left)
      R - the result type (Right)
      Parameters:
      supplier - the supplier to execute
      errorMapper - a function mapping a Throwable to an error value
      Returns:
      a successful Either with the supplier's value, or a failed Either with the mapped error