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 / January 2006

Tip: Looking for answers? Try searching our database.

another enum gotcha

Thread view: 
Roedy Green - 14 Jan 2006 20:07 GMT
/**
* example use of Enum, with method implemented a different way for
each enum
* constant.
*/
package com.mindprod.example;

/**
* @author Roedy Green
*/
public enum BreedI {

   // Usually you list enum constants in alphabetical order.
   // Eclipse will put them in alphabetical order when you do
   // a Source Sort Members.

   /** Dachshund smooth or curly */
   DACHSHUND( "brown" ),

   /** Dalmatian */
   DALMATIAN( "spotted" ) {

   // begin DALMATIAN inner class
   /**
    * typical spots on a Dalmatian
    */
   private final int spots = 50;

   /**
    * Method getSpotcount is Defined only for DALMATIAN. Get typical
count of
    * spots on a Dalmatian
    *
    * @return spot count
    */
    int getSpotCount ()
       {
       return spots;
       }

   // end DALMATIAN inner class
   },

   /** Labrador all sizes */
   LABRADOR( "black or tan" );

   // end of enum constant constructions

   /** additional instance field of every BreedI enum constant object
*/
   private String colours;

   /**
    * additional method of every BreedI enum constant object
    *
    * @return typical colours of the breed
    */
   public String getColours ()
       {
       return colours;
       }

   /**
    * constructor
    *
    * @param colours
    *        typical colours of this breed
    */
   BreedI( String colours )
       {
       this.colours = colours;
       }
   /**
    * Test harness
    * @param args not used
    */
   public static void main ( String [] args )
       {
       Breed myDog = Breed.DALMATIAN;
       // since the DALMATION inner class of Breed is anonymous,
       // we have no way of getting at the getSpotCount method.
       // This won't work:
       // System.out.println( myDog.getSpotCount() );
       }
       
}

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Tony Morris - 14 Jan 2006 12:42 GMT
>     // Usually you list enum constants in alphabetical order.
>     // Eclipse will put them in alphabetical order when you do
>     // a Source Sort Members.

A potential gotcha here is that changing the order of appearance of enum
fields may break existing clients.
http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
"Note that each enum type has a static values method that returns an array
containing all of the values of the enum type in the order they are
declared."

Reflection APIs make no such guarantee of order, and so this does not apply
to other type fields.

--
Tony Morris
http://tmorris.net/

Java Questions and Answers
http://jqa.tmorris.net/
Thomas Hawtin - 14 Jan 2006 20:45 GMT
>  * example use of Enum, with method implemented a different way for
> each enum
>  * constant.

>         Breed myDog = Breed.DALMATIAN;
>         // since the DALMATION inner class of Breed is anonymous,
>         // we have no way of getting at the getSpotCount method.
>         // This won't work:
>         // System.out.println( myDog.getSpotCount() );

It's almost a pity particular enum constant classes can't implement
different interfaces. Certainly the whole structure could be made a bit
more lax.

Two solutions stand out to me. The obvious one is to put the method in
the base enum, and throw an exception or return a special value (like
null) if called on inappropriate objects. I prefer static type safety.

The other way around is to make Breed (or BreedI) an interface. Have two
separate enums (or even just classes that follow the old enum idiom) for
the separate cases. You do lose some of the enum "sugar".

So:

interface Breed {
    PlainBreed   DACHSHUND = PlainBreed  .DACHSHUND;
    PlainBreed   LABRADOR  = PlainBreed  .LABRADOR ;
    SpottedBreed DALMATIAN = SpottedBreed.DALMATIAN;
(Bit nasty this, having a cyclic dependency between super and subtypes.
Bet there is a way to get a class-loading deadlock like this.)

    String getColours();
}

enum PlainBreed implements Breed {
    DACHSHUND("brown"        ),
    LABRADOR ("black or tan" ),
    ;
    private final String colours;
    ...
}

enum SpottedBreed implements Breed {
    DALMATIAN("spotted",     50),
    ;
    private final String colours;
    ... copy & paste ...
}

Possibly not an extremely uncommon thing to do. I came across an example
a couple months ago with WeekDay and WeekendDay.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



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



©2009 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.