Package com.guinetik.corefun
Class Try
java.lang.Object
com.guinetik.corefun.Try
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 Summary
Modifier and TypeMethodDescriptionstatic <T> TgetOrDefault(Callable<T> action, T defaultValue) Executes an operation, returning a default value on failure.static <T> TExecutes an operation, returning null on failure.Lazily wraps an operation, executing only when the result is accessed.Executes an operation and returns a Result with string error messages.static <T,F> Result<T, F> Executes an operation and returns a Result with custom error mapping.ofException(Callable<T> action) Executes an operation and returns a Result preserving the exception.run(SafeRunnable action) Executes a runnable and returns a Result indicating success or failure.
-
Method Details
-
of
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
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
Executes an operation and returns a Result with custom error mapping.- Type Parameters:
T- the result typeF- the failure type- Parameters:
action- the operation to executeerrorMapper- function to convert exception to failure type- Returns:
- Success with the result, or Failure with mapped error
-
run
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
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
Executes an operation, returning a default value on failure.- Type Parameters:
T- the result type- Parameters:
action- the operation to executedefaultValue- the default value on failure- Returns:
- the result or default value
-
getOrNull
Executes an operation, returning null on failure.- Type Parameters:
T- the result type- Parameters:
action- the operation to execute- Returns:
- the result or null
-