I have a Predicate<Object>
and need an equivalent Predicate<Animal>
.
Predicate<Animal> provideIsSentientPredicate() {
// Won't compile -- cannot convert from Predicate<Object> to Predicate<Animal>
return Predicates.instanceOf(Human.class);
}
Predicates are contravariant, so converting a Predicate<Object>
to a Predicate<Animal>
is safe. Is there clean and readable way to convert from a Predicate<Object>
to a Predicate<Animal>
(e.g. without suppressing warnings)?
I'd prefer not to change my method's type signature to return a Predicate<? super Animal>
or Predicate<Object>
unless someone convinces me that is the correct thing to do.