aspose file tools*
The moose likes Java in General and the fly likes Getting a enum based upon one of its fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting a enum based upon one of its fields" Watch "Getting a enum based upon one of its fields" New topic
Author

Getting a enum based upon one of its fields

Bobby Anderson
Ranch Hand

Joined: Oct 28, 2008
Posts: 114
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

Joined: Oct 27, 2005
Posts: 19783
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bobby Anderson
Ranch Hand

Joined: Oct 28, 2008
Posts: 114
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jun 21, 2008
Posts: 423
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

Joined: Jul 27, 2009
Posts: 84
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.


Thanks
Sathish kumar
SCJP, SCWCD
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

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

Joined: Oct 13, 2005
Posts: 40034
    
  28
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting a enum based upon one of its fields