How we can compare two values of flux using springreactor

Viewed 1356

Currently I am new in reactive programming , I have added data in 2 documents, so currently what I am trying to do is to return only those data to client whose tokenIdentifier is same in both document.

Please refer the code below:

I have 2 collection that has

package com.mainApp;

import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(value = "Token")
public class TokenData {

    @Id
    private String id;
    
    private String tokenIdentifier;
    
    private Date todayDate;
    
    
    public TokenData(String id, String tokenIdentifier, Date todayDate) {
        super();
        this.id = id;
        this.tokenIdentifier = tokenIdentifier;
        this.todayDate = todayDate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTokenIdentifier() {
        return tokenIdentifier;
    }

    public void setTokenIdentifier(String tokenIdentifier) {
        this.tokenIdentifier = tokenIdentifier;
    }

    public Date getTodayDate() {
        return todayDate;
    }

    public void setTodayDate(Date todayDate) {
        this.todayDate = todayDate;
    }

    
    

}
package com.mainApp;

import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(value = "TURCollection")
public class TURCollection {
    @Id
    private String id;
    
    private String turIdentifier;
    
    private String tokenIdentifier;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTurIdentifier() {
        return turIdentifier;
    }

    public void setTurIdentifier(String turIdentifier) {
        this.turIdentifier = turIdentifier;
    }

    public String getTokenIdentifier() {
        return tokenIdentifier;
    }

    public void setTokenIdentifier(String tokenIdentifier) {
        this.tokenIdentifier = tokenIdentifier;
    }
    
    

}

I have a controller which will return only those tokenData whose tokenData.getTokenIdentifier() == TURCollection.getTokenIdentifier().

So

@GetMapping(value = "/getAllToken")
    public Flux<TokenData> getToken(){
        /*List<TokenData> returnData = new ArrayList<TokenData>();
        List<TokenData> tokenData = tokenDataRepository.findAll().collectList().block();
        
        List<TURCollection> turCollection = turRepository.findAll().collectList().block();
        
        turCollection.forEach(tur -> {
            for(TokenData data : tokenData) {
                if(tur.getTokenIdentifier().equals(data.getTokenIdentifier())) {
                    returnData.add(data);
                }
            }
        });*/

but the block() code is not working in reactive programming Can anyone help how I can compare values of two flux in reactive way?

1 Answers

You can use a combination of Flux.collectList and Mono.flatMap.

Mono<List<TokenData>> tokenData = tokenDataRepository.findAll().collectList();
Mono<List<TURCollection>> turCollection = turRepository.findAll().collectList();
Mono<List<TokenData>> result = turCollection.flatMap(turs ->
  Set<String> ids = turs.stream().map(TURCollection::getId).collect(toSet());
  return tokenData.map(tokens ->
    tokens.stream().filter(token -> ids.contains(token.getId())).collect(toList())
  );
);
Related