Interface Try<R,E>

Type Parameters:
R - success return value
E - error return value
All Known Implementing Classes:
Try.Er, Try.Ok

public sealed interface Try<R,E> permits Try.Er<R,E>, Try.Ok<R,E>
Try. Monad that represents result that is success Try.Ok or error Try.Er.
  • Method Details

    • isOk

      default boolean isOk()
      Returns:
      true iff this is Try.Ok
    • isEr

      default boolean isEr()
      Returns:
      true iff this is Try.Er
    • result

      default R result()
      Returns:
      getOrThrow()
    • error

      default E error()
      Returns:
      getOrThrow()
    • ifSuccess

      default Try<R,E> ifSuccess(Consumer<R> block)
      Invokes the specified block if this Try is Try.Ok and returns this
    • ifError

      default Try<R,E> ifError(Consumer<E> block)
      Invokes the specified block if this Try is Try.Er and returns this
    • ifAny

      default Try<R,E> ifAny(Runnable block)
      Invokes the specified block and returns this
    • map

      default <R2> Try<R2,E> map(Function<R,R2> block)
      Returns:
      try with success mapped by the specified mapper if success (mapper exceptions are not caught)
    • mapError

      default <E2> Try<R,E2> mapError(Function<E,E2> block)
      Returns:
      try with error mapped by the specified mapper if error (mapper exceptions are not caught)
    • getOrElse

      default R getOrElse(R defaultValue)
      Returns:
      success value or the specified value if error
    • getOrElseGet

      default R getOrElseGet(Supplier<R> defaultValue)
      Returns:
      success value or the specified value if error
    • getOrElseNull

      @Nullable default R getOrElseNull()
      Returns:
      success value or null if error (when success is nullable null is ambiguous)
    • getOrThrow

      default R getOrThrow()
      Returns:
      the Try.Ok result exception if this Try is Try.Er
    • getOrThrow

      default <ER extends Throwable> R getOrThrow(Function<? super E,? extends ER> errorSupplier) throws ER
      Returns:
      the Try.Ok result or the specified exception if this Try is Try.Er
      Throws:
      ER
    • toFlatTry

      static <R, E> Try<List<R>,List<E>> toFlatTry(List<? extends Try<R,E>> tries)
      Returns:
      error with all error values if any error or success with all values
    • runTry

      static <T> Try<Void,Throwable> runTry(sk.softec.util.ThrowingRunnable block)
      Invokes block and returns any Throwable as Try.Er or null as Try.Ok. Any throwable (except RethrowUtilsKt.isFailure(Throwable)) is consumed.
    • runTry

      static <T> Try<T,Throwable> runTry(sk.softec.util.ThrowingSupplier<T> block)
      Invokes block and returns any Throwable as Try.Er or result as Try.Ok. Any throwable (except RethrowUtilsKt.isFailure(Throwable)) is consumed.