Re: Best practice in enum? Re: How to make a class an alias of another
one?
George wrote:
1. I understand enum constant should be in capital-letter in
convention. But my enum is mostly for the representation of the enum
field in the database, where it is in normal upper-lower-mixed
letters. If I use the normal letter, then default toString and
valueof works with it automatically and save me lots of time.
Alternatively, you can use an enum constructor that takes the lower-case form,
storing it in a final String member, and a toString() that returns the
lower-case form from that member. That way the constants can be upper-case in
Java, which helps maintenance, and lower-case to the database, which fulfills
your requirement.
You will need a helper method that returns the correct enum constant for a
given input String.
Your solution works, too, of course. It depends on the relative importance of
canonical naming in your app, and on situations like your #2, which would call
for that sort of engineering regardless.
2. In my database, couple of enum values like "CD-ROM", which is
illegal as variable name in JAVA. I can override their toString to
get it right for enum->String. But the valueof will not work when
String- enum. I read the alias workaround here
http://mindprod.com/jgloss/enum.html#ALIASES.
Another way I can think of is to pre-process the String before
valueof. Is there any other method to get around?
Same trick bails you out here. Store the "CD-ROM" String as the internal
name, and use toString() to get it. Use the helper method to retrieve the
correct enum constant for the String.
John B. Matthews wrote:
[Consider responding to original posts, rather than fakes.]
Let me make that stronger. Please, under no circumstances respond to the fake
posts. Certainly do not quote them. Best to block them with your newsreader
filters.
--
Lew