Nested enums in Java?

enums, java

Solution

While there were some good answers which may work well for other situations, I've decided to go with the following solution as it works best for my situation:

public enum Service
{
    LOGIN,
    REGISTER,
    NEWS;

    private final long LOADED, FAILED;

    private RequestType()
    {
        LOADED  = strToAscii(this.name() + "_LOADED"  );
        FAILED = strToAscii(this.name() + "_FAILED"  );            
    }

    public static long strToAscii(String str)
    {
       StringBuilder sb = new StringBuilder();

       for (char c: str.toCharArray() )
       {
           sb.append( (int) c );
       }

      return Long.parseLong( sb.toString() );
   }
}

This works well for me, since I can now do `RequestType.LOGIN.LOADED == RequestType.REGISTER.LOADED` as I originally wanted. If anyone has any suggestions for improvement, feel free to suggest.

Problem

I want to define some enums for the various ajax services that I have available in my web application, like: ``` Enum Service { REGISTER, LOGIN, NEWS, FAQ } ``` However, each of these enums will also have a certain state like Failed, Loaded, etc. So I want to be able to use `REGISTER.LOADED`, `LOGIN.LOADED` etc, to fire up events on my event bus. However each state enum must be unique. I.e `Register.LOADED` must be different from `FAQ.LOADED`, and so on. Edit: Also, I must be able to store all states in the same hashmap, e.g `Register.LOADED` and `Login.LOADED` must be storable in the same hashmap. And the parent service enums, i.e`LOGIN, REGISTER` etc must be storable in the same hashmap as well. What's the best way to accomplish this?

Original source