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

The following examples show how to use reactor.core.publisher.Flux#zip() . 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
Source File: FluxMergingTests.java    From spring-in-action-5-samples with Apache License 2.0 6 votes vote down vote up
@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
Source File: FluxMergingTests.java    From spring-in-action-5-samples with Apache License 2.0 6 votes vote down vote up
@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
Source File: R043_Zip.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
@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
Source File: R053_Concurrency.java    From reactor-workshop with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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
Source File: R043_Zip.java    From reactor-workshop with GNU General Public License v3.0 5 votes vote down vote up
@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
Source File: BeanFactoryAwareFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@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
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@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
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@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
Source File: BeanFactoryAwareFunctionRegistryMultiInOutTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@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
Source File: RepeaterApplication.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@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
Source File: CombiningPublishersIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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 File: EventRetriever.java    From james-project with 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));
}