I have two prodicates :
Predicate<CategoryModel> predicate1 = NavigationCategoryModel.class::isInstance;
Predicate<CategoryModel> predicate2 = BrandCategoryModel.class::isInstance;
With and if statement , how can I identify which predicate am I using ? I'm trying to do something like this but obviously isn't compiling :
if(predicate1.equals(NavigationCategoryModel.class::isInstance)){
}
if(predicate1==NavigationCategoryModel.class::isInstance){
}
Any hint ? I'm quite new to Java 8 lambdas
This is the code of the Pojos (simple inheritance for test purposes):
public class CategoryModel {
}
public class NavigationCategoryModel extends CategoryModel{
}
public class BrandCategoryModel extends CategoryModel {
}
You should use test
method on Predicates. And, you've to provide the object to perform validation instead of actual method reference
predicate.test(object)
Documentation: Predicate#test
For your problem, you can test if predicate1 returns true when object is of type NavigationCategoryModel
as below:
predicate1.test(new NavigationCategoryModel()) // returns true
Similarly, for BrandCategoryModel
, use:
predicate2.test(new BrandCategoryModel()) // returns true
If you want to test that object matches either of two, you can combine both the predicates like:
predicate1.or(predicate2).test(new NavigationCategoryModel()) // returns true
predicate1.or(predicate2).test(new BrandCategoryModel()) // returns true