8000 GitHub - samwho/result: A Rust-like Result type for Java.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

samwho/result

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Result type for Java

This is my humble attempt at a practical Result type for Java. It is based heavily on the Result type from Rust and care has been taken to make sure it can be propagated in the event of failure.

Usage

public final class Example {
    public static void main(String[] args) throws Throwable {
        // Happy to re-throw? Go for it!
        System.out.println(getInteger().map(i -> i + 1).getOrThrow());

        // Want to do error checking? No problem!
        Result<String> result = Result.from(App::woot).map(String::toUpperCase);
        if (result.isError()) {
          System.err.println("Oh no!");
        } else {
          System.out.println("Result: " + result.get());
        }

        // Want to add more info to errors? We got you.
        String username = Result.from(App::fetchUsername)
            .wrapError(IllegalArgumentException::new, "unable to find user")
            .getOrThrow();
    }

    public static String fetchUsername() throws IOException {
      return mayThrow(); // pretend there's business logic here
    }

    public static Result<Integer> getInteger() {
        return getString().map(Integer::valueOf);
    }

    public static Result<String> getString() {
        if (ThreadLocalRandom.current().nextBoolean()) {
            return Result.success("10");
        } else {
            return Result.fail(new IllegalStateException("Ruh roh"));
        }
    }

    public static String mayThrow() throws IOException {
        if (ThreadLocalRandom.current().nextBoolean()) {
            return "Success!";
        } else {
            throw new IOException("Rats!");
        }
    }
}

About

A Rust-like Result type for Java.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0