Created
November 30, 2019 22:32
-
-
Save renanreismartins/2c544b147dc334d1b2ee0c9c3fd92775 to your computer and use it in GitHub Desktop.
Trying to understand Try vavr api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void test() { | |
loadName(1) | |
.onSuccess(n -> log(format("onSuccess before recovery: %s", n))) | |
.andThen(n -> log(format("and then log this before recovery: %s", n))) | |
.onFailure(e -> log("error before recovery")) | |
.recoverWith(IllegalArgumentException.class, e -> loadNameFromFallback()) | |
.onSuccess(n -> log(format("onSuccess after recovery: %s", n))) | |
.onSuccess(n -> log(format("another onSuccess: %s", n))) | |
.andThen(n -> log(format("and then log this after recovery: %s", n))) | |
.onFailure(e -> log("error after recovery")); | |
} | |
private Try<String> loadName(int id) { | |
if (id == 1) { | |
return Try.success("renan"); | |
} else { | |
return Try.failure(new IllegalArgumentException()); | |
} | |
} | |
private Try<String> loadNameFromFallback() { | |
return Try.success("renan from fallback"); | |
} | |
private void log(String s) { | |
System.out.println(s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment