Re: Question about Typesafe Enums
- From: "Rhino" <no.offline.contact.please@xxxxxxxxxx>
- Date: Wed, 14 Dec 2005 19:34:04 -0500
"Thomas Hawtin" <usenet@xxxxxxxxxxxxxxxxx> wrote in message
news:43a09d36$0$1471$ed2619ec@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Rhino wrote:
>> http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html. [...]
>
>> System.out.println("The mass of the Earth is " + Planet.EARTH.Mass);
>>
>> I'm not wedded to the syntax in that last example; in fact, it looks a
>> bit nasty to me. I'm just trying to indicate that it would use the new
>> Mass enum instead of the mass() method to refer to the first value
>> associated with the given planet.
>
> I'm not entirely sure as to what you want here.
Yeah, I'm not having a lot of luck articulating my wishes in a way that
seems clear even to me, let alone anyone else ;-)
> An enum has a limited set of values, but mass is a (near) continuous
> quantity. So, an enum of mass doesn't make sense.
>
> I'll assume you want an enumeration of properties (mass, radius, etc),
> which can be applied to any Planet.
Yes, that's right!
I didn't put it very well, did I? ;-) It obviously makes no sense to have an
enumerations that contains all the values possible for a planetary mass. I
should have made it clearer that I wanted to enumerate the _properties_ of a
planet. Those would be relatively few, such as mass and radius, and would
lend themselves better to an enum.
Then I want to use the names of the properties to access the actual values
so that I can say something like Planet.EARTH.MASS to get the mass of the
Earth.
> So that you can write a method that prints a property of a given planet,
> but is general to any property. Here's a way to do that.
>
> enum PlanetMeasure {
> RADIUS {
> public double of(Planet planet) {
> return planet.radius();
> },
> MASS {
> public double of(Planet planet) {
> return planet.mass();
> },
> ;
> public abstract double of(Planet planet);
> }
>
> static void printPlanetMeasure(
> Planet planet, PlanetMeasure property
> ) {
> System.out.println(
> "The "+property+" of "+planet+" is " +
> property.of(planet)
> );
> }
>
> (I was going to call the method "value" instead of "of", but that kind of
> clashes with the namings of the enum infrastructure. I'm not entirely
> happy with either. "of" is far too cute.)
>
I *think* you're on the right track but....
I got several errors when I tried to compile the PlanetMeasure class in
Eclipse. Frankly, I'm somewhat confused by what you're doing (not surprising
since I've asked an unclear question!) that it wasn't obvious to me why
these errors were arising. I took a guess and found that the following
compiled fine, although I don't know if it changes the intent of your
code....
===============================================
public enum PlanetMeasure {
RADIUS {
public double of(Planet planet) {
return planet.radius();
}
},
MASS {
public double of(Planet planet) {
return planet.mass();
}
},
;
public abstract double of(Planet planet);
static void printPlanetMeasure(Planet planet, PlanetMeasure property) {
System.out.println("The " + property + " of " + planet + " is " +
property.of(planet));
}
}
================================================
Now, even if I've made reasonable alterations in the code, I'm not quite
clear how to use what you've done; how/where do I specify the _values_ of
the mass and radius for each of my planets so that I can get them back out
again with the of() method?
> Obviously, it gets more complicated if you want to use properties of
> different types.
>
If you can help me understand what you've done so far, I might be able to
work out the rest on my own....
Rhino
.
- Follow-Ups:
- Re: Question about Typesafe Enums
- From: Thomas Hawtin
- Re: Question about Typesafe Enums
- References:
- Question about Typesafe Enums
- From: Rhino
- Re: Question about Typesafe Enums
- From: Thomas Hawtin
- Question about Typesafe Enums
- Prev by Date: Re: Creating a Timeout on a method
- Next by Date: Re: GNU (or GNL or public domain) Checkers AI
- Previous by thread: Re: Question about Typesafe Enums
- Next by thread: Re: Question about Typesafe Enums
- Index(es):
Relevant Pages
|