You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Emmanuel Bourg <eb...@micropole-univers.com> on 2004/06/01 12:11:02 UTC

Re: [collections][lang] generic min/max functions

Rodney Waldhoff wrote:
> On Tue, 25 May 2004, matthew.hawthorne wrote:
> 
> Or, for what it's worth:
> 
> Date date = date1.compareTo(date2) > 0 date2 : date1;

Only if the dates are not null of course, otherwise it becomes something 
like :

Date date = null;

if (date1 != null && date 2 != null) {
     date = date1.compareTo(date2) > 0 ? date2 : date1;
} else {
     date = (date1 == null) ? date2 : date1;
}

That's this boiler plate I'd like to reduce to a simple expression.


Emmanuel Bourg

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


Re: [collections][lang] generic min/max functions

Posted by "C. Scott Ananian" <cs...@cscott.net>.
On Tue, 1 Jun 2004, Emmanuel Bourg wrote:

> Only if the dates are not null of course, otherwise it becomes something

How about Collections.min(Arrays.asList(new Date[] { date1, date2 })) ?

I'm not sure how Date.compareTo() reacts to null (it's not specified in
the javadoc), but if it reacts poorly, all you need is a
  public class NullNaturalComparator implements Comparator {
     public int compare(Object o1, Object o2) {
        if (o1==null) return (o2==null) ? 0 : -1;
        if (o2==null) return +1;
	return ((Comparable)o1).compareTo(o2);
     }
  }

 --scott

Marxist Shoal Bay Rijndael Pakistan NSA anthrax ammunition Sugar Grove
RUCKUS Hawk Suharto shortwave SSBN 743 Nader assassination politics
                         ( http://cscott.net/ )

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