I'm trying to create a finder which takes several predicates and reduces them:
public static <T extends BusinessInterface> Collection<T> findOr(
Context pContext, Class<T> pClass, Predicate<? super T>... pPredicates) {
Predicate<? super T> lReducedPredicate =
Arrays.asList(pPredicates).stream().reduce(Predicate::or).orElse(r -> false);
return find(pContext, pClass, lReducedPredicate);
}
Unfortunately I get following compiler error:
Predicate lReducedPredicate = Arrays.asList(pPredicates).stream().reduce(Predicate::or).orElse(r -> false); incompatible types: Predicate cannot be converted to Predicate where T is a type-variable: T extends BusinessInterface declared in method findOr(Context,Class,Predicate...) where CAP#1,CAP#2 are fresh type-variables: CAP#1 extends Object super: T from capture of ? super T CAP#2 extends Object super: T from capture of ? super T
I get not errors in Eclipse and I have no idea what is going wrong.
Any help is really appreciated :).
One way to solve this is using
public static <T extends BusinessInterface> Collection<T> findOr(
Context pContext, Class<T> pClass, Predicate<? super T>... pPredicates) {
Predicate<? super T> lReducedPredicate = Arrays.asList(pPredicates).stream()
.reduce((a,b) -> t -> a.test(t) || b.test(t)).orElse(r -> false);
return find(pContext, pClass, lReducedPredicate);
}
While you can’t invoke the or
method on a Predicate<? super T>
instance with another Predicate<? super T>
as argument, you can create the same function, or
would return, yourself, as passing a T
instance to either test
method is valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With