tags:

views:

2493

answers:

2

Hi,

I've an enum type: ReportTypeEnum that get passed between methods in all my classes but I then need to pass this on the URL so I use the ordinal method to get the int value. After I get it in my other JSP page I need to convert it to back to an ReportTypeEnum so that I can continue passing it.

How can I convert ordinal to the ReportTypeEnum?

Using Java 6 EE.

+12  A: 
ReportTypeEnum value = ReportTypeEnum.values()[ordinal]
Joachim Sauer
+12  A: 

This is almost certainly a bad idea. Certainly if the ordinal is de-facto persisted (e.g. because someone has bookmarked the URL) - it means that you must always preserve the enum ordering in future, which may not be obvious to code maintainers down the line.

Why not encode the enum using myEnumValue.name() (and decode via ReportTypeEnum.valueOf(s)) instead?

oxbow_lakes
much better idea
Boris Pavlović
I agree, it's the better solution.
Joachim Sauer
What if you change the name of the enum (but keep the ordering)?
Arne Evertsson
@Arne - I think this is much less likely than some inexperienced person coming along and adding a `value` at either the start or its correct alphabetical/logical position. (By logical I mean for example `TimeUnit` values have a logical position)
oxbow_lakes