Class Try<T>

java.lang.Object
io.activej.common.collection.Try<T>
Type Parameters:
T - type of result

public final class Try<T> extends Object
A compound type that represents either a result or an exception

Semantically close to Either<T, Exception>

See Also:
  • Method Details

    • of

      public static <T> Try<T> of(@Nullable T result)
      Creates a new successful Try with a result
      Type Parameters:
      T - a type of result
      Parameters:
      result - a result value of a Try
      Returns:
      a new instance of a successful Try
    • of

      public static <T> Try<T> of(@Nullable T result, @Nullable @Nullable Exception e)
      Creates a new Try which is either successful and has a result or is failed and has an exception
      Type Parameters:
      T - a type of result
      Parameters:
      result - a result value of a Try
      e - an exception of a Try
      Returns:
      a new instance of a Try that is either successful or failed
    • ofException

      public static <T> Try<T> ofException(@NotNull @NotNull Exception e)
      Creates a new failed Try with a given exception
      Type Parameters:
      T - a type of result
      Parameters:
      e - an exception of a Try
      Returns:
      a new instance of a failed Try
    • wrap

      public static <T> Try<T> wrap(@NotNull @NotNull SupplierEx<T> computation)
      Creates a new Try which is either successful or failed based on the result of a supplier call

      If supplier throws exception a Try is failed. Otherwise, it will have supplied value as a result value of a Try

      Type Parameters:
      T - a type of result
      Parameters:
      computation - a throwing supplier of a result of a Try
      Returns:
      a new instance of a Try that is either successful or failed
    • wrap

      public static <T> Try<T> wrap(@NotNull @NotNull RunnableEx computation)
      Creates a new Try which is either successful or failed based on the result of a runnable call

      If runnable throws exception a Try is failed. Otherwise, it will have null as a result value of a Try

      Type Parameters:
      T - a type of result
      Parameters:
      computation - a throwing runnable
      Returns:
      a new instance of a Try that is either successful or failed
    • wrap

      public static <T> Try<T> wrap(@NotNull @NotNull Callable<? extends T> computation)
      Creates a new Try which is either successful or failed based on the result of a callable call

      If callable throws a runtime exception a Try is failed. Otherwise, it will have supplied value as a result value of a Try

      Type Parameters:
      T - a type of result
      Parameters:
      computation - a callable of a result of a Try
      Returns:
      a new instance of a Try that is either successful or failed
    • isSuccess

      @Contract(pure=true) public boolean isSuccess()
      Returns whether this Try is successful
    • isException

      @Contract(pure=true) public boolean isException()
      Returns whether this Try is failed
    • get

      @Contract(pure=true) @Nullable public T get()
      Returns a result of this Try, possibly null
    • getElse

      @Contract(pure=true) public T getElse(@Nullable T defaultValue)
      Returns a result of this Try if a Try is successful. Otherwise, returns a given default value
      Parameters:
      defaultValue - a default value to be returned if this Try is failed
      Returns:
      a result of this Try or a given default value
    • getElseGet

      @Contract(pure=true) public T getElseGet(@NotNull @NotNull Supplier<? extends T> defaultValueSupplier)
      Returns a result of this Try if a Try is successful. Otherwise, returns a supplied default value
      Parameters:
      defaultValueSupplier - a supplier of a default value to be returned if this Try is failed
      Returns:
      a result of this Try or a supplied default value
    • getException

      @Contract(pure=true) @Nullable public @Nullable Exception getException()
      Returns an exception of this Try, possibly null
    • ifSuccess

      @NotNull public @NotNull Try<T> ifSuccess(@NotNull @NotNull Consumer<? super T> resultConsumer)
      Consumes a result of this Try if it is successful. Otherwise, does nothing

      Always returns this Try

      Parameters:
      resultConsumer - a consumer of a result
      Returns:
      this Try
    • ifException

      @NotNull public @NotNull Try<T> ifException(@NotNull @NotNull Consumer<Exception> exceptionConsumer)
      Consumes an exception of this Try if it is failed. Otherwise, does nothing

      Always returns this Try

      Parameters:
      exceptionConsumer - a consumer of an exception
      Returns:
      this Try
    • consume

      @NotNull public @NotNull Try<T> consume(@NotNull @NotNull BiConsumer<? super T,Exception> consumer)
      Consumes both a result and an exception of this Try.

      Always returns this Try

      Parameters:
      consumer - a consumer of a result and an exception
      Returns:
      this Try
    • consume

      @NotNull public @NotNull Try<T> consume(@NotNull @NotNull Consumer<? super T> resultConsumer, @NotNull @NotNull Consumer<Exception> exceptionConsumer)
      Consumes a result of this Try if it is successful. Otherwise, consumes an exception

      Always returns this Try

      Parameters:
      resultConsumer - a consumer of a result
      exceptionConsumer - a consumer of an exception
      Returns:
      this Try
    • reduce

      @Contract(pure=true) public <U> U reduce(@NotNull @NotNull Function<? super T,? extends U> function, @NotNull @NotNull Function<@NotNull Exception,? extends U> exceptionFunction)
      Applies a function to either this Try's result or an exception
      Type Parameters:
      U - a type of mapping result
      Parameters:
      function - a function to map a result value
      exceptionFunction - a function to map an exception
      Returns:
      a result of mapping of either a result or an exception of a Try
    • reduce

      @Contract(pure=true) public <U> U reduce(@NotNull @NotNull BiFunction<? super T,Exception,? extends U> fn)
      Applies a function to this Try's result and an exception
      Type Parameters:
      U - a type of mapping result
      Parameters:
      fn - a function to map a result value and an exception
      Returns:
      a result of mapping of a result and an exception of a Try
    • map

      @Contract(pure=true) @NotNull public <U> @NotNull Try<U> map(@NotNull @NotNull FunctionEx<T,U> function)
      Returns a mapped Try obtained by mapping a result of this Try to a new value
      Type Parameters:
      U - a type of result of a new Try
      Parameters:
      function - a function to map left value to a new value
      Returns:
      a Try with a mapped result
    • flatMap

      @Contract(pure=true) @NotNull public <U> @NotNull Try<U> flatMap(@NotNull @NotNull Function<T,Try<U>> function)
      Returns a mapped Try obtained by mapping a result of this Try to a new Try
      Type Parameters:
      U - a type of result of a new Try
      Parameters:
      function - a function to map a result of this Try a new Try
      Returns:
      a mapped Try
    • toEither

      @Contract(pure=true) @NotNull public @NotNull Either<T,Exception> toEither()
      Convert this Try<T> to an Either<T, Exception>
      Returns:
      an Either which represents either a result or an exception of this Try
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object