You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Jack, Paul" <pj...@sfaf.org> on 2002/06/10 22:45:03 UTC

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

FYI, java.lang.String.CASE_INSENSITIVE_ORDER does what your 
LowercaseComparator does, but somewhat more efficiently.

-Paul


> -----Original Message-----
> From: Eric Pugh [mailto:epugh@upstate.com]
> Sent: Monday, June 10, 2002 1:35 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?
> 
> 
> 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>
> 

--
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 dynami cally pick a method to call on a bean?

Posted by Henri Yandell <ba...@generationjava.com>.
Yeah, I'm not in favour of adding setComparator. I wasn't thinking
clearly, think we've gone over that point before.

On Mon, 10 Jun 2002, Michael A. Smith wrote:

> On Mon, 10 Jun 2002, Eric Pugh wrote:
> > This way you can just daisy chain the decorators in one line..  Also, I was
> > modeling it on ReverseComparator, which doesn't have a setComparator()...
> > However, if you would like, I don't mind adding it!  Might be good to add it
> > to ReverseComparator as well..
>


--
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 dynami cally pick a method to call on a bean?

Posted by "Michael A. Smith" <ma...@apache.org>.
On Mon, 10 Jun 2002, Eric Pugh wrote:
> This way you can just daisy chain the decorators in one line..  Also, I was
> modeling it on ReverseComparator, which doesn't have a setComparator()...
> However, if you would like, I don't mind adding it!  Might be good to add it
> to ReverseComparator as well..

setComparator can introduce instability for collections that use the 
comparator to order its contents.  If the underlying comparator changes 
via setComparator, the order within the collection possibly becomes 
invalid because the "sorted" items within the collection may no longer 
be "sorted" based on the new underlying comparator.

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 dynami cally pick a method to call on a bean?

Posted by Eric Pugh <ep...@upstate.com>.
I do have that, however, since it needs to know what property to call, it
looks like this:

c = new ReverseComparator( new BeanComparator( "customerName",
String.CASE_INSENSITIVE_ORDER ) )

This way you can just daisy chain the decorators in one line..  Also, I was
modeling it on ReverseComparator, which doesn't have a setComparator()...
However, if you would like, I don't mind adding it!  Might be good to add it
to ReverseComparator as well..

Any other suggestions are welcome!

Eric

-----Original Message-----
From: Henri Yandell [mailto:bayard@generationjava.com]
Sent: Monday, June 10, 2002 4:56 PM
To: Jakarta Commons Developers List
Subject: RE: [COLLECTIONS/BEANUTILS] Is there a comparator that can
dynami cally pick a method to call on a bean?



> > 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?

BeanComparator should have a Comparator property.

ie) I'd do:

Comparator comp = new ComparableComparator();
comp = new BeanComparator(comp);

or setComparator(..);

It's the reason why ComparableComparator exists :) So I can easily pass it
into my BeanComparator.

Hen


--
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 dynami cally pick a method to call on a bean?

Posted by Henri Yandell <ba...@generationjava.com>.
> > 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?

BeanComparator should have a Comparator property.

ie) I'd do:

Comparator comp = new ComparableComparator();
comp = new BeanComparator(comp);

or setComparator(..);

It's the reason why ComparableComparator exists :) So I can easily pass it
into my BeanComparator.

Hen


--
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>.
Argh!  Read the darn Manual!  I have slapped it a mention of it to my
LowercaseComparator..  I guess the only thing is that you can't daisy chain
farther with the CASE_INSENSITIVE_ORDER version..  Although I am not sure
you would want to either..

Eric
-----Original Message-----
From: Jack, Paul [mailto:pjack@sfaf.org]
Sent: Monday, June 10, 2002 4:45 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?


FYI, java.lang.String.CASE_INSENSITIVE_ORDER does what your
LowercaseComparator does, but somewhat more efficiently.

-Paul


> -----Original Message-----
> From: Eric Pugh [mailto:epugh@upstate.com]
> Sent: Monday, June 10, 2002 1:35 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?
>
>
> 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>
>

--
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>.
Now I figured out why I have the Lowercase comparator..  I am using the
lowercase comparator decorated by BeanCompartor, which sometiems passes
strings to lowercase comparator and other times passes other objects...
CASE_INSENSITIVE_ORDER throws classcastexceptions on anything that is not a
string..

So, how about changing LowercaseComparator to CaseInsensitiveComparator
which wraps a call to CASE_INSENSITIVE_ORDER when both args are Strings, and
otherwise doesn't?  The problem is still that you can't daisy chain any
furthur..  Maybe only use CaseInsensitiveComparator when the user doesn't
pass in another comparator, other do the toLowerCase call...

ERic

-----Original Message-----
From: Jack, Paul [mailto:pjack@sfaf.org]
Sent: Monday, June 10, 2002 4:45 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?


FYI, java.lang.String.CASE_INSENSITIVE_ORDER does what your
LowercaseComparator does, but somewhat more efficiently.

-Paul


> -----Original Message-----
> From: Eric Pugh [mailto:epugh@upstate.com]
> Sent: Monday, June 10, 2002 1:35 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?
>
>
> 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>
>

--
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>