简体   繁体   English

Java中的度量单位API?

[英]Unit of measurement API in Java?

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 已被拒绝,Java 项目的度量单位 API 是一组接口,但尚未找到开源实现。

On this post: Which jsr-275 units implementation should be used?在这篇文章中: 应该使用哪个 jsr-275 单元实现? 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.项目所有者提到该实现将在去年年底在 JScience 上准备就绪,但没有找到任何可以在重量或长度单位之间转换的内容,当我在https://maven.java.net上查找 JScience 时/ ,我找到了,但 JAR 甚至不在目录https://maven.java.net/content/repositories/snapshots/org/jscience/jscience/5.0-SNAPSHOT/ 中,所以我必须从别的地方。

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?目前是否有在 Java 甚至 Maven 存储库中转换度量单位的实现?

Unit-API ( unitsofmeasurement.org ) is the successor to JSR-275. Unit-API ( unitsofmeasurement.org ) 是 JSR-275 的继承者。

The most active implementation at the moment is Eclipse UOMo目前最活跃的实现是Eclipse UOMo

As far as I know, JScience is very much alive.据我所知,JScience 非常活跃。 The project is currently being migrated to Java.net , and the migration is not complete.项目目前正在迁移至Java.net ,迁移未完成。 That is most likely the reason why you are unable to see snapshot JARs for 5.0.这很可能是您无法看到 5.0 的快照 JAR 的原因。 In fact, the most recent snapshot was prepared only after resolution of a particular configuration problem reported in the Java.net JIRA.事实上,最近的快照是在解决了 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.可能您应该等待几天,或者可能向项目管理员发送一封邮件,说明在此期间必须使用什么 JScience POM。

Update on this JSR-363 Units of Measurement API was completed in 2016 and provides a fairly complete UoM API.JSR-363 度量单位 API 的更新于 2016 年完成,并提供了相当完整的计量单位 API。 This has now been superseded by JSR-385 Units of Measurement API 2.0 .这现已被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 .您可以在https://github.com/unitsofmeasurement在 Github 上找到 API 背后的代码和实现。

Here is a simple conversion example using UoM API 2.0这是一个使用 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 .依赖于tech.units:indriya:2.0.4将打印2500 cm

You can find lots more examples in this repo uom-demos .你可以在这个 repo uom-demos 中找到更多的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM