Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Comparator as anonymous inner class?

Thread view: 
kk_oop@yahoo.com - 28 Jun 2005 15:47 GMT
Hi.  Any opinions for or against defining Comparators as anonymous
inner classes?  I'm wondering if this may cause unit test difficulty or
any other problems?  Would named inner classes be more appropriate
(assuming the comparator would never be applied to other classes)?

Thanks for any thoughts!

Ken
Patricia Shanahan - 28 Jun 2005 16:25 GMT
> Hi.  Any opinions for or against defining Comparators as anonymous
> inner classes?  I'm wondering if this may cause unit test difficulty or
[quoted text clipped - 4 lines]
>
> Ken

It shouldn't cause any unit test difficulty, as long as the anonymous
class capabilities are limited to those specified by the Comparator
interface. If the anonymous class has any extra functions, the unit test
would need to use the actual type to access them.

Patricia
Stefan Schulz - 28 Jun 2005 16:53 GMT
On Tue, 28 Jun 2005 07:47:41 -0700, kk_oop wrote:

> Hi.  Any opinions for or against defining Comparators as anonymous
> inner classes?  I'm wondering if this may cause unit test difficulty or
> any other problems?  Would named inner classes be more appropriate
> (assuming the comparator would never be applied to other classes)?

I'd always use named classes, just for the sake of readability, but in
principle, there is nothing wrong with an anonymous Comparator.

Signature

You can't run away forever,
But there's nothing wrong with getting a good head start.
          --- Jim Steinman, "Rock and Roll Dreams Come Through"
         

Chris Smith - 28 Jun 2005 16:58 GMT
> Hi.  Any opinions for or against defining Comparators as anonymous
> inner classes?  I'm wondering if this may cause unit test difficulty or
> any other problems?

I'd be more precise, and say that the question is whether to use a named
*concept* or not.  The use of an anonymous class is just an
implementation choice.  For example, I sometimes do something like this:

class Foo
{
   public static final Comparator<Foo> SOME_COMPARATOR
       = new Comparator<Foo>() {
           @Override
           public int compare(Foo a, Foo b)
           {
               ...
           }
       };
}

Even though there is an anonymous nested class (though not strictly an
inner class) being used there, the concept that it represents definitely
does have a name.  This is easy to unit test even if the concept is non-
trivial.  On the other hand, a truly on-the-fly Comparator such as:

   List<Foo> foos = ...;
   Collections.sort(foos, new Comparator<Foo>() { ... });

would need to meet a stricter standard of triviality of content, because
you don't get the chance to use an identifier to sum up the abstraction,
and you don't get the ability to reference the concept in and of itself.  
Only the code itself expresses your intent, so the code had better be
pretty obvious, to the point that a unit test would be superfluous.

> Would named inner classes be more appropriate
> (assuming the comparator would never be applied to other classes)?

Because most simple Comparator instances do not have state, I would not
favor exposing a named nested class for a Comparator implementation.  
Instead, I'd do as in the first example above; expose a named instance
of the class.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.