Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum within an enum

Tags:

java

enums

nested

This isn't a matter of me being stuck, but rather I'm looking for a tidy way to write my code.

Essentially, I'm writing an event driven application. The user triggers an event, the event gets sent to the appropriate objects, and the objects handle the events. Now I'm working on writing the even handler methods, and I was hoping to use switch statements to determine how to handle the event. Right now whilst I'm working on the general structure, the event class is really simple:

public class Event {      public static enum Action {         MOVE, FOO, BAR     }      private Action action;     private int duration;      public Event(Action action, int duration) {         this.action = action;         this.duration = duration;     }      public Action getAction() {         return action;     }      public int getDuration() {         return duration;     } 

Then, in another class, I'll have something like:

public void handleEvent(Event evt) {         switch(Event.getAction()) {         case MOVE: doSomething(); break;         case FOO:  doSomething(); break;         case BAR:  doSomething(); break;         default: break;      } } 

What I would like to do is something like this (though I would of course stick the switch statements into their own functions to avoid it turning into a nasty hairball of switches and cases):

public void handleEvent(Event evt) {         switch(Event.getAction()) {         case MOVE: switch(Event.getAction()) {                        case UP:    break;                        case DOWN:  break;                        case LEFT:  break;                        case RIGHT: break;                    }         case FOO:  break;         case BAR:  break;         default:   break;      } } 

So, I'd want to create nested enums... like so:

public static enum Action {     public enum MOVE {UP, DOWN, LEFT, RIGHT}, FOO, BAR } 

It's not like I can't avoid the scenario, it would just be... convenient. So whilst the above doesn't actually work, is there some similar method to achieve this? It would be nice if I could send an event with the action "MOVE.UP", and the method would identify it first as an action of type MOVE, and then further identify that it is specifically in the UP direction. That's just a simple example, it would be grat if I could also make longer chains, something like "DELETE.PAGE1.PARAGRAPH2.SENTENCE2.WORD11.LETTER3". The way I see it, I'm just going to have to use Strings and lots of if/else statements. Hoping there's a better way! (Oh, and performance matters in my case, if that helps)

like image 290
Fault Avatar asked Jan 04 '12 19:01

Fault


People also ask

Can we have an enum within an enum?

yes, but you need to know that.

Can I have enum inside enum in Java?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.

Can we have enum inside enum in C?

It is not possible to have enum of enums, but you could represent your data by having the type and cause separately either as part of a struct or allocating certain bits for each of the fields.

Can an enum extend another enum?

No, we cannot extend an enum in Java. Java enums can extend java. lang. Enum class implicitly, so enum types cannot extend another class.


2 Answers

I believe that in Java, you can simply nest enums, as long as your non-enum constants come first.

enum Action {     FOO,     BAR;     enum MOVE     {          UP,          DOWN,          LEFT,          RIGHT      } } 

This compiles for me and gives me the behavior you were looking for.

like image 95
Patrick Perini Avatar answered Oct 05 '22 19:10

Patrick Perini


Perhaps use an inheritance hierarchy for the Events?

So you have:

- abstract Event -- MoveEvent(Direction) -- FooEvent() -- BarEvent() 

It may make more sense to have:

- abstract Event -- abstract MoveEvent --- MoveUpEvent --- MoveDownEvent --- MoveRightEvent --- MoveLeftEvent -- FooEvent -- BarEvent 

If all the Move events have a distance, then pass that into the MoveEvent constructor (which will ripple down).

like image 41
Dan Hardiker Avatar answered Oct 05 '22 19:10

Dan Hardiker