aspose file tools*
The moose likes Java in General and the fly likes eliminating duplicate Enum code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "eliminating duplicate Enum code" Watch "eliminating duplicate Enum code" New topic
Author

eliminating duplicate Enum code

Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
Hi,

I have a large number of Enums that implement this interface:



A typical example is:



As you can imagine these methods are virtually identical in all implementations of CodableEnum. I would like to eliminate this duplication, but frankly don't know how. I tried using a class such as the following



But this turns out to be fairly useless because:

1. An enum cannot extend a class
2. Elements of an enum (SKYPE, GOOGLE_TALK, etc.) cannot extend a class
3. I cannot provide a default implementation of getByCode(), because DefaultCodableEnum is not itself an Enum. I tried changing DefaultCodableEnum to extend java.lang.Enum, but this doesn't appear to be allowed.

Any suggestions?
Thanks,
Dan Murphy

[ June 11, 2008: Message edited by: Dan Murphy ]
[ June 11, 2008: Message edited by: Dan Murphy ]

SCJP, SCJD, SCWCD
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Any reason you don't use the predefined toString and valueOf methods?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
Yes, the correct functioning of my program depends on these codes, whereas the contract of toString() indicates that the value returned by this method is "for a person to read". Therefore it should be possible be to change the value returned by toString() without breaking anything, which is why I prefer to use the value returned by toString() for logging/debugging purposes only.

More specifically, if I were to use the built-in toString() and valueOf() methods, then my program would no longer work after renaming an element from say YAHOO_MESSENGER to YAHOO_MGR.

[ June 12, 2008: Message edited by: Dan Murphy ]
[ June 12, 2008: Message edited by: Dan Murphy ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In that case, I don't think you can remove the duplication by using inheritance. The only two options I see are reducing the duplication by having the enum methods delegate to some helper class, and/or using code generation to generate the complete enum source code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: eliminating duplicate Enum code
 
Similar Threads
Cloning Question
In case you missed it...
Tranfering entities to a desktop client
Simple J2SE 5.0 Tiger Notes
enum