Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Getting a enum based upon one of its fields

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like a good way to find what enum I need based upon one of its fields.

With:


Now lets say that I have a mass but I want to know which enum it belongs to.
I know that I can loop over all of the values() and compare each values mass to the mass I have to determine which enum to use but that seems like over kill

The other thing I was thinking is having static lists that can convert from a mass to and enum or radius to an enum, i.e.


Is this the best way. Am I missing anything?

Thanks,
Billy
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what will you do if two planets happen to have the same mass and/or radius?

Regardless, when you don't want to iterate over the values a Map is a good idea.
 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i agree that was a bad example.

For lack of a better example what if I know that my enum's values will always be unique to the enum's value. I.E. instead of weight how about distance from the sun. All planets have a unique distance from the sun. Is this still a good design or is there a better way to associate those?

Thanks,
Billy
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that I'm aware of, with Rob's caveat. The code can be encapsulated in the enum and remain relatively clean.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Billy Newman wrote:Ok i agree that was a bad example.

Is this still a good design or is there a better way to associate those?



The question is why are you looking for a better way to do it than simple loop over all values?
Is your code is a performance bootleneck, or maybe it consumes to much memory ?
If no - keep it as simple as possible, simple loop will be easier to understand/maintain by someone else,
or even by you in two years, when you will completely forget why this stuff is so complicated and how it works.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you use constants (final) enum is better to use because without declare lot of constant we can wrape it to enum which good to use.
 
Rob Spoor
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ireneusz Kordal wrote:

Billy Newman wrote:Ok i agree that was a bad example.

Is this still a good design or is there a better way to associate those?



The question is why are you looking for a better way to do it than simple loop over all values?
Is your code is a performance bootleneck, or maybe it consumes to much memory ?
If no - keep it as simple as possible, simple loop will be easier to understand/maintain by someone else,
or even by you in two years, when you will completely forget why this stuff is so complicated and how it works.


I don't agree. A Map should be easy enough to understand.

As for consuming too much memory, that would be a reason not to use a Map. Looping over values() creates a new array with all values, but when that goes out of scope it will be garbage collected. The Map on the other hand will remain in memory indefinitely since it's static (or at least until the enum class is unloaded).
 
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way (with moderator's hat on): we like all posters to quote their sources. You ought to have said where that enum was from. Is it the example in the Java Tutorials?
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic