The solution is to not use one side of the mapping
The program processes an xml file, which contains nodes with a 'type' attribute. One part of the program (the model) passes these types to another part (the view).
To keep things typesafe, I created an enum for the known types. This is were the mapping comes in. Each type from the xml (a
String) maps to an enum.
The program reads the types, stores them in a List, and at a later point creates new nodes with those types.
My first approach was to store the enum that belongs to the type xml-String, and store that enum in the List. But then when a new node has to be created, the enum has to be translated back to the xml-String. That's where I thought a bi-directional mapping was needed.
One solution is not to store the enums, but the xml-Strings as they're read from the xml. When they have to be passed to the view-part, they can be translated to the enums at that moment. It has another advantage - it seperates the type that is used for reading/writing the xml, from the type that is used by the view, which is a good thing. So actually, the String-enum mapping is not one-to-one: several Strings can potentially map to the same enum.
Now that I think of it, I may be able to get rid of the xmlString-to-enum hashmap. Maybe I can store the String in the enum class.. That would be better OO - the String and enum relationship is then encapsulated in one class. I'm not an enum-expert, I'll have to check it out.
[ October 20, 2008: Message edited by: Jan Sterk ]