Important: This package is a mirror of https://github.com/jasongoodwin/better-java-monads, however that package is not maintained any more. This way we can maintain the code ourselves. Furthermore, the dependency as pulled in from maven-central was also unable to properly show javadoc comments for unknown reason. Big chunks of the following text are taken straight from the original source code.
This library adds a Result monad to Java. While Optional exists to express nulls in types, there is no way to
express a success/failure of a callable lambda expression. Result fills this gap.
The Result type is very similar to the Result in Scala's standard lib. Some inspiration is also taken from
the Result monad in the Rust language, and subsequently from @sapphire/result, the TypeScript implementation of the
former.
Lastly, this version of the Result monad also addresses all open issues in the original project, as well as including
the
code of all open pull requests.
<dependency>
<groupId>tech.favware</groupId>
<artifactId>result</artifactId>
<version>1.2.0</version>
</dependency>With Gradle Groovy
dependencies {
implementation 'tech.favware:result:1.2.0'
}With Kotlin DSL
dependencies {
implementation("tech.favware:result:1.2.0")
}The Result monad was attributed to Twitter and placed into the Scala standard library.
While both Scala and Haskell have a monad Either which has a left and a right type,
a Result is specifically of a type T on success or an exception on failure.
The Result api is meant to be similar to the Optional type so has the same functions.
get()returns the held value or throws the thrown exceptiongetUnchecked()returns the held value or throws the thrown exception wrapped in aRuntimeExceptioninstancemap(x)maps the success valuexto a new value and type or otherwise passes the Failure forward.flatMap((x) -> f(x))maps the success valuexto a newResultof f(x).recover((t) -> x)will return the success value of theResultin the success case or the valuexin the failure case. Exposes the exception.recoverWith((t) -> f(x))will return the success value of theResultin the success case or a newResultoff(x)in the failure case. Exposes the exception.filter((x) -> isTrue(x))- IfSuccess, returns the sameSuccessif the predicate succeeds, otherwise, returns aFailurewith typeNoSuchElementException.onSuccess((x) -> f(x))execute some code on success - takes aConsumer(e.g. requires no return value).onFailure((x) -> f(x))execute some code on failure - takes aConsumer(e.g. requires no return value).raise(x)-> will throw an exception of typexwhen it happens.orElse(x)will return the success value of theResultin success case or the valuexin failure case.orElseResult(f)will return the success value of theResultin success case or a newResult(f)in the failure case.orElseThrow(() -> throw new T)gets result or on failure will throw checked exception of typeTtoOptional()will returnOptionalof success value ofResult(if notnull), otherwise it will return an emptyOptionalmapErr(e)maps the failure valueeto a new error type or otherwise passes the success forward.match((x, y)Takes the result of theResultand applies the first function to the success value or the second function to the failure value. Transforms the return type to the return type of the applied function.
Please make sure to read the Contributing Guide before making a pull request.
Thank you to all the people who already contributed to Sapphire!
