You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Thomas Neidhart (JIRA)" <ji...@apache.org> on 2012/07/26 21:59:35 UTC

[jira] [Updated] (COLLECTIONS-290) AllPredicate should be able to cast objects

     [ https://issues.apache.org/jira/browse/COLLECTIONS-290?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Thomas Neidhart updated COLLECTIONS-290:
----------------------------------------

    Fix Version/s:     (was: 4.0-beta-1)
                   4.0
    
> AllPredicate should be able to cast objects
> -------------------------------------------
>
>                 Key: COLLECTIONS-290
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-290
>             Project: Commons Collections
>          Issue Type: Improvement
>          Components: Functor
>    Affects Versions: 3.2
>            Reporter: Stephen Kestle
>            Assignee: Stephen Kestle
>             Fix For: 4.0
>
>   Original Estimate: 4h
>  Remaining Estimate: 4h
>
> When using the generified AllPredicate class (in it's current form), it requires instantiation with all predicates having generic supertypes of the resultant combination:
> {code}public static <T> Predicate<T> getInstance(Predicate<? super T> ... predicates){code}
> However, if one of the predicates was an InstanceOfPredicate, it could mean that every other predicate is able to be a super of the type that the instanceOfPredicate returns true for.
> I propose that InstanceOfPredicate has a signature like:
> {code}public final class InstanceofPredicate<T,CT> implements Predicate<T>, Serializable
> ...
> public static <T,CT> InstanceofPredicate<T,CT> getInstance(Class<CT> type) {code}
> Where CT is the Type of the class that will be matched by the predicate.
> AllPredicate should have constructors like:
> {code}public static <T,CT> AllPredicate<T,CT> getInstance(InstanceOfPredicate<?,CT>, Predicate<? super CT> ... predicates)
> public static <T,CT> AllPredicate<T,CT> getInstance(Class<CT>, Predicate<? super CT> ... predicates){code}
> This would allow far nicer constructs of anonymous or simple predicates, and better type safety.
> There would also be an additional method on AllPredicate:
> {code}public CT cast(Object matched){code}
> That could be used as follows:
> {code}Predicate<Integer> predicate = AllPredicate.instanceOf(Integer.class, ...);
> Object o= ...;
> if (predicate.evaluate(o)){
>     Integer i = predicate.cast(o);
>      //perform action
> }{code}
> Alternatively there could be a different method that attempts to coerce the type:
> {code}public CT transform(T t){
>     if (evaluate(t)){
>         return clazz.cast(t);  //clazz is of type Class<CT>
>     }
>     return null;
> }{code}
> This could be used as follows:
> {code}Predicate<Integer> predicate = AllPredicate.instanceOf(Integer.class, ...);
> Object o= ...;
> Integer i = predicate.transform(o)
> if (i != null){
>     //perform action
> }{code}
> I prefer the cast() method; however, it would not re-evaluate the object, so would need documentation to be used correctly.  The second method would allow it to be used as a transformer, but would substitute boolean checking to null checking.
> Perhaps both  are warranted.  (The reason for the convenience methods are to not generate type-coercion warnings).

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira