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 ]
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
Joined: Mar 29, 2005
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 ]
Joined: Jul 11, 2001
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.