开发者

Unit of measurement API in Java?

开发者 https://www.devze.com 2023-03-15 19:19 出处:网络
JSR-275 has been rejected, the Units of Measurement API for Java project is a set of interfaces, but haven\'t found an open source implementation.

JSR-275 has been rejected, the Units of Measurement API for Java project is a set of interfaces, but haven't found an open source implementation.

On this post: Which jsr-275 units implementation should be used? the project owner mentions the implementation was 开发者_高级运维going to be ready by the end of last year on JScience, but didn't find anything there to convert between weight or length units and when I looked for JScience on https://maven.java.net/, I found it, but the JAR wasn't even in the directory https://maven.java.net/content/repositories/snapshots/org/jscience/jscience/5.0-SNAPSHOT/, so I had to get it from somewhere else.

Has this project been left behind? And is there currently an implementation for conversion of Units of measurement in Java and even perhaps a Maven repo?


Unit-API (unitsofmeasurement.org) is the successor to JSR-275.

The most active implementation at the moment is Eclipse UOMo


As far as I know, JScience is very much alive. The project is currently being migrated to Java.net, and the migration is not complete. That is most likely the reason why you are unable to see snapshot JARs for 5.0. In fact, the most recent snapshot was prepared only after resolution of a particular configuration problem reported in the Java.net JIRA. May be you ought to wait a few days, or probably send a mail to the project administrator on what JScience POM must be used in the interim.


Update on this JSR-363 Units of Measurement API was completed in 2016 and provides a fairly complete UoM API. This has now been superseded by JSR-385 Units of Measurement API 2.0. You can find the code behind the API and implementation on Github here https://github.com/unitsofmeasurement.

Here is a simple conversion example using UoM API 2.0

import tech.units.indriya.quantity.Quantities;
import javax.measure.Quantity;
import javax.measure.quantity.Length;

import static javax.measure.MetricPrefix.CENTI;
import static tech.units.indriya.unit.Units.METRE;

class SimpleUnitExample {
    public static void main(String[] args) {
        Quantity<Length> lengthQuantity = Quantities.getQuantity(25, METRE);
        System.out.println(lengthQuantity.to(CENTI(METRE)));
    }
}

With a dependency on tech.units:indriya:2.0.4 will print 2500 cm.

You can find lots more examples in this repo uom-demos.

0

精彩评论

暂无评论...
验证码 换一张
取 消