Enum not flexible enough?

I am a little bit frustrated with the new enum statement and Enum class.

It is true it has much more features than the equivalent in other languages, but I feel like

it just lacks something to make it truly great.

Sorry if people already debated the topic before...

My main problem is the following:

"All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else."

I feel like it shouldn't be the case, we should have the possibility to extend manually the Enum class and have enum instantiate our new class.

Let's take for example the following problem: In my swing app, I would like to have a class that basically represent a JButton.

publicclass ButtonEnumextends Enum{

JButton m_button;

public ButtonEnum(String name,int ordinal, String label){

super(name, ordinal);

m_button =new JButton(label);

}

public JButton getButton(){

return m_button;

}

}

Then in other classes I could easily instantiate those ButtonEnum and manipulate them in a very convenient way.

publicclass DevicePanelextends JPanelimplements ActionListener{

// easily create instances

enum<ButtonEnum> Button{

START("Start"),

STOP("Stop");

}

...

// manipulate them

for (Button button : Button.values())

button.getButton().addActionListener(this);

...

// easy action listener

publicvoid actionPerformed(ActionEvent event){

if (event.getSource() == Button.START.getButton()){

...

}

}

}

// Do the same in other classes

publicclass OtherPanelextends JPanelimplements ActionListener{

// easily create instances

enum<ButtonEnum> Button{

EDIT("Edit"),

DELETE("Delete");

}

...

}

This would be even more convenient for a class with a lot of properties and fields. It enables to specify all fields together during instantiation, reduce the line of codes, makes it easy to go through the enums and test them.

Basically the enum statement would have a "generics" to choose which class you want as a base. That class would have to implement an "Enum interface", which can be easily done by extending Enum.

The "enum<ButtonEnum> Button" statement would automatically create the Button class that extends ButtonEnum instead of Enum, and then add the static fields. The only trick is the constructor, which should automatically be bound to the ButtonEnum's one.

Just a thought...

Maybe there is a solution to do all that with the current features?

Basically I want to be able to:

- create a bunch of JButtons, if possible with one line of code for each.

- Be able to iterate through them with a loop

- Be able to test a JButton the same way as "if (event.getSource() == m_StartButton)"

- All of this is possible with a local Enum, but I would like to use this in several classes that use different buttons.

The best way I can see is to:

// Create an enum for each button

enum Button{

START,

STOP;

}

// have a seperate array of JButton

JButton[] m_buttons;

// then instantiate in the constructor

public DevicePanel(){

m_buttons =new JButton[Button.values().length];

m_buttons[Button.START.ordinal()] =new JButton("Start");

m_buttons[Button.STOP.ordinal()] =new JButton("Stop");

}

// then you can iterate and test

...

This is much heavier than the previous solution...

What do you guys think? Thank you for your advice

AJG

[5627 byte] By [AJGa] at [2007-10-3 2:51:34]
# 1

My opinion is, you totally misunderstood the concept of Enums.

The whole purpose of an enum is to provide constants. They may have complex behaviour, but still they are constant.

Your idea is to add flexibility to something which by definition is something very unflexible.

The whole point of constants is, that they don't care in what context they are used. They don't really react to their environment, they just are. They may appear different under certain circumstances, but they really aren't.

What you have described here, can already be done - for example with the factory pattern. Nobody hinders you to write a button creation factory, that accepts an arbitrary list of names, and returns a list of created buttons.

Mongera at 2007-7-14 20:40:27 > top of Java-index,Java Essentials,Java Programming...
# 2

>Basically I want to be able to:

>- create a bunch of JButtons, if possible with one line of code for each.

then write a utility method which creates them

>- Be able to iterate through them with a loop

just so you can add an ActionListener to them? Put that in the utility method.

>- Be able to test a JButton the same way as "if (event.getSource() == m_StartButton)"

Just use that technique.

IanSchneidera at 2007-7-14 20:40:27 > top of Java-index,Java Essentials,Java Programming...
# 3

Might be nice, though, to be able to create a new enum which has all the constants of an old one and some extras, but the new one couldn't be a subclass of the old, because it would clearly be illegal to store one of the extended values to the old enum.

In fact the new enum would become a super class of the old.

How about

enum BaseEnum { One, Two };

enum ExtendedEnum super BaseEnum {Three, Four};

malcolmmca at 2007-7-14 20:40:27 > top of Java-index,Java Essentials,Java Programming...
# 4

Thanks for the comments guys.

My exemple was probably not that great.

I understand that enums are supposed to be used as "final constants" but it is very useful to have constants with several fields like:

enum Color {

BLUE_PEARL { "Blue Pearl", "Mat", "#987898D" }

}

Now I completely understand that this can come from a database or xml, and go through a factory to create an instance, but still I think it would be handy for simple cases, especially with the syntaxic shortcuts.

Note also that it's already possible to do that, just you have to add the fields & contructor in each local enum. My suggestion was to be able to do this with the field & constructor shared in a seperate class.

AJG

AJGa at 2007-7-14 20:40:27 > top of Java-index,Java Essentials,Java Programming...
# 5
EnumMaps are useful for hanging auxilliary data on enums, you can initialise them in a static block.
malcolmmca at 2007-7-14 20:40:27 > top of Java-index,Java Essentials,Java Programming...