Interface SafeCallable<T>

Type Parameters:
T - the type of the result
All Superinterfaces:
Callable<T>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface SafeCallable<T> extends Callable<T>
A functional interface for operations that return a value and may throw checked exceptions.

SafeCallable extends the standard Callable interface with convenient methods for exception handling and conversion to Result types. It provides multiple strategies for handling potential failures.

Key Features

  • Exception wrapping - Convert checked exceptions to unchecked with getOrThrow()
  • Default values - Provide fallbacks with getOrElse(Object)
  • Result conversion - Transform to functional Results with toResult()
  • Callable compatibility - Use anywhere a standard Callable is expected

Example Usage


 SafeCallable<String> reader = () -> Files.readString(path);

 // Execute with automatic exception wrapping
 String content = reader.getOrThrow();  // throws SafeException on failure

 // Or use a default value on failure
 String contentOrDefault = reader.getOrElse("default content");

 // Convert to Result for functional handling
 Result<String, Exception> result = reader.toResult();
 result.fold(
     ex -> handleError(ex),
     content -> processContent(content)
 );

 // Create a callable that always returns a value
 SafeCallable<Config> defaultConfig = SafeCallable.of(Config.defaults());
 
Since:
0.1.0
Author:
Guinetik <guinetik@gmail.com>
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Computes the result, potentially throwing a checked exception.
    static <T> SafeCallable<T>
    from(Callable<T> callable)
    Creates a SafeCallable from a standard Callable.
    default T
    getOrElse(T defaultValue)
    Executes and returns the result, or a default value on failure.
    default T
    Executes and returns the result, wrapping any exception in SafeException.
    static <T> SafeCallable<T>
    of(T value)
    Creates a SafeCallable that always returns the given value.
    default Result<T,Exception>
    Converts this callable to a Result, capturing any exception.
    default Result<T,String>
    Converts this callable to a Result with a string error message.
  • Method Details

    • call

      T call() throws Exception
      Computes the result, potentially throwing a checked exception.
      Specified by:
      call in interface Callable<T>
      Returns:
      the computed result
      Throws:
      Exception - if computation fails
    • getOrThrow

      default T getOrThrow()
      Executes and returns the result, wrapping any exception in SafeException.
      Returns:
      the computed result
      Throws:
      SafeException - if computation fails
    • getOrElse

      default T getOrElse(T defaultValue)
      Executes and returns the result, or a default value on failure.
      Parameters:
      defaultValue - the value to return on failure
      Returns:
      the computed result or default
    • toResult

      default Result<T,Exception> toResult()
      Converts this callable to a Result, capturing any exception.
      Returns:
      Success with the result, or Failure with the exception
    • toResultWithMessage

      default Result<T,String> toResultWithMessage()
      Converts this callable to a Result with a string error message.
      Returns:
      Success with the result, or Failure with error message
    • from

      static <T> SafeCallable<T> from(Callable<T> callable)
      Creates a SafeCallable from a standard Callable.
      Type Parameters:
      T - the result type
      Parameters:
      callable - the Callable to wrap
      Returns:
      a SafeCallable that delegates to the Callable
    • of

      static <T> SafeCallable<T> of(T value)
      Creates a SafeCallable that always returns the given value.
      Type Parameters:
      T - the result type
      Parameters:
      value - the value to return
      Returns:
      a SafeCallable that returns the value