Creating a Longitude type based on jsr363

66 views Asked by At

How would you create a Longitude type based on the JSR363 API ?

A longitude can be represented for example as a double between ]-pi,+pi] radians or between ]-180,180] degrees. If you add 20 degrees to a 175 degrees longitude you get -165 degree. This mean the add() method need to handle the wrapping but I don't see how to extend the current implementation.

@cneller : Yes it would be implementing Quantity<Angle> somehow but it could have been as simple as :

public class Longitude extends NumberQuantity<Angle> {

    public Longitude(Number number, Unit<Angle> unit) {
        super(normalize(number, unit), unit);
    }

    public static Number normalize(Number angle, Unit<Angle> unit) {
      ...
    }
}

But that doesn't work with the current implementation because it calling Quantities.getQuantity(..) internaly which is a sort of factory that doesn't know about Longitude.

1

There are 1 answers

0
cneller On

I would recommend implementing the Quantity interface.

You can find examples from the authors of the JSR here: http://central.maven.org/maven2/tec/units/unit-ri/1.0.1/unit-ri-1.0.1-sources.jar