zip 연산자는 여러 publisher를 모아서 변환할 때 사용, 모여진 pulbisher는 Tuple 로 전달
Flux.zip(Flux.just("1", "2", "3"), Flux.just("4", "5", "6")).map(t -> t.getT1() + t.getT2());
firstWithValue 는 여러 publisher중 가장 먼저 값이 전달된 publisher 사용
주로 작업 성공 또는 실패만 관심있을 때 사용
Reactive Streams이 onNext에서 null 을 허용하지 않기 때문에 t의 값이 null인 경우 empty Publisher 전달
publisher가 empty 한 경우 지정한 대체 publisher 전달
flux를 List로 변환하고 Mono<List< T >> 형태로 반환
package io.pivotal.literx;
//generic imports to help with simpler IDEs (ie tech.io)
import java.util.*;
import java.util.function.*;
import java.time.*;
import io.pivotal.literx.domain.User;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;
/**
* Learn how to use various other operators.
*
* @author Sebastien Deleuze
*/
public class Part08OtherOperations {
//========================================================================================
// TODO Create a Flux of user from Flux of username, firstname and lastname.
Flux<User> userFluxFromStringFlux(Flux<String> usernameFlux, Flux<String> firstnameFlux, Flux<String> lastnameFlux) {
return Flux.zip(usernameFlux, firstnameFlux, lastnameFlux)
.map(t -> new User(t.getT1(), t.getT2(), t.getT3()));
}
//========================================================================================
// TODO Return the mono which returns its value faster
Mono<User> useFastestMono(Mono<User> mono1, Mono<User> mono2) {
return Mono.firstWithValue(mono1, mono2);
}
//========================================================================================
// TODO Return the flux which returns the first value faster
Flux<User> useFastestFlux(Flux<User> flux1, Flux<User> flux2) {
return Flux.firstWithValue(flux1, flux2);
}
//========================================================================================
// TODO Convert the input Flux<User> to a Mono<Void> that represents the complete signal of the flux
Mono<Void> fluxCompletion(Flux<User> flux) {
return flux.ignoreElements().then();
}
//========================================================================================
// TODO Return a valid Mono of user for null input and non null input user (hint: Reactive Streams do not accept null values)
Mono<User> nullAwareUserToMono(User user) {
return Mono.justOrEmpty(user);
}
//========================================================================================
// TODO Return the same mono passed as input parameter, expect that it will emit User.SKYLER when empty
Mono<User> emptyToSkyler(Mono<User> mono) {
return mono.switchIfEmpty(Mono.just(User.SKYLER));
}
//========================================================================================
// TODO Convert the input Flux<User> to a Mono<List<User>> containing list of collected flux values
Mono<List<User>> fluxCollection(Flux<User> flux) {
return flux.collectList();
}
}