Package com.guinetik.corefun
Interface SafeExecutor
- All Superinterfaces:
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
- Automatic timing - Every operation is timed and logged
- Structured logging - Consistent start/complete/error messages
- Exception wrapping - Checked exceptions wrapped in
SafeException - Result support - Use
safelyResult(java.lang.String, java.util.concurrent.Callable<T>)for functional error handling - Customizable hooks - Override
onStart(java.lang.String),onComplete(java.lang.String, long),onError(java.lang.String, java.lang.Exception)
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 TypeMethodDescriptionstatic SafeExecutornoop()Creates a no-op SafeExecutor that doesn't log.default voidonComplete(String description, long milliseconds) Called when an operation completes successfully.default voidCalled when an operation fails.default voidCalled when an operation starts.static SafeExecutorprintln()Creates a SafeExecutor that prints to standard output.default <T> TExecutes a callable safely with timing and error handling.safelyResult(String description, Callable<T> action) Executes an operation and returns a Result instead of throwing.default voidsafelySafe(String description, SafeRunnable action) Executes a SafeRunnable safely with timing and error handling.default voidsafelyVoid(String description, Runnable action) Executes a runnable safely with timing and error handling.static SafeExecutorwithLogger(Loggable.Logger logger) Creates a SafeExecutor with the given logger.
-
Method Details
-
onStart
Called when an operation starts. Default implementation logs viaLoggable.logger().- Parameters:
description- the operation description
-
onComplete
Called when an operation completes successfully. Default implementation logs viaLoggable.logger().- Parameters:
description- the operation descriptionmilliseconds- the execution time
-
onError
Called when an operation fails. Default implementation logs viaLoggable.logger().- Parameters:
description- the operation descriptione- the exception that occurred
-
safely
Executes a callable safely with timing and error handling.- Type Parameters:
T- the return type- Parameters:
description- description of the operationaction- the operation to execute- Returns:
- the result of the operation
- Throws:
SafeException- if the operation fails
-
safelyVoid
Executes a runnable safely with timing and error handling.- Parameters:
description- description of the operationaction- the operation to execute- Throws:
SafeException- if the operation fails
-
safelySafe
Executes a SafeRunnable safely with timing and error handling.- Parameters:
description- description of the operationaction- the operation to execute- Throws:
SafeException- if the operation fails
-
safelyResult
Executes an operation and returns a Result instead of throwing.- Type Parameters:
T- the return type- Parameters:
description- description of the operationaction- the operation to execute- Returns:
- Success with result, or Failure with error message
-
withLogger
Creates a SafeExecutor with the given logger.- Parameters:
logger- the logger to use- Returns:
- a SafeExecutor that uses the provided logger
-
println
Creates a SafeExecutor that prints to standard output.- Returns:
- a SafeExecutor that prints execution info
-
noop
Creates a no-op SafeExecutor that doesn't log.- Returns:
- a silent SafeExecutor
-