Class Try

java.lang.Object
com.guinetik.corefun.Try

public final class Try extends Object
Utility class for executing operations that may fail and converting to Results.

Try provides static methods for wrapping operations that might throw exceptions and converting them to Result types for functional error handling. It bridges the gap between exception-based APIs and functional error handling.

Key Features

  • Exception to Result conversion - Wrap throwing operations in Results
  • Multiple error representations - String messages, full Exceptions, or custom types
  • Lazy evaluation - Defer execution until result is needed
  • Safe defaults - Execute with fallback values on failure

Example Usage


 // Execute and get Result with string error
 Result<String, String> content = Try.of(() -> Files.readString(path));

 // Chain operations - errors propagate automatically
 Result<Integer, String> lineCount = Try.of(() -> Files.readString(path))
     .map(s -> s.split("\n").length);

 // Keep full exception for detailed error handling
 Result<User, Exception> user = Try.ofException(() -> userService.find(id));

 // Custom error mapping
 Result<Config, AppError> config = Try.of(
     () -> loadConfig(path),
     e -> new AppError("CONFIG_LOAD_FAILED", e)
 );

 // Safe execution with default
 int port = Try.getOrDefault(() -> Integer.parseInt(env), 8080);
 
Since:
0.1.0
Author:
Guinetik <guinetik@gmail.com>
See Also:
  • Method Details

    • of

      public static <T> Result<T,String> of(Callable<T> action)
      Executes an operation and returns a Result with string error messages.
      Type Parameters:
      T - the result type
      Parameters:
      action - the operation to execute
      Returns:
      Success with the result, or Failure with error message
    • ofException

      public static <T> Result<T,Exception> ofException(Callable<T> action)
      Executes an operation and returns a Result preserving the exception.
      Type Parameters:
      T - the result type
      Parameters:
      action - the operation to execute
      Returns:
      Success with the result, or Failure with the exception
    • of

      public static <T, F> Result<T,F> of(Callable<T> action, Function<Exception,F> errorMapper)
      Executes an operation and returns a Result with custom error mapping.
      Type Parameters:
      T - the result type
      F - the failure type
      Parameters:
      action - the operation to execute
      errorMapper - function to convert exception to failure type
      Returns:
      Success with the result, or Failure with mapped error
    • run

      public static Result<Void,String> run(SafeRunnable action)
      Executes a runnable and returns a Result indicating success or failure.
      Parameters:
      action - the operation to execute
      Returns:
      Success with null, or Failure with error message
    • lazy

      public static <T> Supplier<Result<T,String>> lazy(Callable<T> action)
      Lazily wraps an operation, executing only when the result is accessed.
      Type Parameters:
      T - the result type
      Parameters:
      action - the operation to execute lazily
      Returns:
      a Supplier that executes the action when called
    • getOrDefault

      public static <T> T getOrDefault(Callable<T> action, T defaultValue)
      Executes an operation, returning a default value on failure.
      Type Parameters:
      T - the result type
      Parameters:
      action - the operation to execute
      defaultValue - the default value on failure
      Returns:
      the result or default value
    • getOrNull

      public static <T> T getOrNull(Callable<T> action)
      Executes an operation, returning null on failure.
      Type Parameters:
      T - the result type
      Parameters:
      action - the operation to execute
      Returns:
      the result or null