JFrog Ltd founder in 2008, and AlphaCSP founder in 1998 as chief architect. My development experience goes back to 1992, and I'm hooked on Java since 1997. My Open Source activity are with jfrog and setllarium4Java. Frederic is a DZone MVB and is not an employee of DZone and has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

Extended Enums usage

  • submit to reddit

Since the re-launch of extended enums I'm paying attention in my everyday coding (yes I still write code :) if extended enums will help me.
Here are 2 new examples:

1) I found out that most of the time the name() of the enum is not what I need. I need it to map an XML or HTML tag, an external ID, an entry name in excel or simple type name in a JSON object. So I write something like:

    public enum ConfigName {

        private final String xmlKey;

        LdapConfig(String xmlKey) {
            this.xmlKey = xmlKey;

        public String getXmlKey() {
            return xmlKey;

I could remove the getter, and the IDE helping a lot writing the boiler plate code but why? Here is how it should look:

    public enum ConfigName extends AlternateEnumKey {

The extra feature here is that the name() of the enum (ALL, INTERNAL, OR) is a constant that is not recognized as such by the javac compiler. So:

    @XmlTag(tag = ConfigName.ALL)

will not compile and so:

    @XmlTag(tag = ConfigName.ALL.xmlKey)

will for sure not.

But with extended enum you'll have:

    public @interface XmlTag { AlternateEnumKey tag(); }

    // The framework managing XmlTag will take the alternate key value
    @XmlTag(tag = ConfigName.ALL)

2) Since extended enums also supports generics for enums, and I just have this issue associating also a long (ID) to an enums, you can now write:

    public enum ConfigName extends EnumPairExtension<Long, String> {
        ALL(45L, "local-all"),
        INTERNAL(56L, "local-int"),
        OR(98L, "local-int-ext");

    assert ConfigName.OR.a == 98L;

With this, parsing definitions are a lot cleaner to write.

From http://freddy33.blogspot.com/2011/07/extended-enums-usage.html

Published at DZone with permission of Frederic Simon, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)