The Result Utility Library is a Java implementation of the "Result" monad, providing a structured way to represent success and failure states in functional programming paradigms. Inspired by functional languages like Rust and Haskell, this library enables safer error handling without relying on exceptions.
The library aims to simplify error handling by encapsulating operations
within Result<S, F>
types, where S
represents success values and F
represents failure values. This approach allows developers to process results
in a predictable and composable manner.
Traditional exception handling in Java can be cumbersome and error-prone.
By using Result<S, F>
, we avoid unchecked exceptions and enforce explicit
handling of success and failure cases. This improves code clarity and maintainability.
Full documentation can be found here.
-
Creation Methods
success(S value)
: Creates a successful result.failure(F value)
: Creates a failure result.fromOptional(Optional<S> optional, Supplier<F> supplier)
: Converts anOptional
into aResult
.fromPredicate(S value, Predicate<S> predicate, Supplier<F> supplier)
: Evaluates a predicate to determine success or failure.fromBoolean(Boolean value, Supplier<S> successFn, Supplier<F> failureFn)
: Converts a boolean condition into aResult
.
-
Transformation Methods
map(Function<S, S2> mapper)
: Transforms the success value.mapFailure(Function<F, F2> mapper)
: Transforms the failure value.mapEither(Function<S, S2> successMapper, Function<F, F2> failureMapper)
: Transforms both success and failure values.
-
Binding Methods
bind(Function<S, Result<S2, F>> binding)
: Chains operations that returnResult
.bindFailure(Function<F, Result<S, F2>> binding)
: Chains operations on failures.
-
Utility Methods
isSuccess() / isFailure()
: Checks the result state.orThrow()
: Extracts the success value or throws an exception.toOptional()
: Converts the result to anOptional
.
// declare type
Result<Integer, String> success = Result.success(42);
Result<Integer, String> failure = Result.failure("Error occurred");
// provide alternative type
var success = Result.success(42, String.class);
var failure = Result.failure("Error occurred", Integer.class);
// use generics
var success = Result.<Integer, String>success(42);
var failure = Result.<Integer, String>failure("Error occurred");
Result<Integer, String> result = Result.success(10);
Result<String, String> transformed = result.map(value -> "Value: " + value);
Result<Integer, String> divide(int a, int b) {
return b == 0 ? Result.failure("Cannot divide by zero") : Result.success(a / b);
}
Result<Integer, String> finalResult = divide(10, 2).bind(value -> divide(value, 2));
<dependency>
<groupId>at.base10</groupId>
<artifactId>result</artifactId>
<version>1.4.0</version>
</dependency>
implementation 'at.base10:result:1.4.0'
This library is released under the MIT License.
Contributions are welcome! Feel free to submit issues and pull requests to improve this library.
For any questions, reach out via GitHub issues.