I have a list of sources from a JSON file stored in S3. I need to provide search filters on few fields of the list. I have created a Builder class with all the needed fields on which the data can be filtered.
Now I want to use Java streams and apply the filters on the data based on the parameters the users will provide. Users can provide all the 6 parameters or only pass 2 or 3 parameters based on their need. For example user provides only 2 parameters out of 6 I should be able to filter the data based on the 2 parameters given and ignore the 4 parameters where the value is null.
public class ListOfFields {
private String country;
@JsonProperty("product")
private String product;
private String transactionId;
private String sourceBy;
private String category;
private String specialTag;
private String organicTag;
private String successTag;
private String speedTag;
private String foodTag;
private String costPrice;
private String fruitType;
private String fruitVendorId;
}
The below is the builder class
public class ProductClass {
@Builder.Default
private String country = 'UnitedStates';
private String sourceBy;
private Boolean specialTag;
private String category;
private Boolean priceTag;
private String fruitType;
}
The below method where user can make a call to get the product
public String getProduct(ProductClass productQuery) {
List<ListOfFields> result = listOfFields // (the declaration is made before in the class as private List<ListOfFields> listOfFields;)
.stream().filter(productQuery::match)
.collect(Collectors.toList());
How do I come up with dynamic predicate as the match function above? I should be able to filter on the parameters provided by the user be it 1 or 2 or 3 or 4 ... and not using the parameters where the user did not pass any value in the filter criteria.