Q:

Project Reactor verification of conditions

tell me how to work with conditions in Reactive.
I have simple DTO SignInRequest and signIn method.
public class SignInRequest {

  private String userName;
  private String password;
}

public Mono<SignInResponse> signIn(SignInRequest signInRequest) {
    boolean isValid = StringUtils.isEmailValid(signInRequest.getUserName());
    return userService.findByEmail(signInRequest.getUserName())
        .switchIfEmpty(Mono.error(new NotFoundException("User not found [email: " + signInRequest.getEmail() + "]")))
        .filter(user -> passwordService.validatePassword(user.getPassword(), signInRequest.getPassword()))
        .switchIfEmpty(Mono.error(new BadRequestException("Wrong email or password")))
        .flatMap(this::generateTokens);
}

How to correctly implement the following logic. 
If isValid == true then call the findByEmail method. Otherwise, call findByUsername

A:

Personally, I think the most readable implementation would be the following:
public Mono<SignInResponse> signIn(SignInRequest signInRequest) {
    return Mono
            .fromSupplier(() -> StringUtils.isEmailValid(signInRequest.getUserName()))
            .flatMap(isValid ->
                    isValid ? userService.findByEmail(signInRequest.getUserName())
                            : userService.findByUsername(signInRequest.getUserName())
            )
            .switchIfEmpty(Mono.error(new BadRequestException("Wrong email or password")))
            .flatMap(this::generateTokens);
}

