You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Attila Szegedi <sz...@freemail.hu> on 2004/02/11 14:43:36 UTC

Bean property comparator

Hi folks,

there's one comparator I've been missing from the commons-collection: a
comparator that uses a specified bean property of objects to perform the
comparisons.

Basically, it works like this: if you construct an instance with:

Comparator c =
  new BeanPropertyComparator(Customer.class, "birthDate");

c will use the natural ordering on the values of Customer.getBirthDate() to
compare Customer objects.

There's an alternative constructor to specify an embedded comparator for
comparing the retrieved property values:

Comparator c =
    new BeanPropertyComparator(
       Customer.class, "birthDate", new ReverseComparator());

Admittedly, the class is fairly trivial, but I already have it implemented
and tested - would the project be interested in the contribution?

Attila.




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


Re: Bean property comparator

Posted by Attila Szegedi <sz...@freemail.hu>.
"Brian S O'Neill" <br...@earthlink.net> wrote in message
news:402A649E.9090103@earthlink.net...
> An alternative BeanComparator, that runs even faster is available in the
> TeaTrove project.
>
>
http://teatrove.sourceforge.net/javadoc/com/go/trove/util/BeanComparator.html
>
> It relies on a builder patterm that auto-generates bytecode to avoid
> reflection calls and rule interpretation overhead.
>


I have looked into it, and it is just cool...
Thanks.
Attila.




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


Re: Bean property comparator

Posted by Brian S O'Neill <br...@earthlink.net>.
An alternative BeanComparator, that runs even faster is available in the 
TeaTrove project.

http://teatrove.sourceforge.net/javadoc/com/go/trove/util/BeanComparator.html

It relies on a builder patterm that auto-generates bytecode to avoid 
reflection calls and rule interpretation overhead.

Attila Szegedi wrote:

>Hm, you're right. Oversight on my part. Anyway, I think I'll keep using my
>own comparator, as the one now existing in Commons relies on
>PropertyUtils.getProperty(), and it's implementation has a bit too high
>overhead for my taste - all those indexOf(), substring() and instanceof
>calls when all I want is invoke a single method reflectively (that got
>cached at the comparator construction time). I believe it can significantly
>impact execution time when sorting large lists...
>
>Anyway, thanks for the pointer.
>
>Attila.
>
>"Henri Yandell" <ba...@generationjava.com> wrote in message
>news:Pine.LNX.4.44.0402111103330.25708-100000@umbongo.flamefew.net...
>  
>
>>It's a part of BeanUtils, org.apache.commons.beanutils.BeanComparator I
>>think.
>>
>>It doesn't take the class in the constructor, so will work on any class
>>with getBirthDate().
>>
>>Hen
>>
>>On Wed, 11 Feb 2004, Attila Szegedi wrote:
>>
>>    
>>
>>>Hi folks,
>>>
>>>there's one comparator I've been missing from the commons-collection: a
>>>comparator that uses a specified bean property of objects to perform the
>>>comparisons.
>>>
>>>Basically, it works like this: if you construct an instance with:
>>>
>>>Comparator c =
>>>  new BeanPropertyComparator(Customer.class, "birthDate");
>>>
>>>c will use the natural ordering on the values of Customer.getBirthDate()
>>>      
>>>
>to
>  
>
>>>compare Customer objects.
>>>
>>>There's an alternative constructor to specify an embedded comparator for
>>>comparing the retrieved property values:
>>>
>>>Comparator c =
>>>    new BeanPropertyComparator(
>>>       Customer.class, "birthDate", new ReverseComparator());
>>>
>>>Admittedly, the class is fairly trivial, but I already have it
>>>      
>>>
>implemented
>  
>
>>>and tested - would the project be interested in the contribution?
>>>
>>>Attila.
>>>
>>>
>>>
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>>
>>>      
>>>
>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
>  
>

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


Re: Bean property comparator

Posted by Attila Szegedi <sz...@freemail.hu>.
Hm, you're right. Oversight on my part. Anyway, I think I'll keep using my
own comparator, as the one now existing in Commons relies on
PropertyUtils.getProperty(), and it's implementation has a bit too high
overhead for my taste - all those indexOf(), substring() and instanceof
calls when all I want is invoke a single method reflectively (that got
cached at the comparator construction time). I believe it can significantly
impact execution time when sorting large lists...

Anyway, thanks for the pointer.

Attila.

"Henri Yandell" <ba...@generationjava.com> wrote in message
news:Pine.LNX.4.44.0402111103330.25708-100000@umbongo.flamefew.net...
>
> It's a part of BeanUtils, org.apache.commons.beanutils.BeanComparator I
> think.
>
> It doesn't take the class in the constructor, so will work on any class
> with getBirthDate().
>
> Hen
>
> On Wed, 11 Feb 2004, Attila Szegedi wrote:
>
> > Hi folks,
> >
> > there's one comparator I've been missing from the commons-collection: a
> > comparator that uses a specified bean property of objects to perform the
> > comparisons.
> >
> > Basically, it works like this: if you construct an instance with:
> >
> > Comparator c =
> >   new BeanPropertyComparator(Customer.class, "birthDate");
> >
> > c will use the natural ordering on the values of Customer.getBirthDate()
to
> > compare Customer objects.
> >
> > There's an alternative constructor to specify an embedded comparator for
> > comparing the retrieved property values:
> >
> > Comparator c =
> >     new BeanPropertyComparator(
> >        Customer.class, "birthDate", new ReverseComparator());
> >
> > Admittedly, the class is fairly trivial, but I already have it
implemented
> > and tested - would the project be interested in the contribution?
> >
> > Attila.
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> >




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


Re: Bean property comparator

Posted by Henri Yandell <ba...@generationjava.com>.
It's a part of BeanUtils, org.apache.commons.beanutils.BeanComparator I
think.

It doesn't take the class in the constructor, so will work on any class
with getBirthDate().

Hen

On Wed, 11 Feb 2004, Attila Szegedi wrote:

> Hi folks,
>
> there's one comparator I've been missing from the commons-collection: a
> comparator that uses a specified bean property of objects to perform the
> comparisons.
>
> Basically, it works like this: if you construct an instance with:
>
> Comparator c =
>   new BeanPropertyComparator(Customer.class, "birthDate");
>
> c will use the natural ordering on the values of Customer.getBirthDate() to
> compare Customer objects.
>
> There's an alternative constructor to specify an embedded comparator for
> comparing the retrieved property values:
>
> Comparator c =
>     new BeanPropertyComparator(
>        Customer.class, "birthDate", new ReverseComparator());
>
> Admittedly, the class is fairly trivial, but I already have it implemented
> and tested - would the project be interested in the contribution?
>
> Attila.
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>


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