A Java library to handle success and failure without exceptions
If you seek advice… then this Java library is for you!
Result
objects have all the answers you need. Start by “asking the ball” a Yes or No question, then turn it over and let the answers magically appear. Results can be either positive (“Success”) or negative (“Failure”). Great fun for software developers and adults alike. Colors and decorations may vary.
Instances of Result
represent either the success or failure of an operation.
Result objects are immutable and type-safe. Operations that succeed produce results encapsulating a success value; operations that fail produce results with a failure value.
Treating failed operations as regular values allows for a functional approach to error handling – instead of throwing
exceptions and using try-catch blocks. In terms of functional programming, Result
is just a monadic container type.
The best way to think of Result
is as a super-powered version of Optional
. The only difference is that, whereas
Optional
may contain a value or be empty, Result
contains either a success value or a failure value.
The library requires JDK 1.8 or higher. Other than that, it has no external dependencies and it is very lightweight. Adding it to your build should be very easy.
Artifact coordinates:
com.leakyabstractions
result
0.15.0.1
To add the dependency using Maven, use the following:
<dependency>
<groupId>com.leakyabstractions</groupId>
<artifactId>result</artifactId>
<version>0.15.0.1</version>
</dependency>
To add the dependency using Gradle, if you are building an application that will use Result internally:
dependencies {
implementation("com.leakyabstractions:result:0.15.0.1")
}
If you are building a library that will use Result type in its public API, you should use instead:
dependencies {
api("com.leakyabstractions:result:0.15.0.1")
}
Results.success
creates a new successful result containing the given value.Results.failure
creates a new failed result containing the given value.Results.ofNullable
creates a new result based on the given possibly-null value.Results.ofOptional
creates a new result based on the given possibly-empty optional.Results.ofCallable
creates a new result based on the given possibly-throwing task.hasSuccess
checks if a result contains a success value.hasFailure
checks if a result contains a failure value.getSuccess
returns a result’s success value as a possibly-empty optional.getFailure
returns a result’s failure value as a possibly-empty optional.orElse
returns a result’s success value, or the given one.orElseMap
returns a result’s success value, or maps its failure value.streamSuccess
returns a result’s success value as a possibly-empty stream.streamFailure
returns a result’s failure value as a possibly-empty stream.ifSuccess
performs the given action with a successful result’s value.ifFailure
performs the given action with a failed result’s value.ifSuccessOrElse
performs either of the given actions with a result’s value.filter
transforms a successful result into a failed one, based on the given condition.recover
transforms a failed result into a successful one, based on the given condition.mapSuccess
transforms the value of a successful result.mapFailure
transforms the value of a failed result.map
transforms either the success or the failure value of a result.flatMapSuccess
transforms a successful result into a different one.flatMapFailure
transforms a failed result into a different one.flatMap
transforms a result into a different one.This library adheres to Pragmatic Versioning.
Artifacts are available in Maven Central.
Here you can find the full Javadoc documentation.
You may want to visualize the latest benchmark report.
We’d love to help. Check out the support guidelines.
If you’d like to contribute to this project, please start here.
This project is governed by the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.
Copyright 2024 Guillermo Calvo.
This library is licensed under the Apache License, Version 2.0 (the “License”); you may not use it except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
Permitted:
Required:
Forbidden: