commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Guy Rouillier <guyr-...@burntmail.com>
Subject Re: Determine if an object is in a list
Date Tue, 01 Dec 2009 04:38:37 GMT
James Carman wrote:
> It's not that hard to create your own predicate that does what you
> want.  We did that for our project.  Here are the two classes we use
> (the first one is a useful superclass for property value-based
> predicates):

James, thank you.  This is indeed what I was looking for.  Mind if I 
include your classes with a feature request? (with attribution, of 
course.)  I think this is basic functionality that should be included 
with Commons Collections.

> 
> public abstract class AbstractPropertyValuePredicate implements Predicate {
> 
>     private final String propertyName;
> 
>     public AbstractPropertyValuePredicate (final String propertyName)  {
>         this.propertyName = propertyName;
>     }
> 
>     public final boolean evaluate( Object o )
>     {
>         try
>         {
>             final Object propertyValue = PropertyUtils.getProperty( o,
> propertyName );
>             return evaluatePropertyValue(propertyValue);
>         }
>         catch( IllegalAccessException e )
>         {
>             throw new RuntimeException(e);
>         }
>         catch( InvocationTargetException e )
>         {
>             throw new RuntimeException(e);
>         }
>         catch( NoSuchMethodException e )
>         {
>             throw new RuntimeException(e);
>         }
>     }
> 
>     protected abstract boolean evaluatePropertyValue(Object value);
> }
> 
> public class PropertyValuePredicate extends AbstractPropertyValuePredicate {
>     private final Object targetValue;
> 
>     public PropertyValuePredicate (String propertyName, Object targetValue) {
>         super( propertyName );
>         this.targetValue = targetValue;
>     }
> 
>     protected boolean evaluatePropertyValue( Object value )
>     {
>         return targetValue == value || (targetValue != null &&
> targetValue.equals( value ));
>     }
> }
> On Mon, Nov 30, 2009 at 10:37 PM, Guy Rouillier <guyr-ml1@burntmail.com> wrote:
>> I have a list of objects of class X.  X has a unique identifier property  P.
>>  Given a value p, I want to see if the list contains an object where x.P =
>> p.  Very simple, I ended up using a for loop to iterate over the objects in
>> listX, breaking when I found the first occurrence.
>>
>> Before doing that, however, I tried to find something in Commons that would
>> do this for me.  The closest I could find was
>> org.apache.commons.collections.CollectionUtils.find(java.util.Collection
>> collection, Predicate predicate).  However, I couldn't find a simple
>> PropertyPredicate that allowed me to say "look for objects whose property P
>> is p".  That seems like one of the most obvious uses of find, so I believe
>> I'm just not looking in the right place.
>>
>> I'd appreciate a pointer to where I can find this.  Thanks.
>>
>> --
>> Guy Rouillier
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 
> 


-- 
Guy Rouillier

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message