I am doing astrophysical research. I wrote a package containing the classes Star, Band, and Datfile. I also have the enumerated type of BandName. Each star contains several Bands, each Band contains several Datfiles.
I have observational data for several galaxies. For each of these, I make a StarDatabase class (a HashMap of Stars) and a Main class.
The problem I’m having is with the enumerated type of BandName. So far, all of the data I have used has been in the I and V bands. Now I have data in J, H, and K bands. If I simply add J, H, and K to BandName, all of my loops that iterate over all of the items in BandName and do something are now broken.
Any ideas?
Edit: To sum up my problem, I want every package to have its own BandName enum that it can iterate through. But this doesn’t work, because the methods in the Star package are expecting objects of type Star.BandName and I am providing objects of type IndividualPackage.BandName.
You can’t inherit an enum from another enum, although you can have your enum implement an interface. The technical problem (that all enums implicitly extend
java.lang.Enum, thus they can’t extend another class, only implement additional interfaces) is no accident:From Effective Java 2nd Edition, Item 34.
However, I don’t fully understand your problem: haven’t you used
values()for iterating through your enum? Then you shouldn’t worry about extending your enum with new values.Please specify more clearly what “broken” is supposed to mean.
Update: so you need to have distinct sets of bands for different types of stars – this can be implemented using distinct enums extending a common interface, e.g.:
And you can use these by making your Star class generic:
This works nicely if the bands are organized into distinct groups. If there are stars with mixed band groups, however, you might prefer an alternative approach:
This allows you to set up stars with an arbitrary mix of bands. However, this way you can’t use
EnumSetorEnumMapon the bands.