Package com.guinetik.corefun
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.
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 TypeMethodDescriptioncall()Computes the result, potentially throwing a checked exception.static <T> SafeCallable<T>Creates a SafeCallable from a standard Callable.default TExecutes and returns the result, or a default value on failure.default TExecutes 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.toResult()Converts this callable to a Result, capturing any exception.Converts this callable to a Result with a string error message.
-
Method Details
-
call
Computes the result, potentially throwing a checked exception. -
getOrThrow
Executes and returns the result, wrapping any exception in SafeException.- Returns:
- the computed result
- Throws:
SafeException- if computation fails
-
getOrElse
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
Converts this callable to a Result, capturing any exception.- Returns:
- Success with the result, or Failure with the exception
-
toResultWithMessage
Converts this callable to a Result with a string error message.- Returns:
- Success with the result, or Failure with error message
-
from
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
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
-