Java Code Examples for reactor.core.publisher.Flux#zip()

The following examples show how to use reactor.core.publisher.Flux#zip() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void zipFluxes() {
  Flux<String> characterFlux = Flux
      .just("Garfield", "Kojak", "Barbossa");
  Flux<String> foodFlux = Flux
      .just("Lasagna", "Lollipops", "Apples");
  
  Flux<Tuple2<String, String>> zippedFlux = 
      Flux.zip(characterFlux, foodFlux);
  
  StepVerifier.create(zippedFlux)
        .expectNextMatches(p -> 
            p.getT1().equals("Garfield") && 
            p.getT2().equals("Lasagna"))
        .expectNextMatches(p -> 
            p.getT1().equals("Kojak") && 
            p.getT2().equals("Lollipops"))
        .expectNextMatches(p -> 
            p.getT1().equals("Barbossa") && 
            p.getT2().equals("Apples"))
        .verifyComplete();
}
 
Example 2
@Test
public void zipFluxesToObject() {
  Flux<String> characterFlux = Flux
      .just("Garfield", "Kojak", "Barbossa");
  Flux<String> foodFlux = Flux
      .just("Lasagna", "Lollipops", "Apples");
  
  Flux<String> zippedFlux = 
      Flux.zip(characterFlux, foodFlux, (c, f) -> c + " eats " + f);
  
  StepVerifier.create(zippedFlux)
        .expectNext("Garfield eats Lasagna")
        .expectNext("Kojak eats Lollipops")
        .expectNext("Barbossa eats Apples")
        .verifyComplete();
}
 
Example 3
@Test
public void customCombinator() throws Exception {
	//given
	final Flux<Integer> nums = Flux.just(1, 2, 3);
	final Flux<String> strs = Flux.just("a", "bc", "def");

	//when
	final Flux<Double> doubles = Flux.zip(
			nums,
			strs,
			(n, s) -> n * s.length() * 2.0
	);

	//then
	doubles
			.as(StepVerifier::create)
			.expectNext(2.0, 8.0, 18.0)
			.verifyComplete();
}
 
Example 4
/**
 * TODO Generate list of tuples, but this time by zipping ({@link Flux#zip(Publisher, Publisher)})
 * stream of domains with stream of responses.
 * Why does it fail?
 */
@Test
public void zipIsBroken() throws Exception {
	//given
	final Flux<Domain> domains = Domains.all();

	//when
	Flux<Html> responses = null; // TODO
	final Flux<Tuple2<URI, Html>> tuples = Flux.zip(
			domains.map(Domain::getUri),
			responses
	);

	//then
	final List<Tuple2<URI, Html>> list = tuples
			.collectList()
			.block();

	assertThat(list)
			.hasSize(500)
			.contains(Tuples.of(new URI("http://archive.org"), new Html("<html><title>http://archive.org</title></html>")))
			.contains(Tuples.of(new URI("http://github.com"), new Html("<html><title>http://github.com</title></html>")));

	list.forEach(pair ->
			assertThat(pair.getT2().getRaw()).contains(pair.getT1().getHost()));
}
 
Example 5
@Test
public void zipTwoStreams() throws Exception {
	//given
	final Flux<Integer> nums = Flux.just(1, 2, 3);
	final Flux<String> strs = Flux.just("a", "b");

	//when
	final Flux<Tuple2<Integer, String>> pairs = nums.zipWith(strs);
	final Flux<Tuple2<Integer, String>> pairs2 = Flux.zip(nums, strs);  //same thing

	//then
	pairs.subscribe(p -> log.info("Pair: {}", p));
}
 
Example 6
@Bean
public Function<Tuple2<Flux<String>, Flux<Integer>>, Flux<String>> multiInputSingleOutputViaReactiveTuple() {
	return tuple -> {
		Flux<String> stringStream = tuple.getT1();
		Flux<Integer> intStream = tuple.getT2();
		return Flux.zip(stringStream, intStream, (string, integer) -> string + "-" + integer);
	};
}
 
Example 7
@Bean
public Function<Tuple2<Flux<String>, Flux<Integer>>, Flux<String>> multiInputSingleOutputViaReactiveTuple() {
	return tuple -> {
		Flux<String> stringStream = tuple.getT1();
		Flux<Integer> intStream = tuple.getT2();
		return Flux.zip(stringStream, intStream, (string, integer) -> string + "-" + integer);
	};
}
 
Example 8
@Bean
public BiFunction<Flux<String>, Flux<Integer>, Flux<String>> multiInputSingleOutputViaBiFunction() {
	return (in1, in2) -> {
		Flux<String> stringStream = in1;
		Flux<Integer> intStream = in2;
		return Flux.zip(stringStream, intStream, (string, integer) -> string + "-" + integer);
	};
}
 
Example 9
@Bean
public Function<Tuple2<Flux<CartEvent>, Flux<CheckoutEvent>>, Flux<OrderEvent>> thomas() {
	return tuple -> {
		Flux<CartEvent> cartEventStream = tuple.getT1();
		Flux<CheckoutEvent> checkoutEventStream = tuple.getT2();

		return Flux.zip(cartEventStream, checkoutEventStream, (cartEvent, checkoutEvent) -> {
			OrderEvent oe = new OrderEvent();
			oe.setOrderEvent(cartEvent.toString() + "- " + checkoutEvent.toString());
			return oe;
		});
	};
}
 
Example 10
@Bean
public Function<Tuple2<Flux<CartEvent>, Flux<CheckoutEvent>>, Flux<OrderEvent>> fn() {
	return tuple -> {
		Flux<CartEvent> cartEventStream = tuple.getT1();
		Flux<CheckoutEvent> checkoutEventStream = tuple.getT2();

		return Flux.zip(cartEventStream, checkoutEventStream, (cartEvent, checkoutEvent) -> {
			OrderEvent oe = new OrderEvent();
			oe.setOrderEvent(cartEvent.toString() + "- " + checkoutEvent.toString());
			return oe;
		});
	};
}
 
Example 11
@Test
public void givenFluxes_whenZipIsInvoked_thenZip() {
    Flux<Integer> fluxOfIntegers = Flux.zip(
            evenNumbers, 
            oddNumbers, 
            (a, b) -> a + b);
    
    StepVerifier.create(fluxOfIntegers)
    .expectNext(3)
    .expectNext(7)
    .expectComplete()
    .verify();
}
 
Example 12
Source Project: james-project   File: EventRetriever.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Flux<Tuple3<Group, Event, EventDeadLetters.InsertionId>> retrieveEvents(EventDeadLetters deadLetters) {
    return Flux.zip(Mono.just(group), deadLetters.failedEvent(group, insertionId), Mono.just(insertionId));
}