Re: enum paralellism
On 1/16/2012 9:16 AM, Roedy Green wrote:
What if you have two enum classes that have similar structure, e.g.
similar method names, similar instance variables or similar enum
constants.
Is there any way to specify that similarity in one place or to use
interfaces, abstract classes, EnumSets or inheritance to enforce the
parallel structure? I have not found a way.
Is this parallelism a result of using classes from different
sources, each with its own enum to represent the same set of things?
For example, do you have two different enums with constants MUNCHKIN,
QUADLING, WINKIE, and GILLIKIN, in class libraries obtained from
independent authors of Oz software? If so, I think your best bet may
be to invent an enum of your own with references to the corresponding
"foreign" instances:
package com.mindprod.oz;
import com.baum.oz.Country;
import net.fandom.oz.OzLands;
enum OzTerritories {
MUNCHKIN(Country.MUNCHKIN, OzLands.MUNCHKIN),
QUADLING(Country.QUADLING, OzLands.QUADLING),
WINKIE(Country.WINKIE, OzLands.WINKIE),
GILLIKIN(Country.GILLIKIN, OzLands.GILLIKIN);
private final Country baumEnum;
private final OzLands fandomEnum;
private OzTerritories(Country baum, OzLands fandom) {
baumEnum = baum;
fandomEnum = fandom;
}
...
}
On the other hand, if the parallelism is in your own classes
and of your own invention, perhaps the parallel enums should really
just be one omnibus enum. For example, if in one enum you list the
lands of Oz and indicate their general locations (MUNCHKIN is East,
despite much confusion) and in the other you associate each land
with its proper witch (Glinda the Good rules in GILLIKIN), maybe
you should combine the now-parallel enums into a single enum that
embodies all the attributes of interest. Alternatively, you might
settle for a single "bare" enum with no (or few) attributes of its
own, plus a bunch of EnumMap's to hold the associations.
On the gripping hand, perhaps I've completely failed to grasp
(pun intended) the nature and origins of the parallelism you face.
Could you describe it further?
--
Eric Sosman
esosman@ieee-dot-org.invalid