John B. Matthews wrote:
In article
<24ebe868-e3b3-49f9-a23c-0c47a107d6fe@a32g2000yqm.googlegroups.com>,
"david.karr" <davidmichaelkarr@gmail.com> wrote:
On Dec 18, 4:25 pm, EJP <esmond.not.p...@not.bigpond.com> wrote:
david.karr wrote:
Can someone think of a better way to do this, that doesn't repeat
the column values?
Have each enum value enter itself on construction into a
Map<Integer, YourEnum> with its own ColumnValue as the key.
Like Peter I cannot see the point of setColumnValue(), and the Map
would of course require the columnValue to be constant.
Yes, you're right, there's no need for "setColumnValue()".
Concerning the Map, I had already thought of that. That's the
obvious way to do it. Now, how would you do it? I would assume
you'd define a static Map in the enum type and have the constructor
put itself into the map. The problem is, it doesn't appear to be
possible to do that. It doesn't compile.
A static initializer works, as suggested in the "enum Color" discussion:
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9>
Here's a similar example that constructs a Map<Integer, Key>, where
Integer is a keyCode and Key is an enum:
<http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/
src/org/gcs/robot/Key.java?revision=27&view=markup>
The mapping is persisted using java.util.prefs.Preferences rather than a
database, but the result is similar.
What might work is implementing some sort of "super-map" that holds
all the mappings for all enum types you implement, so the key would
have to concatenate the class and the custom ordinal.
representation of the enum value.
You can do something similar for integer or any 'Representation' type.