Skip to content

Instantly share code, notes, and snippets.

@rnkoaa
Created November 18, 2021 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnkoaa/4ba34782b9ba14a0361ee7b4976ec1e4 to your computer and use it in GitHub Desktop.
Save rnkoaa/4ba34782b9ba14a0361ee7b4976ec1e4 to your computer and use it in GitHub Desktop.
VAVR try to Reactor Conversion
public Mono<Person> load(UUID id) {
return API.Match(tryLoadPerson(id)).of(
Case($Success($()), Mono::just),
Case($Failure($(instanceOf(IllegalArgumentException.class))), Mono::error),
Case($Failure($()), x -> Mono.error(new RuntimeException("unknown error")))
);
}
private Try<Person> tryLoadPerson(UUID id) {
return API.Match(success()).of(
Case($(true), Try.success(new Person(UUID.randomUUID(), "Will Smith"))),
Case($(), Try.failure(new IllegalArgumentException("Unknown error")))
);
}
private boolean success() {
return (int) (Math.random() * 2 + 1) < 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment