Andy
Andy

Reputation: 3522

Printing out all defined units in JScience

We have a system where a user can provide data of any unit. The data is stored and potentially converted into any other unit of the same dimension. The is based on the JScience api.

I'd like to produce a list of all supported units, along with all aliases they have. I can't seem to find a way of doing this. Currently I'm just doing this:

    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));

First off, this will only produce a list of the labels, I can't find a way to get at the aliases at all.

Secondly, it doesn't even seem to contain all of the units. If I look in the decompiled file javax.measure.unit.UnitFormat, which appears to be where all the labels are attached; I see the line:

DEFAULT.label(NonSI.ROENTGEN, "Roentgen");

But I do not see "Reontgen" in the output. Does anyone have a solution?

Upvotes: 2

Views: 555

Answers (1)

Werner Keil
Werner Keil

Reputation: 642

You might want to give JSR 363 a try first. It's the successor to JScience 4 and JSR 275 which never went final while JSR 363 did in September 2016.

JSR 363 knows multiple Unit Systems, but let's just take the one built into the RI. You can apply a library utility called SystemOfUnitsReporter and apply it as follows:

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

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

This will print out a list of units in the SystemOfUnits implementation to the console.

Upvotes: 2

Related Questions