Class Try<T>

java.lang.Object
com.github.darksoulq.abyssallib.common.util.Try<T>
Type Parameters:
T - The type of the successful value.

public abstract class Try<T> extends Object
A monadic container type that represents a computation that may either result in a successful value or a failure containing an exception.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    A functional interface for a function that maps an input to an output and may throw an exception.
    static interface 
    A functional interface for a runnable task that may throw a checked or unchecked exception.
    static interface 
    A functional interface for a supplier that may throw a checked or unchecked exception.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Try()
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract <U> Try<U>
    flatMap(Try.ThrowingFunction<? super T, Try<U>> mapper)
    Transforms the successful value into another Try using a throwing mapper function.
    abstract T
    get()
    Retrieves the successful value or throws a RuntimeException wrapping the failure.
    abstract Throwable
    Retrieves the exception that caused the failure.
    abstract boolean
    Determines if the computation completed successfully.
    abstract <U> Try<U>
    map(Try.ThrowingFunction<? super T, ? extends U> mapper)
    Transforms the successful value using the provided throwing mapper function.
    static <T> Try<T>
    of(Try.ThrowingSupplier<T> supplier)
    Executes a supplier that may throw an exception and wraps the result in a Try.
    abstract Try<T>
    Executes the provided consumer action if the computation resulted in a failure.
    abstract Try<T>
    onSuccess(Consumer<T> action)
    Executes the provided consumer action if the computation was successful.
    abstract T
    orElse(T other)
    Returns the successful value if present, otherwise returns the specified default.
    abstract T
    orElseGet(Supplier<? extends T> other)
    Returns the successful value if present, otherwise invokes the supplier for a fallback.
    abstract <X extends Throwable>
    T
    orElseThrow(Function<Throwable, X> exceptionProvider)
    Returns the successful value or throws a transformed exception.
    static Try<Void>
    Executes a runnable that may throw an exception and wraps the result in a Try.
    abstract Optional<T>
    Converts this Try instance into a standard Java Optional.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Try

      public Try()
  • Method Details

    • of

      public static <T> Try<T> of(Try.ThrowingSupplier<T> supplier)
      Executes a supplier that may throw an exception and wraps the result in a Try.
      Type Parameters:
      T - The result type of the supplier.
      Parameters:
      supplier - The computation to perform which may throw a Throwable.
      Returns:
      A Success containing the value if successful, or a Failure containing the caught Throwable.
    • run

      public static Try<Void> run(Try.ThrowingRunnable runnable)
      Executes a runnable that may throw an exception and wraps the result in a Try.
      Parameters:
      runnable - The action to perform which may throw a Throwable.
      Returns:
      A Success containing null if successful, or a Failure containing the caught Throwable.
    • isSuccess

      public abstract boolean isSuccess()
      Determines if the computation completed successfully.
      Returns:
      True if the instance is a Success, false if it is a Failure.
    • get

      public abstract T get()
      Retrieves the successful value or throws a RuntimeException wrapping the failure.
      Returns:
      The successful value of type T.
    • getException

      public abstract Throwable getException()
      Retrieves the exception that caused the failure.
      Returns:
      The Throwable caught during the computation.
      Throws:
      NoSuchElementException - If the Try is a Success and contains no exception.
    • map

      public abstract <U> Try<U> map(Try.ThrowingFunction<? super T, ? extends U> mapper)
      Transforms the successful value using the provided throwing mapper function.
      Type Parameters:
      U - The new result type after mapping.
      Parameters:
      mapper - The function to apply to the successful value.
      Returns:
      A new Success instance if mapping succeeds, or a Failure if an exception is caught.
    • flatMap

      public abstract <U> Try<U> flatMap(Try.ThrowingFunction<? super T, Try<U>> mapper)
      Transforms the successful value into another Try using a throwing mapper function.
      Type Parameters:
      U - The result type of the new Try.
      Parameters:
      mapper - The function to apply which returns a Try.
      Returns:
      The result of the mapper, or a Failure if an exception occurs during execution.
    • onFailure

      public abstract Try<T> onFailure(Consumer<Throwable> action)
      Executes the provided consumer action if the computation resulted in a failure.
      Parameters:
      action - The consumer to process the caught Throwable.
      Returns:
      The current Try instance for method chaining.
    • onSuccess

      public abstract Try<T> onSuccess(Consumer<T> action)
      Executes the provided consumer action if the computation was successful.
      Parameters:
      action - The consumer to process the successful value.
      Returns:
      The current Try instance for method chaining.
    • orElse

      public abstract T orElse(@Nullable T other)
      Returns the successful value if present, otherwise returns the specified default.
      Parameters:
      other - The fallback value to return on failure.
      Returns:
      The successful value or the provided default.
    • orElseGet

      public abstract T orElseGet(Supplier<? extends T> other)
      Returns the successful value if present, otherwise invokes the supplier for a fallback.
      Parameters:
      other - The supplier used to provide a default value on failure.
      Returns:
      The successful value or the supplied default.
    • orElseThrow

      public abstract <X extends Throwable> T orElseThrow(Function<Throwable, X> exceptionProvider) throws X
      Returns the successful value or throws a transformed exception.
      Type Parameters:
      X - The specific type of Throwable to be thrown.
      Parameters:
      exceptionProvider - Function to convert the original Throwable into type X.
      Returns:
      The successful value.
      Throws:
      X - If the computation failed.
    • toOptional

      public abstract Optional<T> toOptional()
      Converts this Try instance into a standard Java Optional.
      Returns:
      An Optional containing the value if successful, or an empty Optional on failure.