I have a question about Java 5 enums. I'm trying to have UserState.ACTIVE in my code but have "A" saved to the database. Then when I get "A" from the database I get the enum representing UserState.ACTIVE. We're limited by the User POJO which can't change from using a String.
Here's my code so far...
I use the private method getEnum("A") instead of UserState.valueOf("A") as I get an error saying the value "A" doesn't exist (which I know it doesn't). The thing is, I'll have to modify the private getEnum method everytime a new state is added to the enum.
Is there a better way to code this enum so I can use UserState.valueOf("A") ?
Thanks for the nudge in the right direction Rob. Sorry Oscar, I don't like UserState.L (what does that stand for!?) Some last minute reading and a few ideas later, I came up with the following as Rob suggested...I like my getEnum method so much now I made it public I'm also glad it's now scalable for the future while relaxing the rules for instantiating an instance. I can use either the value or the name (case insensitive) keeping UserState.ACTIVE in the Java code and "A", "I" etc. in the database as per the specification which I'm not allowed to change (the best of both worlds).It's a pity you can't override valueOf but heh, I'll document it. It'll be a lot better using this with Hibernate/JPA rather than going down this road!