开发者

Why do Predicates not seem to compile as expected in some compilers?

I recently wrote some code to use Predicates from the Guava library to compose complex predicates as filters for a result set. I constructed a class called PredicateFamily which represents a collection of predicates for a specific domain within the result set. The Predicate family also tracks which of the predicates are active and the following method is supposed to compose a single predicate from all the separate family objects.

/**
 * This method will AND together all the families and OR within the families
 * 
 * @param families
 * @return
 */
public static <E> Predicate<E> sumPredicates(Iterable<PredicateFamily<E>> families) {
    Predicate<E> ret = Predicates.alwaysTrue();

    for (PredicateFamily<E> family : families) {
        if (family.hasActivePredicates()) {
                            // family.getActive() returns List<Predicate<E>>
            Predicate<E> or = Predicates.or(family.getActive());
            ret = Predicates.and(ret, or);
        }
    }
    return ret;
}

This worked swimmingly when running it locally, but When it came to running this through Hudson I got the following baffling error:

[javac] symbol  : method and(com.google.common.base.Predicate<T>,com.google.common.base.Predicate<T>)
[javac] location: class com.google.common.base.Predicates
[javac]                 ret = Predicates.and(ret, or);
[javac]                                                 ^

(that caret should be under the opening parenthesis of Predicates.and)

This was rather confusing, from what I can tell, this should satisfy the signature of Predicates.and. Checking Hudson I found that it was running under 1.6.0_18 on Ub开发者_JAVA技巧untu and compiler was set to 1.5.0_22.

After some investigation we found that the following code satisfies the tests and compiles, however we lose type-safety in doing so.

public static <E> Predicate<E> sumPredicates(Iterable<PredicateFamily<E>> families) {
    Predicate<E> ret = Predicates.alwaysTrue();

    for (PredicateFamily<E> family : families) {
        if (family.hasActivePredicates()) {
            Predicate<E> or = Predicates.or(family.getActive());
            ret = Predicates.and(Arrays.asList(ret, or));
        }
    }
    return ret;
}

Can anyone offer any ideas as to why this didn't work? I'd really like to know .

Edit: Just for information, this was running under Guava r06, however checking the change log from r07 I do not see a change in the signature of Predicates.and.


A little late, but... I believe that this may be fixed by putting in the generic type :

ret = Predicates.<E>and( ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜