commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Henri Yandell <bay...@generationjava.com>
Subject Re: [COLLECTIONS/BEANUTILS] Is there a comparator that can dynamically pick a method to call on a bean?
Date Fri, 07 Jun 2002 14:50:00 GMT

I don't think there is one in Commons yet. I've had one for myself for a
bit and it can be a lifesaver sometimes.

BeanComparator bc = new BeanComparator("[1]"); was very sweet when I
realised that would work :) Not just Beans but also arrays/collections.

I'm +1 for a BeanComparator, +1 in that I plan to do it sometime but have
not had the time to learn the Jakarta BeanUtils and stop using my own.

Hen

On Fri, 7 Jun 2002, Eric Pugh wrote:

> Hi all,
>
> I have a series of Torque objects that I want to sort.  Sometimes I want to
> sort by MethodA, sometiems by MethodB.  (Basically mapping onto all the
> columns in my database).
>
> Right now, I have a comparator compare method that looks like this:
> public int compare( Object o1, Object o2 ) {
> 		Daughterboard db1 = (Daughterboard) o1;
> 		Daughterboard db2 = (Daughterboard) o2;
> 		int sort = 0;
> 		if ( sortMethod.equals( "ScintillationFileNumber" ) ) {
> 			/*
> 			 *  if ( db1 == null == db2 ) {
> 			 *  return 0;
> 			 *  }
> 			 *  else if (db1 == null &&
> 			 */
> 			sort = Strings.clean( db1.getScintillationFileNumber() ).compareTo(
> Strings.clean( db2.getScintillationFileNumber() ) );
>
> 		}
> 		else if ( sortMethod.equals( "DaughterboardId" ) ) {
> 			sort = db1.getDaughterboardId().compareTo( db2.getDaughterboardId() );
> 		}
> 		if ( sortPolarity.equals( "desc" ) ) {
> 			sort = sort * -1;
> 		}
>
> 		return sort;
> 	}
>
> What I really want to do is pass in a sortMethod like
> ScintillationFileNumber, and dynamically call the objects getter for that
> name.  If I wrap my object in a WrapDynaBean, and use that to call the data,
> will is actually change the ordering of my objects?  Or should I just use
> the WrapDynaBean locally inside of my compare method to facilitate calling
> the methods?
>
> First time using BeanUtils, but it looks great..
>
> Lastly, would a comparator like this be something of general interest to add
> to the collections list of comparators?
>
> Eric
>
>
>
> --
> To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>
>
>


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


Mime
View raw message