Skip to content

Instantly share code, notes, and snippets.

@joost-de-vries
Last active December 18, 2020 17:32
Show Gist options
  • Save joost-de-vries/c761e680ac403cf7c0ea49a5cc7c448e to your computer and use it in GitHub Desktop.
Save joost-de-vries/c761e680ac403cf7c0ea49a5cc7c448e to your computer and use it in GitHub Desktop.
"Functional programming"
class FunctionalProgramming {
private static <T> CheckoutModel<Optional<T>> oldHandleObservable(Observable<T> observable, RegisterFailed attempt) {
final Set<ChkNotificationMessage> errorMessages = new HashSet<>();
Optional<T> optional = observable
.doOnError(throwable -> errorMessages.addAll(mapChkAdapterExceptions(throwable, attempt)))
.onErrorResumeNext(Observable.empty())
.map(Optional::of)
.defaultIfEmpty(Optional.empty())
.toBlocking()
.single();
optional.ifPresent(it -> errorMessages.addAll(getMessages(it)));
return new CheckoutModel<>(optional, errorMessages);
}
private static <T> Observable<CheckoutModel<Optional<T>>> handleObservable(Observable<T> observable, RegisterFailed attempt) {
return observable
.map(it -> new CheckoutModel<>(Optional.of(it), getMessages(it)))
.onErrorResumeNext(this::handleRedirect) // throw redirect exception
.onErrorResumeNext(it -> handleOtherThanRedirect(it, attempt));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment