https://github.com/deplant/commons-result
Implementation of Result pattern for modern Java inspired by Rust std result crate. Perfectly suited for pattern matching.
https://github.com/deplant/commons-result
pattern-matching result-pattern
Last synced: 10 months ago
JSON representation
Implementation of Result pattern for modern Java inspired by Rust std result crate. Perfectly suited for pattern matching.
- Host: GitHub
- URL: https://github.com/deplant/commons-result
- Owner: deplant
- License: apache-2.0
- Created: 2025-05-28T12:51:16.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2025-05-28T15:11:19.000Z (11 months ago)
- Last Synced: 2025-06-09T10:09:49.115Z (11 months ago)
- Topics: pattern-matching, result-pattern
- Language: Java
- Homepage:
- Size: 77.1 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Deplant Commons Result API
[](https://shields.io/)
[](https://shields.io/)
* Discuss in
Telegram: [](https://t.me/deplant\_chat\_en)
* Read full
docs: [](https://javadoc.io/doc/tech.deplant.commons/result)
**Deplant Commons Result API** implements **Result Pattern** for Java. It is a small
Java library with a wrapper class named `Result` with sealed implementations `Ok`
and `Err`.
Idea comes from Rust [std.Result crate](https://doc.rust-lang.org/std/result/) with
some changes in API to better suite Java needs.
Made in functional style, it perfectly matches with **Pattern matching** techniques
or more traditional conditional statements to manage results. Supports chaining results,
most of the functions are lazy and don't do unnecessary work.
Library is pure Java and has no dependencies aside from test ones.
## Writing function that can throw
```java
public static Result functionCanFail(Integer i) {
return Result.of(() -> 2 / i);
}
```
## Pattern matching result variants
As `Result` is a **sealed** interface, you can **pattern match** it without `default` branch.
```java
var result = Result.of(() -> 8 / 0);
switch (result) {
case Ok(Integer i) when i > 2 -> System.out.println("Good!");
case Ok ok -> System.out.println("Ok: " + ok.result());
case Err err -> System.out.println("Err: " + err.error().getMessage());
}
// prints "Err: / by zero"
```
## Mapping and unwrapping results
Results can be chained with other results through `.result.mapResult( functionThatCanFail )`,
wrapped value and type can be transformed though `.map( functionThatShouldntFail )`.
If you like to transpose `Result<>` to `Optional<>` you can do it with `Result.ofOptional()`
, `.ok()` and `.err()` methods.
```java
Result goodResult = Result.of(() -> 7);
Result multipliedResult = goodResult.map(i -> i * 2); // transformations
Result stringResult = goodResult.map(Object::toString); // type transformations
Result combinedResult = goodResult.mapResult(s -> functionCanFail(s)); // transformations with other results
Integer digit = combinedResult.orElse(2); // get or default when fail
Optional optionalDigit = combinedResult.ok(); // get as optional (empty when fail)
```
## Requirements
* OpenJDK 21 (or more)
*
## Maven or Gradle setup:
* Gradle
```groovy
dependencies {
implementation 'tech.deplant.commons:result:0.1.0'
}
```
* Maven
```xml
tech.deplant.commons
result
0.1.0
```