Coder Social home page Coder Social logo

mrkloan / result-type Goto Github PK

View Code? Open in Web Editor NEW
24.0 2.0 4.0 129 KB

A Result type for the Java programming language.

License: Do What The F*ck You Want To Public License

Java 100.00%
java type result railway railway-oriented-programming error-handling

result-type's Introduction

result-type Build Status codecov

A Result type for the Java programming language.

Usage

Wrap a value of any type:

Result.ok(1)
      .map(value -> value + 2)
      .map(String::valueOf)
      .flatMap(value -> Result.ok(value + "3"))
      .ifOk(value -> { /* Do something with the value */ });

Propagate errors safely:

Result.error(new IllegalArgumentException("Error message"))
      .mapError(IllegalStateException::new)
      .ifError(error -> { /* Do something with the error */ });

Unwrap the value:

final Result<String> result = Result.ok("Value");

if(result.isOk()) {
    final String value = result.get();
    /* Do something with the value */
}

Unwrap the error:

final Result<String> result = Result.error(new IllegalStateException("Error message"));

if(result.isError()) {
    final Throwable error = result.getError();
    /* Do something with the error */
}

Wrap a value safely by supplying a fallback error:

// If `someVariable` is null, the default error is a NullPointerException.
Result.ofNullable(someVariable);

// Use a custom error supplier.
Result.ofNullable(someVariable, IllegalStateException::new);

Or migrate an Optional<T> type to a Result<T>:

final Optional<String> optional = Optional.of("Value");
final Result<String> result = Result.of(optional);

Unwrap safely by supplying a fallback value:

final Result<Integer> result = Result.error(new IllegalStateException("Error message"));
final Integer value = result.getOrElse(() -> 7);

Wrap a legacy method that may throw an exception to gracefully integrate it to your railway flow:

public Result<User> findUser(final Id id) {
    return Result
        .of(() -> legacyService.findUser(id))
        .map(user -> legacyService.transform(user));
}

Try to recover from an error using a fallback method:

public Result<User> findUser(final Id id) {
    return Result
        .of(() -> legacyService.findUser(id))
        .switchIfError(error -> fallbackSearch(id))
        .map(user -> legacyService.transform(user));
}

private Result<User> fallbackSearch(final Id id) {
    return Result.of(() -> cacheService.findUser(id));
}

Installation

For Java 8 compatibility, use the latest supported version.

Gradle:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

dependencies {
	implementation 'com.github.MrKloan:result-type:2.0.0'
}

Maven:

<repositories>
	<repository>
		<id>jitpack.io</id>
		<url>https://jitpack.io</url>
	</repository>
</repositories>

<dependencies>
	<dependency>
		<groupId>com.github.MrKloan</groupId>
		<artifactId>result-type</artifactId>
		<version>2.0.0</version>
	</dependency>
</dependencies>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.