java enum with enum in constructor

Marthin picture Marthin · May 6, 2011 · Viewed 15.7k times · Source

Is it possible to let a enum in java take a set of enums as as argument? If yes, then how do I implement it?

When using this I whant to be able to say something like: Take a MODAL_SETTINGS.EDIT_MODAL_WINDOW and create this with the buttons MODAL_BUTTONS.SAVE & MODAL_BUTTONS.CANCEL.

This is what I have as of now

public enum MODAL_SETTINGS {
    NEW_MODAL_WINDOW(MODAL_BUTTONS.class),
    EDIT_MODAL_WINDOW(MODAL_BUTTONS.class),
    DELETE_MODAL_WINDOW(MODAL_BUTTONS.class);

    private EnumSet buttons;

    private MODAL_SETTINGS(EnumSet<MODAL_BUTTONS> buttons){

    }

}
public enum MODAL_BUTTONS {
    SAVE, UPDATE, CANCEL, DELETE
}

Answer

Jon Skeet picture Jon Skeet · May 6, 2011

Instead of this:

NEW_MODAL_WINDOW(MODAL_BUTTONS.class),

I suspect you want this:

NEW_MODAL_WINDOW(EnumSet.allOf(MODAL_BUTTONS.class))

or

NEW_MODAL_WINDOW(EnumSet.of(MODAL_BUTTONS.SAVE, MODAL_BUTTONS.CANCEL))

(etc).

Otherwise you're just passing a Class<T>, not an EnumSet.