Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting a enum based upon one of its fields

 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • 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.
 
G.Sathish kumar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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).
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic