You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Guy Rouillier <gu...@burntmail.com> on 2009/12/01 04:37:06 UTC

Determine if an object is in a list

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


Re: Determine if an object is in a list

Posted by Guy Rouillier <gu...@burntmail.com>.
Brent Worden wrote:
> On Mon, Nov 30, 2009 at 10:38 PM, Guy Rouillier <gu...@burntmail.com> wrote:
>> 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.
>>
> 
> I think commons-beanutils has the predicate you need.
> 
> http://commons.apache.org/beanutils/v1.8.2/apidocs/org/apache/commons/beanutils/BeanPropertyValueEqualsPredicate.html
> 

Brent, thanks, that is exactly what I want!  Complete with an example 
relevant to my need, no less.  Interesting that this one predicate is 
packaged with BeanUtils instead of Collections, since it depends 
completely on the Collections classes.  I would not have thought to look 
in BeanUtils for a Collections predicate, so I appreciate you pointing 
it out.

-- 
Guy Rouillier

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


Re: Determine if an object is in a list

Posted by James Carman <ja...@carmanconsulting.com>.
I was looking for that.  It used to be in collections I believe.

On Tue, Dec 1, 2009 at 10:43 AM, Brent Worden <br...@gmail.com> wrote:
> On Mon, Nov 30, 2009 at 10:38 PM, Guy Rouillier <gu...@burntmail.com> wrote:
>> 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.
>>
>
> I think commons-beanutils has the predicate you need.
>
> http://commons.apache.org/beanutils/v1.8.2/apidocs/org/apache/commons/beanutils/BeanPropertyValueEqualsPredicate.html
>
> Brent
>
> ---------------------------------------------------------------------
> 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


Re: Determine if an object is in a list

Posted by Brent Worden <br...@gmail.com>.
On Mon, Nov 30, 2009 at 10:38 PM, Guy Rouillier <gu...@burntmail.com> wrote:
> 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.
>

I think commons-beanutils has the predicate you need.

http://commons.apache.org/beanutils/v1.8.2/apidocs/org/apache/commons/beanutils/BeanPropertyValueEqualsPredicate.html

Brent

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


Re: Determine if an object is in a list

Posted by James Carman <ja...@carmanconsulting.com>.
I don't know if it would get through (I'd probably vote against it and
I'm the author).  We don't want to get to "beanutilsy" in collections.
 Collections only provides the framework.

On Mon, Nov 30, 2009 at 11:38 PM, Guy Rouillier <gu...@burntmail.com> wrote:
> 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 <gu...@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
>
>

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


Re: Determine if an object is in a list

Posted by Guy Rouillier <gu...@burntmail.com>.
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 <gu...@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


Re: Determine if an object is in a list

Posted by James Carman <ja...@carmanconsulting.com>.
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):

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 <gu...@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