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.0.0</version>
</dependency>
With Gradle Groovy
dependencies {
implementation 'tech.favware:result:1.0.0'
}
With Kotlin DSL
dependencies {
implementation("tech.favware:result:1.0.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 aRuntimeException
instancemap(x)
maps the success valuex
to a new value and type or otherwise passes the Failure forward.flatMap((x) -> f(x))
maps the success valuex
to a newResult
of f(x).recover((t) -> x)
will return the success value of theResult
in the success case or the valuex
in the failure case. Exposes the exception.recoverWith((t) -> f(x))
will return the success value of theResult
in the success case or a newResult
off(x)
in the failure case. Exposes the exception.filter((x) -> isTrue(x))
- IfSuccess
, returns the sameSuccess
if the predicate succeeds, otherwise, returns aFailure
with 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 typex
when it happens.orElse(x)
will return the success value of theResult
in success case or the valuex
in failure case.orElseResult(f)
will return the success value of theResult
in success case or a newResult(f)
in the failure case.orElseThrow(() -> throw new T)
gets result or on failure will throw checked exception of typeT
toOptional()
will returnOptional
of success value ofResult
(if notnull
), otherwise it will return an emptyOptional
Please make sure to read the Contributing Guide before making a pull request.
Thank you to all the people who already contributed to Sapphire!