Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: Comparator ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 11 Topic 16040 of 16078
Post > Topic >>

Re: Comparator in the driver

by Lew <lew@[EMAIL PROTECTED] > Apr 30, 2008 at 09:15 PM

thufir wrote:
>     79          static final Comparator<Guest> BY_NAME = new 
> Comparator<Guest>(){
>     80                  public int compare(Guest g1, Guest g2){
>     81                          ContactInfo c1 = g1.getContactInfo();

What if g1 or g2 are null?

>     82                          ContactInfo c2 = g2.getContactInfo();
>     83                         
System.out.println("******************");

Don't put output in a Comparator.

>     84                          String s1 = c1.getLastName();
>     85                          String s2 = c2.getLastName();

What if c1 or c2 are null?

If the design of ContactInfo guarantees non-null elements, use 'assert' to

assure the invariants.

>     86                          int foo = s1.compareTo(s2);
>     87                          System.out.println(foo);

Really, don't put output in Comparators.  Also, "System.out.println()" may
be 
ugly and inflexible, but at least it causes lots of unnecessary overhead
in 
production without providing any benefit as used here.

>     88                          return foo;
>     89                  }
>     90          };

> I'd like to put this Comparator into a00720398.util.CollectionUtil and 

That's an interesting package name.

> still keep the collections in the driver to meet other requirements.
> 
> Any pointers as to how to do that?

Just declare a top-level class in that package.

package a00720398.util;

public class GuestComparer implements Comparator <Guest>
{
  @[EMAIL PROTECTED]
  public int compare( Guest g1, Guest g2 )
  {
   if ( g1 == null )
   {
     return (g2 == null? 0 : -1);
   }
   if ( g2 == null )
   {
     return 1;
   }
   ContactInfo
     c1 = g1.getContactInfo(),
     c2 = g2.getContactInfo();
   return (c1 == null? (c2 == null? 0 : -1)
            : c1.compareTo( c2 ));
  }
}

If you want to make it a nested class (why in the world would one do
that?), 
that's easy enough to do.

-- 
Lew




 11 Posts in Topic:
Comparator in the driver
thufir <hawat.thufir@[  2008-04-30 08:11:35 
Re: Comparator in the driver
"Matt Humphrey"  2008-04-30 07:51:59 
Re: Comparator in the driver
thufir <hawat.thufir@[  2008-04-30 12:35:32 
Re: Comparator in the driver
thufir <hawat.thufir@[  2008-04-30 12:51:54 
Re: Comparator in the driver
Lew <lew@[EMAIL PROTEC  2008-04-30 21:15:06 
Re: Comparator in the driver
Roedy Green <see_websi  2008-04-30 13:43:44 
Re: Comparator in the driver
Thufir <hawat.thufir@[  2008-04-30 16:29:40 
Re: Comparator in the driver
Roedy Green <see_websi  2008-05-02 10:56:20 
Re: Comparator in the driver
thufir <hawat.thufir@[  2008-05-01 17:01:32 
Re: Comparator in the driver
Lew <lew@[EMAIL PROTEC  2008-05-01 18:23:34 
Re: Comparator in the driver
thufir <hawat.thufir@[  2008-05-02 04:57:13 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 11:44:36 CDT 2008.