Re: Question about Typesafe Enums




"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


.



Relevant Pages

  • Question about Typesafe Enums
    ... In the Planet enum that is shown in the article, they have a list of planets ... and each planet has two specific values, a mass and a radius. ... instead of the massmethod to refer to the first value associated with the ...
    (comp.lang.java.programmer)
  • Re: Question about Typesafe Enums
    ... In the Planet enum that is shown in the article, they have a list of planets and each planet has two specific values, a mass and a radius. ... I'm just trying to indicate that it would use the new Mass enum instead of the massmethod to refer to the first value associated with the given planet. ...
    (comp.lang.java.programmer)
  • Daily Report #4844
    ... sequence stars other than the Sun. ... to carry out FGS astrometric studies on four stars hosting seven ... Our understanding of the planet formation process will ... as such by assuming that the minimum mass is the actual mass. ...
    (sci.astro.hubble)
  • Re: Our expanding earth - video
    ... So why are planets round? ... sufficient mass, and sufficient pressure of these waves, pushing out, ... The question I have always had though is if, a planet, could form, by ... well be Bose Einstein condensate. ...
    (sci.physics)
  • Re: mass increase due to speed
    ... and if "inertial mass" changes then so does ... All mass measurements in your frame are with respect to other ... To rephrase the question, if a planet flies by us going "real fast", ... acceleration depends on its velocity. ...
    (sci.physics.relativity)