commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thomas Neidhart (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (COLLECTIONS-537) PredicateUtils (all|any)Predicate type misbehaviour Array vs. Collection
Date Fri, 26 Dec 2014 13:30:13 GMT

    [ https://issues.apache.org/jira/browse/COLLECTIONS-537?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14259075#comment-14259075
] 

Thomas Neidhart commented on COLLECTIONS-537:
---------------------------------------------

Yes indeed, the correct signature should be:

{code}
    Predicate<T> anyPredicate(final Collection<? extends Predicate<? super T>>
predicates)
{code}

This change will require some more changes in other places too for sake of consistency.
The test case to demonstrate the change:

{code}
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.PredicateUtils;

public class MyTest {


    public static class A {
        int val;
        
        public A(int val) {
            this.val = val;
        }
        
        public String toString() {
            return "A";
        }
    }
    
    public static class B extends A {
        
        public B(int val) {
            super(val);
        }
        
        public String toString() {
            return "B";
        }
    }
    
    public static class MyPredicate implements Predicate<A> {

        private int eval;
        
        public MyPredicate(int eval) {
            this.eval = eval;
        }

        @Override
        public boolean evaluate(A object) {
            return object.val < eval;
        }
        
    }

    public static void main(String[] args) {
        MyPredicate p1 = new MyPredicate(10);
        MyPredicate p2 = new MyPredicate(20);

        Predicate<B> anyPredicate = PredicateUtils.anyPredicate(p1, p2);
        
        System.out.println(anyPredicate.evaluate(new B(10)));
        
        List<Predicate<A>> list = new ArrayList<Predicate<A>>();
        list.add(p1);
        list.add(p2);
        
        Predicate<B> anyPredicate2 = PredicateUtils.anyPredicate(list);
        
        System.out.println(anyPredicate2.evaluate(new B(10)));
    }
}
{code}

> PredicateUtils (all|any)Predicate type misbehaviour Array vs. Collection
> ------------------------------------------------------------------------
>
>                 Key: COLLECTIONS-537
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-537
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Functor
>    Affects Versions: 4.0
>            Reporter: Frank Jakop
>
> Migrating from collections-generic to collections4 we encountered a type problem with
PredicateUtils. When you look at the method anyPredicate(), the signature with array is typed
with "Predicate<? super T>" whereas the signature with Collection is typed "? extends
Predicate<T>", so the both methods are not equivalent.
> We think both methods should have the same types, so it would not break compatibility
with a lot of legacy code.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message