In Java, I've been trying to find a way to:
- Group enums in such a way that they can be accessed such as: MainGroup.FirstEnumGroup.GREEN.
- With the grouped enums, have a base enum (sort of like a bass class) that can reference the group of enums. In the example below, MainGroup is the base enum for the group of 2 seperate enums. But, the problem is, MainGroup is not able to reference either FirstEnumGroup or SecondEnumGroup.
- Be able to pass the group of enums to a single method to be processed.
For example, let's say I have two seperate enums. And I'd like to be able to call a method passing either of the two seperate enums. The only way I can see this working is through having two seperate methods to receive each seperate enum. Instead, I'd like to find a way to group the enums and allow the user to select an enum from the group, and have a method handle it.
Do I wrap and group the enums into a class somehow? Use EnumSet somehow?
Below is an example class of what I'm trying to accomplish. Although, bare in mind, some of this is really just pseudocode of how I would like for it to work to sort of give the idea.
Specifically, see the Run()
method below.
// NOTE: This is not expected to compile.
class PseduocodeTestClass
{
// The idea here is that I would like to have a way to group the enums
// in such a way where they can be selected by the user as such:
// Ex #1: MainGroup.FirstEnumGroup.RED
// Ex #2: MainGroup.SecondEnumGroup.TRUCK
enum MainGroup
{
DUMMY1,
DUMMY2;
enum FirstEnumGroup
{
RED,
GREEN,
BLUE
};
enum SecondEnumGroup
{
CAR,
TRUCK
}
}
// This is what I would like to avoid, multiple method
// calls.
// void processEnum(MainGroup.FirstEnumGroup e)
// {
//
// }
//
// void processEnum(MainGroup.SecondEnumGroup e)
// {
//
// }
//Instead, I would like to have one single method by which I could
//pass different grouped enums to one method.
void processEnum(MainGroup e)
{
int ordinal = 0;
// below is SIMPLY pseduocode on how this could be envisioned to
// be used. I'd like to be able to pull the enum out that was passed
// into this method.
if(e.getClass() == MainGroup.FirstEnumGroup)
{
//again, this doesn't obvioulsy work, but this is just to demonstrate
//the desire to pull the enum out somehow. Either cast it or otherwise.
MainGroup.FirstEnumGroup theSelectedEnum = e.getClass();
ordinal = theSelectedEnum.ordinal();
}
else if(e.getClass() == MainGroup.SecondEnumGroup)
{
MainGroup.SecondEnumGroup theSelectedEnum = e.getClass();
ordinal = theSelectedEnum.ordinal();
}
// DO SOMETHING HERE WITH 'ordinal'.
}
void Run()
{
//Start of with BLUE
MainGroup e = MainGroup.FirstEnumGroup.BLUE;
//First we pass in BLUE from the FirstEnumGroup
processEnum(e);
//select a different enum
e = MainGroup.SecondEnumGroup.TRUCK
//Second we pass in TRUCK from the SecondEnumGroup
processEnum(e);
//Finally, pass in RED
processEnum(MainGroup.FirstEnumGroup.RED);
}
};
Unfortunately, I've not been able to find a way to do what I want to do. Maybe, there really is no way to simply accomplish this?