2

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?

Andy
  • 3,228
  • 8
  • 40
  • 65
  • 1
    I ended up at your question because I need the same thing. After some extensive digging in the source code, I'm pretty sure that it's actually not possible :(. I will get the units by reflection out of the SI and NonSI classes. If you consider this a valid solution I can post it here – David ten Hove May 22 '15 at 13:38
  • I came to the same conclusion. Since there's a few other things we need to do that cannot be done in the existing API, I think we're just going to rip and build our own. – Andy Jun 08 '15 at 09:33

1 Answers1

2

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.

Werner Keil
  • 592
  • 5
  • 12