Class ResultExtensions

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

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

    • ResultExtensions

      private ResultExtensions()
  • Method Details

    • fromOptional

      public static <T, E> Result<T,E> fromOptional(Optional<T> optional, Supplier<? extends E> errorSupplier)
      Converts an Optional to a Result.
      Type Parameters:
      T - type of value
      E - type of error
      Parameters:
      optional - the optional value
      errorSupplier - 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 a CompletableFuture into a Result. Blocks the current thread.
      Type Parameters:
      T - type of value
      E - type of error
      Parameters:
      future - the future
      errorMapper - 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 a Result.
      Type Parameters:
      T - the result type
      E - the error type
      Parameters:
      supplier - the supplier to execute
      errorMapper - a function mapping a Throwable to an error value
      Returns:
      a successful Result with the supplier's value, or a failed Result with the mapped error
    • fromChecked

      public static <T, E> Result<T,E> fromChecked(Supplier<T> supplier, Function<Throwable,? extends E> errorMapper)
      Wraps a supplier that may throw, producing a Result. Alias for fromSupplier(Supplier, Function).
      Type Parameters:
      T - type of value
      E - type of error
      Parameters:
      supplier - the supplier
      errorMapper - mapper from Throwable to error type
      Returns:
      Result with value or error