Java 打印出JScience中所有定义的单位

Java 打印出JScience中所有定义的单位,java,units-of-measurement,jscience,Java,Units Of Measurement,Jscience,我们有一个系统,用户可以提供任何单位的数据。数据被存储并可能转换为相同维度的任何其他单位。该应用程序基于JScience api 我想列出所有受支持的单元,以及它们拥有的所有别名。我似乎找不到这样做的方法。目前我正在做这件事: for (javax.measure.unit.Unit<?> unit: javax.measure.unit.SI.getInstance().getUnits()) System.out.println(UnitFormat.ge

我们有一个系统,用户可以提供任何单位的数据。数据被存储并可能转换为相同维度的任何其他单位。该应用程序基于JScience api

我想列出所有受支持的单元,以及它们拥有的所有别名。我似乎找不到这样做的方法。目前我正在做这件事:

    for (javax.measure.unit.Unit<?> unit: javax.measure.unit.SI.getInstance().getUnits())
        System.out.println(UnitFormat.getInstance().format(unit));

    for (javax.measure.unit.Unit<?> unit: javax.measure.unit.NonSI.getInstance().getUnits())
        System.out.println(UnitFormat.getInstance().format(unit));
但我在输出中没有看到“Reontgen”。有人有解决方案吗?

您可能想先试一试。它是JSScience 4和JSR 275的继任者,后者从未进入决赛,而JSR 363在2016年9月进入决赛

JSR363知道多单元系统,但让我们看看RI中内置的一个。您可以应用名为的库实用程序,并按如下方式应用它:

import tec.units.ri.unit.Units;
import tec.uom.lib.common.util.SystemOfUnitsReporter;

SystemOfUnitsReporter reporter = SystemOfUnitsReporter.of(Units.getInstance());
reporter.report();

这将把
SystemOfUnits
实现中的单元列表打印到控制台。

我回答了你的问题,因为我需要同样的东西。在对源代码进行了大量挖掘之后,我非常确定这实际上是不可能的:(我将通过SI和非SI类的反射来获得这些单元。如果你认为这是一个有效的解决方案,我可以在这里发表同样的结论。因为在现有的API中,我们还需要做一些其他的事情,我想我们只是要撕毁并建立我们自己的API。
import tec.units.ri.unit.Units;
import tec.uom.lib.common.util.SystemOfUnitsReporter;

SystemOfUnitsReporter reporter = SystemOfUnitsReporter.of(Units.getInstance());
reporter.report();