Class Try<T>

java.lang.Object
com.dudko.tools.safejavastreams.core.Try<T>
Direct Known Subclasses:
Try.Failure, Try.Success

public abstract class Try<T> extends Object
Try is a container for a computation that may either result in a value (Success) or an exception (Failure). Similar to Either<Throwable, T>.
  • Constructor Details

    • Try

      public Try()
  • Method Details

    • isSuccess

      public abstract boolean isSuccess()
      Returns true if this is a Success.
    • isFailure

      public boolean isFailure()
      Returns true if this is a Failure.
    • get

      public abstract T get() throws Throwable
      Returns the value if this is a Success, otherwise throws the exception.
      Throws:
      Throwable
    • map

      public abstract <U> Try<U> map(Function<? super T,? extends U> mapper)
      Applies a function to the value if Success, otherwise propagates the Failure.
      Type Parameters:
      U - mapped value type
      Parameters:
      mapper - function to apply
      Returns:
      new Try with mapped value or the original Failure
    • flatMap

      public abstract <U> Try<U> flatMap(Function<? super T,Try<U>> mapper)
      Applies a function that returns Try if Success, otherwise propagates the Failure.
      Type Parameters:
      U - mapped value type
      Parameters:
      mapper - function to apply
      Returns:
      result of function or the original Failure
    • recover

      public abstract Try<T> recover(Function<? super Throwable,? extends T> recovery)
      Applies a recovery function if this is a Failure.
      Parameters:
      recovery - recovery function
      Returns:
      Success with recovered value or original Success
    • recoverWith

      public abstract Try<T> recoverWith(Function<? super Throwable,Try<T>> recovery)
      Applies a recovery function that returns Try if this is a Failure.
      Parameters:
      recovery - recovery function
      Returns:
      Try from recovery or original Success
    • fold

      public abstract <U> U fold(Function<? super Throwable,? extends U> failureMapper, Function<? super T,? extends U> successMapper)
      Applies one of two functions depending on whether it's a Success or Failure.
      Type Parameters:
      U - return type
      Parameters:
      failureMapper - mapper for Failure
      successMapper - mapper for Success
      Returns:
      result of applying the corresponding function
    • peek

      public abstract Try<T> peek(Consumer<? super T> action)
      Performs side-effecting operation on Success.
      Parameters:
      action - consumer to run if Success
      Returns:
      the same Try instance
    • peekFailure

      public abstract Try<T> peekFailure(Consumer<? super Throwable> action)
      Performs side-effecting operation on Failure.
      Parameters:
      action - consumer to run if Failure
      Returns:
      the same Try instance
    • toOptional

      public abstract Optional<T> toOptional()
      Returns the value as Optional if Success, otherwise empty.
    • getError

      public abstract Throwable getError()
    • toEither

      public abstract <L> Either<L,T> toEither(Function<? super Throwable,? extends L> mapper)
    • isFailureOf

      public boolean isFailureOf(Class<? extends Throwable> type)
      Checks if this Try is a Failure of specific exception type.
      Parameters:
      type - exception type to check
      Returns:
      true if this is Failure with given type
    • onSuccess

      public Try<T> onSuccess(Consumer<? super T> action)
      Executes an action if Success.
    • onFailure

      public Try<T> onFailure(Consumer<? super Throwable> action)
      Executes an action if Failure.
    • getOrElse

      public T getOrElse(T defaultValue)
      Returns value or default if Failure.
    • getOrElseGet

      public T getOrElseGet(Function<Throwable,? extends T> supplier)
      Returns value or result of supplier if Failure.
    • getOrThrow

      public T getOrThrow()
      Returns value or throws RuntimeException if Failure.
    • getOrThrow

      public T getOrThrow(Function<Throwable,? extends RuntimeException> mapper)
      Returns value or throws custom RuntimeException from mapper.
    • filter

      public Try<T> filter(Predicate<? super T> predicate, Supplier<Throwable> ifInvalid)
      Applies a predicate to value if Success. Returns Failure if predicate fails.
    • toStream

      public Stream<T> toStream()
      Converts to Java Stream with 0 or 1 element.
    • of

      public static <T> Try<T> of(ThrowingSupplier<T> supplier)
      Executes given code block and wraps result in Try.
    • success

      public static <T> Try<T> success(T value)
      Creates a Success instance.
      Type Parameters:
      T - type of the value
      Parameters:
      value - the value
      Returns:
      Success with the value
    • failure

      public static <T> Try<T> failure(Throwable exception)
      Creates a Failure instance.
      Type Parameters:
      T - type of the value
      Parameters:
      exception - the exception
      Returns:
      Failure with the exception