You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Ivelin Ivanov <iv...@apache.org> on 2002/06/07 12:02:24 UTC

Re: [LATKA] Complex examples

Is there an example of Latka script which tests a dynamic web application:
form submit, session, etc.
The documentation only includes tests for static web sites and I couldn't
find more in CVS.

Thanks,

Ivelin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [COLLECTIONS/BEANUTILS] Is there a comparator that can dynamically pick a method to call on a bean?

Posted by Eric Pugh <ep...@upstate.com>.
Hi all,

Attached is my first attempt at submitting a BeanComparator, along with the
test case..  However, i am running into two issues becomes of my lack of
familiarty with beans that are causing me pain.  However, in my app,
everything is working great!  My testcase is attached.

1) the test suite fails because my test object which has a getBeanValue and
setBeanValue causes the WrapDynaBean class to throw an exception that
"value" has no read method..  Yet it does, and in other uses of the
BeanComparator in my code it works great.

2) All comparisions are string comparisons..  So when I compare Bigdecimals
2, 12, and 22, the orders is 12, 2, 22!  Do I need to hand in another class
to cast the objects to?

Also, I would love to see the ComparatorUtils, I created my own one to
handle the reversing of my sorts based on the text values ASC/DESC, and
removed them from my BeanComparator.

I have also attached my LowercaseComparator..  I was sorting columns of
String data, and noticed that if I didn't lowercase them, the A versus a
differenced caused funny looking ordering.  I also set up
LowercaseComparator and BeanComparator to work as decorators (similar to
ReverseComparator).  Lastly, for the bean comparator, I am using
PropertyUtils, so if you can pass in NESTED properties!  customer.name
results in getCustomer().getName() being returned!  I would love to add
jxPath converter as I build up some more experience!

Here is some sample code:

	public static Comparator sortedBean( String sortProperty, String
sortPolarity )
		throws java.lang.IllegalArgumentException {
		Comparator c = null;
		sortPolarity = sortPolarity.toUpperCase();
		if ( !sortPolarity.equals( ASC ) && !sortPolarity.equals( DESC ) ) {
			throw new java.lang.IllegalArgumentException( "The argument:" +
sortPolarity + " was invalid." );
		}
		if ( sortPolarity.equals( ASC ) ) {
			c = new BeanComparator( sortProperty, new LowercaseComparator() );
		}
		else {
			c = new ReverseComparator( new BeanComparator( sortProperty, new
LowercaseComparator() ) );

		}
		return c;
	}


Any suggestions/help would be much appreciated, and I would love to see
these added to the comparators available!

Eric

-----Original Message-----
From: Michael A. Smith [mailto:mas@apache.org]
Sent: Friday, June 07, 2002 3:07 PM
To: Jakarta Commons Developers List
Subject: RE: [COLLECTIONS/BEANUTILS] Is there a comparator that can
dynamically pick a method to call on a bean?


On Fri, 7 Jun 2002, Henri Yandell wrote:
> Sorry Eric, I'm not sure you got my question.
>
> BeanComparator = good, +1. I think it'd be great to commit a
> BeanComparator.
>
> The ASC/DESC bit is unnecessary I think due to ReverseComparator. This is
> an opinion though, I don't believe in ASC/DESC in Comparators. So I was
> just -1 on the Polarity part of your BeanComparator :)
>
> Morgan or Michael may want to veto that though :)

Neither of us can veto your veto.  We can try to twist your arm, but in
this case, I have no reason to: I agree with you.  The reverse
comparator can provide the complementary ascending or descending
behavior.

regards,
michael


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>

RE: [COLLECTIONS/BEANUTILS] Is there a comparator that can dynamically pick a method to call on a bean?

Posted by "Michael A. Smith" <ma...@apache.org>.
On Fri, 7 Jun 2002, Henri Yandell wrote:
> Sorry Eric, I'm not sure you got my question.
> 
> BeanComparator = good, +1. I think it'd be great to commit a
> BeanComparator.
> 
> The ASC/DESC bit is unnecessary I think due to ReverseComparator. This is
> an opinion though, I don't believe in ASC/DESC in Comparators. So I was
> just -1 on the Polarity part of your BeanComparator :)
> 
> Morgan or Michael may want to veto that though :)

Neither of us can veto your veto.  We can try to twist your arm, but in
this case, I have no reason to: I agree with you.  The reverse
comparator can provide the complementary ascending or descending
behavior.

regards,
michael


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [COLLECTIONS/BEANUTILS] Is there a comparator that can dynamically pick a method to call on a bean?

Posted by Henri Yandell <ba...@generationjava.com>.
Sorry Eric, I'm not sure you got my question.

BeanComparator = good, +1. I think it'd be great to commit a
BeanComparator.

The ASC/DESC bit is unnecessary I think due to ReverseComparator. This is
an opinion though, I don't believe in ASC/DESC in Comparators. So I was
just -1 on the Polarity part of your BeanComparator :)

Morgan or Michael may want to veto that though :)

Hen

On Fri, 7 Jun 2002, Eric Pugh wrote:

> Works for me..  For my application, if there was a good comparator, then I
> would probably wrap up my code in something that combined ReverseComparator
> and BeanMethodComparartor properly...
>
> I guess I was just kinda tossing it out as an example..  If there is
> interest in adding it to CVS, I will make the changes, change the package
> class and add licesnising etc....
>
> Eric
>
> -----Original Message-----
> From: Henri Yandell [mailto:bayard@generationjava.com]
> Sent: Friday, June 07, 2002 11:21 AM
> To: Jakarta Commons Developers List
> Subject: RE: [COLLECTIONS/BEANUTILS] Is there a comparator that can
> dynamically pick a method to call on a bean?
>
>
> -1 to the ASC/DESC bit. That shouldn't be in comparators in my opinion,
> instead use the ReverseComparator.
>
> What is Polarity vs Property?
>
> On Fri, 7 Jun 2002, Eric Pugh wrote:
>
> > I forged ahead..  I had a need to sort in asc/desc...  so I have added a
> > sortPolarity and sortProperty methods..
> >
> > Here is what I created...
> >
> > package com.upstate.util;
> >
> > import org.apache.commons.beanutils.WrapDynaBean;
> > import org.apache.commons.beanutils.*;
> >
> > import org.apache.log4j.Category;
> >
> > /**
> >  *  Description of the Class
> >  *
> >  *@author     epugh
> >  *@created    May 5, 2002
> >  */
> > public class BeanMethodComparator implements java.util.Comparator {
> >
> > 	public final static String ASC = "ASC";
> > 	public final static String DESC = "DESC";
> >
> > 	private static Category log = Category.getInstance(
> > BeanMethodComparator.class.getName() );
> > 	private String sortProperty;
> > 	private String sortPolarity = "ASC";
> >
> > 	/**  Constructor for the BeanMethodComparator object */
> > 	public BeanMethodComparator() { }
> >
> > 	/**
> > 	 *  Constructor for the BeanMetodComparator object
> > 	 *
> > 	 *@param  sortProperty  Description of Parameter
> > 	 *@param  sortPolarity  Description of Parameter
> > 	 */
> > 	public BeanMethodComparator( String sortProperty, String sortPolarity ) {
> > 		setSortProperty( sortProperty );
> > 		setSortPolarity( sortPolarity );
> > 	}
> >
> > 	/**
> > 	 *  Sets the sortProperty attribute of the SortDaughterboard object
> > 	 *
> > 	 *@param  sortProperty  The new sortProperty value
> > 	 */
> > 	public void setSortProperty( String sortProperty ) {
> > 		this.sortProperty = sortProperty;
> > 	}
> >
> > 	/**
> > 	 *  Sets the sortPolarity attribute of the SortDaughterboard object
> > 	 *
> > 	 *@param  sortPolarity                            The new sortPolarity
> > value.
> > 	 *      Can be either "ASC"/"DESC".
> > 	 *@exception  java.lang.IllegalArgumentException  Thrown if you pass in a
> > bad
> > 	 *      sortPolarity.
> > 	 */
> > 	public void setSortPolarity( String sortPolarity )
> > 		throws java.lang.IllegalArgumentException {
> > 		sortPolarity = sortPolarity.toUpperCase();
> > 		if ( sortPolarity.equals( ASC ) || sortPolarity.equals( DESC ) ) {
> > 			throw new java.lang.IllegalArgumentException( "The argument:" +
> > sortPolarity + " was invalid." );
> > 		}
> > 		this.sortPolarity = sortPolarity;
> > 	}
> >
> > 	/**
> > 	 *  Gets the sortPolarity attribute of the SortDaughterboard object
> > 	 *
> > 	 *@return    The sortPolarity value
> > 	 */
> > 	public String getSortPolarity() {
> > 		return sortPolarity;
> > 	}
> >
> >
> > 	/**
> > 	 *  Gets the sortProperty attribute of the SortDaughterboard object
> > 	 *
> > 	 *@return    The sortProperty value
> > 	 */
> > 	public String getSortProperty() {
> > 		return sortProperty;
> > 	}
> >
> > 	/**
> > 	 *  Description of the Method
> > 	 *
> > 	 *@param  o1  Description of Parameter
> > 	 *@param  o2  Description of Parameter
> > 	 *@return     Description of the Returned Value
> > 	 */
> > 	public int compare( Object o1, Object o2 ) {
> > 		try {
> > 			WrapDynaBean bean1 = new WrapDynaBean( o1 );
> > 			WrapDynaBean bean2 = new WrapDynaBean( o2 );
> >
> > 			Comparable value1 = (Comparable) bean1.get( sortProperty );
> > 			Comparable value2 = (Comparable) bean2.get( sortProperty );
> >
> > 			int sort = 0;
> >
> > 			if ( ( value1 == null ) & ( value2 == null ) ) {
> > 				sort = 0;
> > 			}
> > 			else if ( value1 == null & value2 != null ) {
> > 				sort = 1;
> > 			}
> > 			else if ( value1 != null & value2 == null ) {
> > 				sort = -1;
> > 			}
> > 			else {
> > 				sort = value1.compareTo( value2 );
> > 			}
> >
> > 			if ( sortPolarity.equals( DESC ) ) {
> > 				sort = sort * -1;
> > 			}
> >
> > 			return sort;
> > 		}
> >
> > 		catch ( Exception e ) {
> > 			log.error( "Problem in Sort. sortPolarity:" + sortPolarity + ",
> > sortProperty:" + sortProperty, e );
> > 			return 0;
> > 		}
> > 	}
> >
> > }
> >
> > Eric Pugh
> >
> >
> > -----Original Message-----
> > From: Henri Yandell [mailto:bayard@generationjava.com]
> > Sent: Friday, June 07, 2002 10:50 AM
> > To: Jakarta Commons Developers List
> > Subject: Re: [COLLECTIONS/BEANUTILS] Is there a comparator that can
> > dynamically pick a method to call on a bean?
> >
> >
> >
> > 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:
> > <ma...@jakarta.apache.org>
> > > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> > >
> > >
> >
> >
> > --
> > To unsubscribe, e-mail:
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> >
> >
> > --
> > To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> >
> >
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [COLLECTIONS/BEANUTILS] Is there a comparator that can dynamically pick a method to call on a bean?

Posted by Eric Pugh <ep...@upstate.com>.
Works for me..  For my application, if there was a good comparator, then I
would probably wrap up my code in something that combined ReverseComparator
and BeanMethodComparartor properly...

I guess I was just kinda tossing it out as an example..  If there is
interest in adding it to CVS, I will make the changes, change the package
class and add licesnising etc....

Eric

-----Original Message-----
From: Henri Yandell [mailto:bayard@generationjava.com]
Sent: Friday, June 07, 2002 11:21 AM
To: Jakarta Commons Developers List
Subject: RE: [COLLECTIONS/BEANUTILS] Is there a comparator that can
dynamically pick a method to call on a bean?


-1 to the ASC/DESC bit. That shouldn't be in comparators in my opinion,
instead use the ReverseComparator.

What is Polarity vs Property?

On Fri, 7 Jun 2002, Eric Pugh wrote:

> I forged ahead..  I had a need to sort in asc/desc...  so I have added a
> sortPolarity and sortProperty methods..
>
> Here is what I created...
>
> package com.upstate.util;
>
> import org.apache.commons.beanutils.WrapDynaBean;
> import org.apache.commons.beanutils.*;
>
> import org.apache.log4j.Category;
>
> /**
>  *  Description of the Class
>  *
>  *@author     epugh
>  *@created    May 5, 2002
>  */
> public class BeanMethodComparator implements java.util.Comparator {
>
> 	public final static String ASC = "ASC";
> 	public final static String DESC = "DESC";
>
> 	private static Category log = Category.getInstance(
> BeanMethodComparator.class.getName() );
> 	private String sortProperty;
> 	private String sortPolarity = "ASC";
>
> 	/**  Constructor for the BeanMethodComparator object */
> 	public BeanMethodComparator() { }
>
> 	/**
> 	 *  Constructor for the BeanMetodComparator object
> 	 *
> 	 *@param  sortProperty  Description of Parameter
> 	 *@param  sortPolarity  Description of Parameter
> 	 */
> 	public BeanMethodComparator( String sortProperty, String sortPolarity ) {
> 		setSortProperty( sortProperty );
> 		setSortPolarity( sortPolarity );
> 	}
>
> 	/**
> 	 *  Sets the sortProperty attribute of the SortDaughterboard object
> 	 *
> 	 *@param  sortProperty  The new sortProperty value
> 	 */
> 	public void setSortProperty( String sortProperty ) {
> 		this.sortProperty = sortProperty;
> 	}
>
> 	/**
> 	 *  Sets the sortPolarity attribute of the SortDaughterboard object
> 	 *
> 	 *@param  sortPolarity                            The new sortPolarity
> value.
> 	 *      Can be either "ASC"/"DESC".
> 	 *@exception  java.lang.IllegalArgumentException  Thrown if you pass in a
> bad
> 	 *      sortPolarity.
> 	 */
> 	public void setSortPolarity( String sortPolarity )
> 		throws java.lang.IllegalArgumentException {
> 		sortPolarity = sortPolarity.toUpperCase();
> 		if ( sortPolarity.equals( ASC ) || sortPolarity.equals( DESC ) ) {
> 			throw new java.lang.IllegalArgumentException( "The argument:" +
> sortPolarity + " was invalid." );
> 		}
> 		this.sortPolarity = sortPolarity;
> 	}
>
> 	/**
> 	 *  Gets the sortPolarity attribute of the SortDaughterboard object
> 	 *
> 	 *@return    The sortPolarity value
> 	 */
> 	public String getSortPolarity() {
> 		return sortPolarity;
> 	}
>
>
> 	/**
> 	 *  Gets the sortProperty attribute of the SortDaughterboard object
> 	 *
> 	 *@return    The sortProperty value
> 	 */
> 	public String getSortProperty() {
> 		return sortProperty;
> 	}
>
> 	/**
> 	 *  Description of the Method
> 	 *
> 	 *@param  o1  Description of Parameter
> 	 *@param  o2  Description of Parameter
> 	 *@return     Description of the Returned Value
> 	 */
> 	public int compare( Object o1, Object o2 ) {
> 		try {
> 			WrapDynaBean bean1 = new WrapDynaBean( o1 );
> 			WrapDynaBean bean2 = new WrapDynaBean( o2 );
>
> 			Comparable value1 = (Comparable) bean1.get( sortProperty );
> 			Comparable value2 = (Comparable) bean2.get( sortProperty );
>
> 			int sort = 0;
>
> 			if ( ( value1 == null ) & ( value2 == null ) ) {
> 				sort = 0;
> 			}
> 			else if ( value1 == null & value2 != null ) {
> 				sort = 1;
> 			}
> 			else if ( value1 != null & value2 == null ) {
> 				sort = -1;
> 			}
> 			else {
> 				sort = value1.compareTo( value2 );
> 			}
>
> 			if ( sortPolarity.equals( DESC ) ) {
> 				sort = sort * -1;
> 			}
>
> 			return sort;
> 		}
>
> 		catch ( Exception e ) {
> 			log.error( "Problem in Sort. sortPolarity:" + sortPolarity + ",
> sortProperty:" + sortProperty, e );
> 			return 0;
> 		}
> 	}
>
> }
>
> Eric Pugh
>
>
> -----Original Message-----
> From: Henri Yandell [mailto:bayard@generationjava.com]
> Sent: Friday, June 07, 2002 10:50 AM
> To: Jakarta Commons Developers List
> Subject: Re: [COLLECTIONS/BEANUTILS] Is there a comparator that can
> dynamically pick a method to call on a bean?
>
>
>
> 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:
> <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> >
> >
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [COLLECTIONS/BEANUTILS] Is there a comparator that can dynamically pick a method to call on a bean?

Posted by Henri Yandell <ba...@generationjava.com>.
-1 to the ASC/DESC bit. That shouldn't be in comparators in my opinion,
instead use the ReverseComparator.

What is Polarity vs Property?

On Fri, 7 Jun 2002, Eric Pugh wrote:

> I forged ahead..  I had a need to sort in asc/desc...  so I have added a
> sortPolarity and sortProperty methods..
>
> Here is what I created...
>
> package com.upstate.util;
>
> import org.apache.commons.beanutils.WrapDynaBean;
> import org.apache.commons.beanutils.*;
>
> import org.apache.log4j.Category;
>
> /**
>  *  Description of the Class
>  *
>  *@author     epugh
>  *@created    May 5, 2002
>  */
> public class BeanMethodComparator implements java.util.Comparator {
>
> 	public final static String ASC = "ASC";
> 	public final static String DESC = "DESC";
>
> 	private static Category log = Category.getInstance(
> BeanMethodComparator.class.getName() );
> 	private String sortProperty;
> 	private String sortPolarity = "ASC";
>
> 	/**  Constructor for the BeanMethodComparator object */
> 	public BeanMethodComparator() { }
>
> 	/**
> 	 *  Constructor for the BeanMetodComparator object
> 	 *
> 	 *@param  sortProperty  Description of Parameter
> 	 *@param  sortPolarity  Description of Parameter
> 	 */
> 	public BeanMethodComparator( String sortProperty, String sortPolarity ) {
> 		setSortProperty( sortProperty );
> 		setSortPolarity( sortPolarity );
> 	}
>
> 	/**
> 	 *  Sets the sortProperty attribute of the SortDaughterboard object
> 	 *
> 	 *@param  sortProperty  The new sortProperty value
> 	 */
> 	public void setSortProperty( String sortProperty ) {
> 		this.sortProperty = sortProperty;
> 	}
>
> 	/**
> 	 *  Sets the sortPolarity attribute of the SortDaughterboard object
> 	 *
> 	 *@param  sortPolarity                            The new sortPolarity
> value.
> 	 *      Can be either "ASC"/"DESC".
> 	 *@exception  java.lang.IllegalArgumentException  Thrown if you pass in a
> bad
> 	 *      sortPolarity.
> 	 */
> 	public void setSortPolarity( String sortPolarity )
> 		throws java.lang.IllegalArgumentException {
> 		sortPolarity = sortPolarity.toUpperCase();
> 		if ( sortPolarity.equals( ASC ) || sortPolarity.equals( DESC ) ) {
> 			throw new java.lang.IllegalArgumentException( "The argument:" +
> sortPolarity + " was invalid." );
> 		}
> 		this.sortPolarity = sortPolarity;
> 	}
>
> 	/**
> 	 *  Gets the sortPolarity attribute of the SortDaughterboard object
> 	 *
> 	 *@return    The sortPolarity value
> 	 */
> 	public String getSortPolarity() {
> 		return sortPolarity;
> 	}
>
>
> 	/**
> 	 *  Gets the sortProperty attribute of the SortDaughterboard object
> 	 *
> 	 *@return    The sortProperty value
> 	 */
> 	public String getSortProperty() {
> 		return sortProperty;
> 	}
>
> 	/**
> 	 *  Description of the Method
> 	 *
> 	 *@param  o1  Description of Parameter
> 	 *@param  o2  Description of Parameter
> 	 *@return     Description of the Returned Value
> 	 */
> 	public int compare( Object o1, Object o2 ) {
> 		try {
> 			WrapDynaBean bean1 = new WrapDynaBean( o1 );
> 			WrapDynaBean bean2 = new WrapDynaBean( o2 );
>
> 			Comparable value1 = (Comparable) bean1.get( sortProperty );
> 			Comparable value2 = (Comparable) bean2.get( sortProperty );
>
> 			int sort = 0;
>
> 			if ( ( value1 == null ) & ( value2 == null ) ) {
> 				sort = 0;
> 			}
> 			else if ( value1 == null & value2 != null ) {
> 				sort = 1;
> 			}
> 			else if ( value1 != null & value2 == null ) {
> 				sort = -1;
> 			}
> 			else {
> 				sort = value1.compareTo( value2 );
> 			}
>
> 			if ( sortPolarity.equals( DESC ) ) {
> 				sort = sort * -1;
> 			}
>
> 			return sort;
> 		}
>
> 		catch ( Exception e ) {
> 			log.error( "Problem in Sort. sortPolarity:" + sortPolarity + ",
> sortProperty:" + sortProperty, e );
> 			return 0;
> 		}
> 	}
>
> }
>
> Eric Pugh
>
>
> -----Original Message-----
> From: Henri Yandell [mailto:bayard@generationjava.com]
> Sent: Friday, June 07, 2002 10:50 AM
> To: Jakarta Commons Developers List
> Subject: Re: [COLLECTIONS/BEANUTILS] Is there a comparator that can
> dynamically pick a method to call on a bean?
>
>
>
> 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:
> <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> >
> >
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [COLLECTIONS/BEANUTILS] Is there a comparator that can dynamically pick a method to call on a bean?

Posted by Eric Pugh <ep...@upstate.com>.
I forged ahead..  I had a need to sort in asc/desc...  so I have added a
sortPolarity and sortProperty methods..

Here is what I created...

package com.upstate.util;

import org.apache.commons.beanutils.WrapDynaBean;
import org.apache.commons.beanutils.*;

import org.apache.log4j.Category;

/**
 *  Description of the Class
 *
 *@author     epugh
 *@created    May 5, 2002
 */
public class BeanMethodComparator implements java.util.Comparator {

	public final static String ASC = "ASC";
	public final static String DESC = "DESC";

	private static Category log = Category.getInstance(
BeanMethodComparator.class.getName() );
	private String sortProperty;
	private String sortPolarity = "ASC";

	/**  Constructor for the BeanMethodComparator object */
	public BeanMethodComparator() { }

	/**
	 *  Constructor for the BeanMetodComparator object
	 *
	 *@param  sortProperty  Description of Parameter
	 *@param  sortPolarity  Description of Parameter
	 */
	public BeanMethodComparator( String sortProperty, String sortPolarity ) {
		setSortProperty( sortProperty );
		setSortPolarity( sortPolarity );
	}

	/**
	 *  Sets the sortProperty attribute of the SortDaughterboard object
	 *
	 *@param  sortProperty  The new sortProperty value
	 */
	public void setSortProperty( String sortProperty ) {
		this.sortProperty = sortProperty;
	}

	/**
	 *  Sets the sortPolarity attribute of the SortDaughterboard object
	 *
	 *@param  sortPolarity                            The new sortPolarity
value.
	 *      Can be either "ASC"/"DESC".
	 *@exception  java.lang.IllegalArgumentException  Thrown if you pass in a
bad
	 *      sortPolarity.
	 */
	public void setSortPolarity( String sortPolarity )
		throws java.lang.IllegalArgumentException {
		sortPolarity = sortPolarity.toUpperCase();
		if ( sortPolarity.equals( ASC ) || sortPolarity.equals( DESC ) ) {
			throw new java.lang.IllegalArgumentException( "The argument:" +
sortPolarity + " was invalid." );
		}
		this.sortPolarity = sortPolarity;
	}

	/**
	 *  Gets the sortPolarity attribute of the SortDaughterboard object
	 *
	 *@return    The sortPolarity value
	 */
	public String getSortPolarity() {
		return sortPolarity;
	}


	/**
	 *  Gets the sortProperty attribute of the SortDaughterboard object
	 *
	 *@return    The sortProperty value
	 */
	public String getSortProperty() {
		return sortProperty;
	}

	/**
	 *  Description of the Method
	 *
	 *@param  o1  Description of Parameter
	 *@param  o2  Description of Parameter
	 *@return     Description of the Returned Value
	 */
	public int compare( Object o1, Object o2 ) {
		try {
			WrapDynaBean bean1 = new WrapDynaBean( o1 );
			WrapDynaBean bean2 = new WrapDynaBean( o2 );

			Comparable value1 = (Comparable) bean1.get( sortProperty );
			Comparable value2 = (Comparable) bean2.get( sortProperty );

			int sort = 0;

			if ( ( value1 == null ) & ( value2 == null ) ) {
				sort = 0;
			}
			else if ( value1 == null & value2 != null ) {
				sort = 1;
			}
			else if ( value1 != null & value2 == null ) {
				sort = -1;
			}
			else {
				sort = value1.compareTo( value2 );
			}

			if ( sortPolarity.equals( DESC ) ) {
				sort = sort * -1;
			}

			return sort;
		}

		catch ( Exception e ) {
			log.error( "Problem in Sort. sortPolarity:" + sortPolarity + ",
sortProperty:" + sortProperty, e );
			return 0;
		}
	}

}

Eric Pugh


-----Original Message-----
From: Henri Yandell [mailto:bayard@generationjava.com]
Sent: Friday, June 07, 2002 10:50 AM
To: Jakarta Commons Developers List
Subject: Re: [COLLECTIONS/BEANUTILS] Is there a comparator that can
dynamically pick a method to call on a bean?



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:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [COLLECTIONS/BEANUTILS] Is there a comparator that can dynamically pick a method to call on a bean?

Posted by Henri Yandell <ba...@generationjava.com>.
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:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


[COLLECTIONS/BEANUTILS] Is there a comparator that can dynamically pick a method to call on a bean?

Posted by Eric Pugh <ep...@upstate.com>.
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:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>