RE: [Collections] ComparableComparator - nulls OK
- Original message From: Jack, Paul [EMAIL PROTECTED] I too like the ComparatorUtils concept outlined below. However, we might want to consider whether the method names are nullFirst(Comparator) or nullFirstComparator(Comparator) etc. I raise this because PredicateUtils uses the latter at the moment, and it would be nice to be consistent (we could change PredicateUtils if required). Hm. I always favor shorter names, especially when the longer names are redundant...the ComparatorUtils I submitted uses the shorter names... What do you think of Set predicate(Set set, Predicate predicate) ? Is that too nondescript? Actually, there is another factor involved. The java.utils.Collections class uses the long form, eg. Collections.unmodifiableMap(Map); Thus I would prefer the long form. We could agree to differ from Sun however, in which case I would submit a patch to the Predicate code. Stephen -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
RE: [Collections] ComparableComparator - nulls OK
I too like the ComparatorUtils concept outlined below. However, we might want to consider whether the method names are nullFirst(Comparator) or nullFirstComparator(Comparator) etc. I raise this because PredicateUtils uses the latter at the moment, and it would be nice to be consistent (we could change PredicateUtils if required). Hm. I always favor shorter names, especially when the longer names are redundant...the ComparatorUtils I submitted uses the shorter names... What do you think of Set predicate(Set set, Predicate predicate) ? Is that too nondescript? -Paul -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
Re: [Collections] ComparableComparator - nulls OK
I too like the ComparatorUtils concept outlined below. However, we might want to consider whether the method names are nullFirst(Comparator) or nullFirstComparator(Comparator) etc. I raise this because PredicateUtils uses the latter at the moment, and it would be nice to be consistent (we could change PredicateUtils if required). Stephen - Original Message - From: Jack, Paul [EMAIL PROTECTED] To: 'Jakarta Commons Developers List' [EMAIL PROTECTED] Sent: Friday, June 07, 2002 9:09 PM Subject: RE: [Collections] ComparableComparator - nulls OK I like the idea of having the functionality provided by NullFirstComparator and NullLastComparator, but I have an additional suggestion. Currently, all of the classes in the comparators subpackage are simple, and very useful. However, it seems that they'd most often be used in conjunction with each other; typically, I'd use ReverseComparator with ComparableComparator to get reverse natural ordering, and I'd probably use a NullSomething comparator with ComparableComparator to get natural ordering that accepts nulls. So my suggestion is, can we fold these all into one static utility API? It would make them much more convenient, IMHO. I'm thinking of something like: public class ComparatorUtils { // same as ComparableComparator.getInstance public static Comparator NATURAL; public static Comparator nullFirst(Comparator c); public static Comparator nullLast(Comparator c); // same as reverseComparator public static Comparator reverse(Comparator c); public static Comparator bean(Comparator c, String getterName); public static Comparator transform(Comparator c, Transformer t); } Also, there are operations involving Comparators that I use frequently that would be nice to have in the API: public static Object min(Object o1, Object o2, Comparator c); public static Object max(Object o1, Object o2, Comparator c); which would return the higher or lower of the given objects according to the comparator. Any of this make sense? -Paul -Original Message- From: Michael A. Smith [mailto:[EMAIL PROTECTED]] Sent: Friday, June 07, 2002 1:00 PM To: Jakarta Commons Developers List Subject: RE: [Collections] ComparableComparator - nulls OK On Fri, 7 Jun 2002, Eric Pugh wrote: +1, In my sorts, having to deal with nulls is causing me difficulties as well.. Although I could see something like any nulls being ignored as a type of behavior.. Sort everything, and drop the nulls! consider: Comparator.compare(null, x); how do you drop or ignore the null when doing this compare? -Original Message- From: Jonathan Carlson [mailto:[EMAIL PROTECTED]] Sent: Friday, June 07, 2002 3:38 PM To: [EMAIL PROTECTED] Subject: [Collections] ComparableComparator - nulls OK I'd like to make the case for a ComparableComparator that allows the sorting of nulls to the bottom. This could be a flag to set on the existing class or another Comparator called something like NullableComparableComparator (or ComparableNullComparator?). How about something like this: public class NullFirstComparator implements Comparator { private Comparator c; public NullFirstComparator(Comparator nonNullComparator) { this.c = nonNullComparator; } public int compare(Object a, Object b) { if(a == b) return 0; if(a == null) return -1; if(b == null) return 1; return c.compare(a,b); } } and public class NullLastComparator implements Comparator { private Comparator c; public NullLastComparator(Comparator nonNullComparator) { this.c = nonNullComparator; } public int compare(Object a, Object b) { if(a == b) return 0; if(a == null) return 1; if(b == null) return -1; return c.compare(a,b); } } That allows you to adjust the behavior of comparison to null for any comparator and not just the ComparableComparator. It sounds like in your case (sorting nulls last using ComparableComparator), you'd use: new NullLastComparator(ComparableComparator.getInstance()) If that sounds reasonable, I'll add a full implementation (with a better Comparator.equals method) to the list of things on my todo list. regards, michael p.s. I just threw together the above implementations. I wouldn't trust it to actually sort things properly (or even compile) -- I may have things reversed or something where nulls go first instead of last and vice-versa. -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
RE: [Collections] ComparableComparator - nulls OK
+1, In my sorts, having to deal with nulls is causing me difficulties as well.. Although I could see something like any nulls being ignored as a type of behavior.. Sort everything, and drop the nulls! Eric -Original Message- From: Jonathan Carlson [mailto:[EMAIL PROTECTED]] Sent: Friday, June 07, 2002 3:38 PM To: [EMAIL PROTECTED] Subject: [Collections] ComparableComparator - nulls OK I'd like to make the case for a ComparableComparator that allows the sorting of nulls to the bottom. This could be a flag to set on the existing class or another Comparator called something like NullableComparableComparator (or ComparableNullComparator?). This becomes a big issue when sorting a collection of things based on one of their getter methods with generic components like the TransformingComparator I just submitted. Too often the returned value can be a null. It's not too hard to write my own (I already have), but I prefer having things like this standardized in one library rather than written differently for each project. My two cents. I'd appreciate feedback, even negative feedback if it's appropriate. Jonathan = Jonathan Carlson [EMAIL PROTECTED] Minneapolis, Minnesota __ Do You Yahoo!? Yahoo! - Official partner of 2002 FIFA World Cup http://fifaworldcup.yahoo.com -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
RE: [Collections] ComparableComparator - nulls OK
I can't think of a how nulls could be dropped. The Comparator API only allows for returning a -1, 0, or +1. But thanks for the support for the option of sorting nulls to the bottom. Jonathan = Jonathan Carlson [EMAIL PROTECTED] Minneapolis, Minnesota __ Do You Yahoo!? Yahoo! - Official partner of 2002 FIFA World Cup http://fifaworldcup.yahoo.com -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
RE: [Collections] ComparableComparator - nulls OK
I don't think that there's any obvious ordering for null and x (or some other arbitrary object)...I would be inclined to throw an exception if null is passed in. If you're sorting a list with nulls, it's probably best to filter them out before sorting. I definitely give a (non-binding) -1 to any kind of special case null handling. -- Tim Moore / Blackboard Inc. / Software Engineer 1899 L Street, NW / 5th Floor / Washington, DC 20036 Phone 202-463-4860 ext. 258 / Fax 202-463-4863 -Original Message- From: Michael A. Smith [mailto:[EMAIL PROTECTED]] Sent: Friday, June 07, 2002 4:00 PM To: Jakarta Commons Developers List Subject: RE: [Collections] ComparableComparator - nulls OK On Fri, 7 Jun 2002, Eric Pugh wrote: +1, In my sorts, having to deal with nulls is causing me difficulties +as well.. Although I could see something like any nulls being ignored as a type of behavior.. Sort everything, and drop the nulls! consider: Comparator.compare(null, x); how do you drop or ignore the null when doing this compare? -Original Message- From: Jonathan Carlson [mailto:[EMAIL PROTECTED]] Sent: Friday, June 07, 2002 3:38 PM To: [EMAIL PROTECTED] Subject: [Collections] ComparableComparator - nulls OK I'd like to make the case for a ComparableComparator that allows the sorting of nulls to the bottom. This could be a flag to set on the existing class or another Comparator called something like NullableComparableComparator (or ComparableNullComparator?). How about something like this: public class NullFirstComparator implements Comparator { private Comparator c; public NullFirstComparator(Comparator nonNullComparator) { this.c = nonNullComparator; } public int compare(Object a, Object b) { if(a == b) return 0; if(a == null) return -1; if(b == null) return 1; return c.compare(a,b); } } and public class NullLastComparator implements Comparator { private Comparator c; public NullLastComparator(Comparator nonNullComparator) { this.c = nonNullComparator; } public int compare(Object a, Object b) { if(a == b) return 0; if(a == null) return 1; if(b == null) return -1; return c.compare(a,b); } } That allows you to adjust the behavior of comparison to null for any comparator and not just the ComparableComparator. It sounds like in your case (sorting nulls last using ComparableComparator), you'd use: new NullLastComparator(ComparableComparator.getInstance()) If that sounds reasonable, I'll add a full implementation (with a better Comparator.equals method) to the list of things on my todo list. regards, michael p.s. I just threw together the above implementations. I wouldn't trust it to actually sort things properly (or even compile) -- I may have things reversed or something where nulls go first instead of last and vice-versa. -- To unsubscribe, e-mail: mailto:commons-dev- [EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
RE: [Collections] ComparableComparator - nulls OK
I'm +1 on sorting to the bottom. Would it harm for this to be a default? ie) no ClassCastException anymore? On Fri, 7 Jun 2002, Jonathan Carlson wrote: I can't think of a how nulls could be dropped. The Comparator API only allows for returning a -1, 0, or +1. But thanks for the support for the option of sorting nulls to the bottom. Jonathan = Jonathan Carlson [EMAIL PROTECTED] Minneapolis, Minnesota __ Do You Yahoo!? Yahoo! - Official partner of 2002 FIFA World Cup http://fifaworldcup.yahoo.com -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]
RE: [Collections] ComparableComparator - nulls OK
I like the idea of having the functionality provided by NullFirstComparator and NullLastComparator, but I have an additional suggestion. Currently, all of the classes in the comparators subpackage are simple, and very useful. However, it seems that they'd most often be used in conjunction with each other; typically, I'd use ReverseComparator with ComparableComparator to get reverse natural ordering, and I'd probably use a NullSomething comparator with ComparableComparator to get natural ordering that accepts nulls. So my suggestion is, can we fold these all into one static utility API? It would make them much more convenient, IMHO. I'm thinking of something like: public class ComparatorUtils { // same as ComparableComparator.getInstance public static Comparator NATURAL; public static Comparator nullFirst(Comparator c); public static Comparator nullLast(Comparator c); // same as reverseComparator public static Comparator reverse(Comparator c); public static Comparator bean(Comparator c, String getterName); public static Comparator transform(Comparator c, Transformer t); } Also, there are operations involving Comparators that I use frequently that would be nice to have in the API: public static Object min(Object o1, Object o2, Comparator c); public static Object max(Object o1, Object o2, Comparator c); which would return the higher or lower of the given objects according to the comparator. Any of this make sense? -Paul -Original Message- From: Michael A. Smith [mailto:[EMAIL PROTECTED]] Sent: Friday, June 07, 2002 1:00 PM To: Jakarta Commons Developers List Subject: RE: [Collections] ComparableComparator - nulls OK On Fri, 7 Jun 2002, Eric Pugh wrote: +1, In my sorts, having to deal with nulls is causing me difficulties as well.. Although I could see something like any nulls being ignored as a type of behavior.. Sort everything, and drop the nulls! consider: Comparator.compare(null, x); how do you drop or ignore the null when doing this compare? -Original Message- From: Jonathan Carlson [mailto:[EMAIL PROTECTED]] Sent: Friday, June 07, 2002 3:38 PM To: [EMAIL PROTECTED] Subject: [Collections] ComparableComparator - nulls OK I'd like to make the case for a ComparableComparator that allows the sorting of nulls to the bottom. This could be a flag to set on the existing class or another Comparator called something like NullableComparableComparator (or ComparableNullComparator?). How about something like this: public class NullFirstComparator implements Comparator { private Comparator c; public NullFirstComparator(Comparator nonNullComparator) { this.c = nonNullComparator; } public int compare(Object a, Object b) { if(a == b) return 0; if(a == null) return -1; if(b == null) return 1; return c.compare(a,b); } } and public class NullLastComparator implements Comparator { private Comparator c; public NullLastComparator(Comparator nonNullComparator) { this.c = nonNullComparator; } public int compare(Object a, Object b) { if(a == b) return 0; if(a == null) return 1; if(b == null) return -1; return c.compare(a,b); } } That allows you to adjust the behavior of comparison to null for any comparator and not just the ComparableComparator. It sounds like in your case (sorting nulls last using ComparableComparator), you'd use: new NullLastComparator(ComparableComparator.getInstance()) If that sounds reasonable, I'll add a full implementation (with a better Comparator.equals method) to the list of things on my todo list. regards, michael p.s. I just threw together the above implementations. I wouldn't trust it to actually sort things properly (or even compile) -- I may have things reversed or something where nulls go first instead of last and vice-versa. -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED] -- To unsubscribe, e-mail: mailto:[EMAIL PROTECTED] For additional commands, e-mail: mailto:[EMAIL PROTECTED]