DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Extended Enums usage

Extended Enums usage

Frederic Simon user avatar by
Frederic Simon
·
Jul. 06, 11 · Java Zone · Interview
Like (0)
Save
Tweet
5.48K Views

Join the DZone community and get the full member experience.

Join For Free

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 {
        ALL("local-all"),
        INTERNAL("local-int"),
        OR("local-int-ext");

        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 {
        ALL("local-all"),
        INTERNAL("local-int"),
        OR("local-int-ext");
    }

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

Coding (social sciences) Javac XML HTML JSON Integrated development environment Object (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Role of Development Team in an Agile Environment
  • How to Determine if Microservices Architecture Is Right for Your Business?
  • 6 Best Books to Learn Multithreading and Concurrency in Java
  • 3 Predictions About How Technology Businesses Will Change In 10 Years

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo