I’d like to create a few enums such as described in this answer:
Lookup enum by string value
(I.e an enum that has a toString() and a name which is different to the enum itself)
Is there any way I can reuse this functionality without having to reimplement it in each enum?
If you pass in the list of possible values, then you can do it without so much as depending upon the type being an
enum.Call with:
Rather than calling
Blah.values()every time, you could use put it in an object, which could be passed around and used generically. Perhaps other methods added later.Call with:
In fact, you might want to optimise that. Do the
toStringing once, and use a map.