Interface SafeExecutor

All Superinterfaces:
Loggable

public interface SafeExecutor extends Loggable
Provides safe execution of operations with timing, logging, and error handling.

SafeExecutor extends Loggable and combines timing, logging, and exception handling into a single interface for safely executing operations. By default, it logs operation start, completion (with timing), and errors.

Key Features

Implementation Pattern

Implement this interface in service classes to get automatic logging and timing for all operations. The interface-with-default-methods pattern means you only need to implement Loggable.logger().

Example with SLF4J


 public class DataProcessor implements SafeExecutor {
     private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(DataProcessor.class);

     @Override
     public Loggable.Logger logger() {
         return Loggable.Logger.of(LOG::info, LOG::warn, LOG::error);
     }

     public Data process() {
         return safely("Process data", () -> {
             // processing logic - automatically logged
             return processedData;
         });
     }
 }
 

Example with Simple println


 public class SimpleProcessor implements SafeExecutor {
     @Override
     public Loggable.Logger logger() {
         return Loggable.Logger.println();
     }

     public void doWork() {
         safely("Do work", () -> performWork());
         // Logs: [INFO] Executing: Do work
         // Logs: [INFO] Completed: Do work in 123ms
     }
 }
 

Result-Based Error Handling


 Result<Data, String> result = safelyResult("Load data", () -> loadData());
 result.fold(
     error -> showError(error),
     data -> displayData(data)
 );
 
Since:
0.1.0
Author:
Guinetik <guinetik@gmail.com>
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface com.guinetik.corefun.Loggable

    Loggable.Logger
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a no-op SafeExecutor that doesn't log.
    default void
    onComplete(String description, long milliseconds)
    Called when an operation completes successfully.
    default void
    onError(String description, Exception e)
    Called when an operation fails.
    default void
    onStart(String description)
    Called when an operation starts.
    Creates a SafeExecutor that prints to standard output.
    default <T> T
    safely(String description, Callable<T> action)
    Executes a callable safely with timing and error handling.
    default <T> Result<T,String>
    safelyResult(String description, Callable<T> action)
    Executes an operation and returns a Result instead of throwing.
    default void
    safelySafe(String description, SafeRunnable action)
    Executes a SafeRunnable safely with timing and error handling.
    default void
    safelyVoid(String description, Runnable action)
    Executes a runnable safely with timing and error handling.
    Creates a SafeExecutor with the given logger.

    Methods inherited from interface com.guinetik.corefun.Loggable

    logger
  • Method Details

    • onStart

      default void onStart(String description)
      Called when an operation starts. Default implementation logs via Loggable.logger().
      Parameters:
      description - the operation description
    • onComplete

      default void onComplete(String description, long milliseconds)
      Called when an operation completes successfully. Default implementation logs via Loggable.logger().
      Parameters:
      description - the operation description
      milliseconds - the execution time
    • onError

      default void onError(String description, Exception e)
      Called when an operation fails. Default implementation logs via Loggable.logger().
      Parameters:
      description - the operation description
      e - the exception that occurred
    • safely

      default <T> T safely(String description, Callable<T> action)
      Executes a callable safely with timing and error handling.
      Type Parameters:
      T - the return type
      Parameters:
      description - description of the operation
      action - the operation to execute
      Returns:
      the result of the operation
      Throws:
      SafeException - if the operation fails
    • safelyVoid

      default void safelyVoid(String description, Runnable action)
      Executes a runnable safely with timing and error handling.
      Parameters:
      description - description of the operation
      action - the operation to execute
      Throws:
      SafeException - if the operation fails
    • safelySafe

      default void safelySafe(String description, SafeRunnable action)
      Executes a SafeRunnable safely with timing and error handling.
      Parameters:
      description - description of the operation
      action - the operation to execute
      Throws:
      SafeException - if the operation fails
    • safelyResult

      default <T> Result<T,String> safelyResult(String description, Callable<T> action)
      Executes an operation and returns a Result instead of throwing.
      Type Parameters:
      T - the return type
      Parameters:
      description - description of the operation
      action - the operation to execute
      Returns:
      Success with result, or Failure with error message
    • withLogger

      static SafeExecutor withLogger(Loggable.Logger logger)
      Creates a SafeExecutor with the given logger.
      Parameters:
      logger - the logger to use
      Returns:
      a SafeExecutor that uses the provided logger
    • println

      static SafeExecutor println()
      Creates a SafeExecutor that prints to standard output.
      Returns:
      a SafeExecutor that prints execution info
    • noop

      static SafeExecutor noop()
      Creates a no-op SafeExecutor that doesn't log.
      Returns:
      a silent SafeExecutor