Skip to content

Instantly share code, notes, and snippets.

@mzakyalvan
Created September 20, 2018 17:45
Show Gist options
  • Save mzakyalvan/c3a99f65881c0384b0c17096c2fe7d37 to your computer and use it in GitHub Desktop.
Save mzakyalvan/c3a99f65881c0384b0c17096c2fe7d37 to your computer and use it in GitHub Desktop.
package sample;
import java.util.ArrayList;
import java.util.List;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
public class CombiningSample {
public static void main(String[] args) {
List<Mono<Boolean>> sources = new ArrayList<>();
/**
* Don't use {@link Mono#just}, if you expect execution on subscription time,
* instead of assembly time.
*/
sources.add(Mono.fromCallable(() -> true));
sources.add(Mono.fromCallable(() -> true));
sources.add(Mono.fromCallable(() -> false));
sources.add(Mono.fromCallable(() -> true));
sources.add(Mono.fromCallable(() -> false));
Mono<Boolean> finalMono = sources.stream()
.reduce(Mono.just(true), (previous, next) -> previous.flatMap(value -> value ? next : Mono.empty()));
/**
* End with complete signal, without on next.
*/
StepVerifier.create(finalMono)
.expectSubscription()
.expectComplete()
.verify();
/**
* End with false as fallback value.
*/
StepVerifier.create(finalMono.switchIfEmpty(Mono.just(false)))
.expectSubscription()
.expectNext(false)
.expectComplete()
.verify();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment