3

In Java, I've been trying to find a way to:

  1. Group enums in such a way that they can be accessed such as: MainGroup.FirstEnumGroup.GREEN.
  2. 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.
  3. 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?

Rodia
  • 1,407
  • 8
  • 22
  • 29
JasonF
  • 31
  • 1
  • 4
  • *"...and, I'd like to be able to call a method passing either of the two seperate enums. "* ... the best way to do this is to have each of your enum classes implement a common interface that specifies the contract of behaviors that are common to the enum groups. If your method is defined to accept a parameter of the interface type, then you can pass in any enum constant from any group that has implemented that type. – scottb Mar 09 '16 at 05:53
  • maybe like: http://stackoverflow.com/questions/8732710/enum-within-an-enum/8941089#8941089 – Ray Tayek Feb 13 '17 at 03:23
  • or http://stackoverflow.com/questions/8671088/breaking-up-a-large-java-enum/8941551#8941551 – Ray Tayek Feb 13 '17 at 03:36

1 Answers1

1

The issue is you are trying to reference MainGroup.FirstEnumGroup.BLUE as MainGroup type which is not correct since its type is FirstEnumGroup. The same holds true for SecondEnumGroup. What you can do is to have a marker interface for both FirstEnumGroup and SecondEnumGroup and then the return type can be that interface.

Something like this:

public interface EnumGroupInterface {
}

enum FirstEnumGroup implements EnumGroupInterface {
 ...
}

enum SecondEnumGroup implements EnumGroupInterface {
 ...
}

void Run() {
    EnumGroupInterface e = MainGroup.FirstEnumGroup.BLUE;
    ...
}

void processEnum(EnumGroupInterface e) {
 ...
}
Ankit Bansal
  • 4,962
  • 1
  • 23
  • 40