4

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.

4

1 回答 1

4
Predicate<Animal> provideIsSentientPredicate() 
{
    return cast( Predicates.instanceOf(Human.class) );
}

static <A, B extends A> Predicate<B> cast(Predicate<A> pa)
{
    @SuppressWarnings("unchecked")
    Predicate<B> pb = (Predicate)(pa);
    return pb;

    // we know it works correctly in practice.
    // or if you are a theorist, create a wrapper predicate
    //
    //     Predicate<B>
    //         boolean apply(B b)
    //             return pa.apply(b);
}

顺便说一句,Guava 没有理由不将该方法声明为

static <T> Predicate<T> instanceOf(Class<?> clazz)

与其他兄弟方法一致。

于 2013-04-22T19:05:06.667 回答