Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Messy enum problem

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm getting myself a bit tied up with enums. I have an enum with an int value and a String description which uses symbols:



However, what I want to do is find a clean way to return the value for a given descriptor. I can do it by wrapping the enum in a class with :



but now my nice clean type got all messy. Is there a cleaner approach?

Thanks.

Ken
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't seem that messy to me. You talk about "wrapping the enum in a class" but that seems unnecessary - you can define the static field and method as part of the enum itself.

There is something similar to this already built into enums - the ability to look up values by name, using a String. E.g.

This build-in functionality is very similar to what you want, but it only works using the identifiers for the enum values you have defined - in Type above, that's "ABC", "DEF", "GHI". If you want this sort of easy lookup you can often choose your identifiers to match the existing strings you want to use for lookup. Except for things like "*&", "��", "^^" you can't do this, because those aren't legal Java identifiers. So, you've got to use a Map as a static field, just as you've done above.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove the mapping from your enum type to its own abstraction away from the enum. This is done with an interface that expresses the mapping operation in a method.

The "uncleanliness" that you are observing is called the object-oriented programming paradigm, which seems to have become linked with the notion of static type inheritance.

Simply, don't try to pile as many methods into a type as you can. Abstract atomic and unrelated behaviour.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Expanding on what Jim said: since just one instance of the Enum will be constructed for each value, you could add the values into the map in the constructor, eliminating the separate static block; i.e.,

map.put(descriptor, this);
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't do that, actually. See JLS 8.9:

It is a compile-time error to reference a static field of an enum type that is not a compile-time constant (�15.28) from constructors, instance initializer blocks, or instance variable initializer expressions of that type. It is a compile-time error for the constructors, instance initializer blocks, or instance variable initializer expressions of an enum constant e to refer to itself or to an enum constant of the same type that is declared to the right of e.

(See also the discussion section which follows.) You can't reference a static field (the Map, in this case) from an enum constructor, since it hasn't been initialized yet. I suppose they could have allowed this to work if the static field were allowed to be declared before the enum values, but that would've muddied up the enum definition, and still been error prone, I think. Unless the compiler were to force the correct ordering of the declarations. They didn't do that though, so the short answer is, we can't reference a static field in an enum constructor - that's why I used the static block.
[ April 03, 2006: Message edited by: Jim Yingst ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic