Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Enums Dynamically

Tags:

Let's say I have a file whose format is basic XML, like so:

<?xml version="1.0"?>
<enum-set>
    <enum>
        <name>SomeEnum</name>
        <values>
            <value>
                <name>SOMEVALUE</name>
                <displayText>This is some value</displayText>
             </value>
            ... more values ...
        </values>
    </enum>
    ... more enums ...
</enum-set>

and I wanted to turn SomeEnum into something like this at runtime:

public enum SomeEnum implements HasDisplayText {
    SOMEVALUE("This is some value"),
    ... more values ...;

    private String displayText;

    SomeEnum(String displayText) {
        this.displayText = displayText;
    }

    @Override
    public String getDisplayText() {
        return displayText;
    }
}

... and then pass the newly created enum SomeEnum around my application. How might I achieve something like this? Is it doable?

like image 742
Chris Cashwell Avatar asked Dec 11 '11 20:12

Chris Cashwell


People also ask

Can you dynamically create an enum?

You cannot dynamically add cases to an enum. All of an enum's cases will be known at compile-time.

Can we have dynamic enum in Java?

Because Java enums associate each enum constant with a String value, a heavyweight DynamicEnumerable also needs to define a String name associated to its lightweight representation. The DynamicEnum itself is essentially a factory for the dynamic enum constants that represent a given DynamicEnumerable.

Are enums always static?

As enums are inherently static , there is no need and makes no difference when using static-keyword in enums . If an enum is a member of a class, it is implicitly static.

Can enums be subclassed?

We've learned that we can't create a subclass of an existing enum. However, an interface is extensible. Therefore, we can emulate extensible enums by implementing an interface.


1 Answers

What you're trying to do doesn't make a whole lot of sense. Enums are really only for the benefit of compile time, as they represent a fixed set of constants. At runtime, what would be the meaning of a dynamically generated enum - how would this be different from an plain object? For example:

public class Salutation implements HasDisplayText {

   private String displayText;

   private Salutation(String displayText) {
       this.displayText = displayText;
   }

   @Override
   public String getDisplayText() {
       return displayText;
   }

   public static Collection<Salutation> loadSalutations(String xml) {
      //parse, instantiate, and return Salutations
   }
}

Your XML could be parsed into newly instantiated Salutation objects, which could be stored in some Collection or otherwise used by your program. Notice in my example, I've restricted the creation of Salutation by giving it a private constructor - in this case the only way to retrieve instances is by calling the factory method which takes your XML. I believe this achieves the behavior you're looking for.

like image 184
Paul Bellora Avatar answered Oct 19 '22 19:10

Paul Bellora