7

I am using the reference implementation of JSR 363: Units of Measurement API from maven (tec.units:unit-ri).

Now I have to add a few units like teaspoon, fluid ounce and so on.

Im extending the Units class to add a new unit like this:

public static final Unit<Volume> TEASPOON = addUnit(new TransformedUnit<Volume>("tsp", CUBIC_METRE, new MultiplyConverter(0.000005)));

This seems to work for converting but "tsp" is not parsing, so how do I add it to the parser?

And Im having trouble adding Fahrenheit for example: T(°F) = T(K) × 9/5 - 459.67

How can I do this with the converters, or do I have to extend UnitConverter and create my own?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
TeKo
  • 465
  • 1
  • 5
  • 17

2 Answers2

1

Though you may define your own unit, common units like TEASPOON or Fluid Ounce are already available in extension modules for JSR 363 like https://github.com/unitsofmeasurement/uom-systems

https://github.com/unitsofmeasurement/uom-systems/tree/master/common for the RI-based library. Fahrenheit is also there.

All these systems or the "full SI" system are available on the JCenter public repo. We plan to also sync it to MavenCentral soon, but you can use it the same way from JCenter if you add its repository definition to your Maven (or Gradle, etc.) build files.

Werner Keil
  • 592
  • 5
  • 12
  • I've also tried this exercise by defining a custom unit PARSEC (I know it's already there). I'm struggling to have it printed as Parsec (pc). I always get it printed as meter. Any help how to achieve this and have it convertable to metre, kilometere and so on? – Alex_M Aug 23 '16 at 09:13
  • With the next update, PARSEC is also going to be in [Unicode CLDR][1], there both on RI and Java SE8+ using `SimpleUnitformat`. [1]: https://github.com/unitsofmeasurement/uom-systems/tree/master/unicode – Werner Keil Aug 27 '16 at 13:37
0

Thanks for mentioning. If you're using SimpleUnitFormat (leaving Locale aside for now) you may be missing a label() expression in the right places.

Do you have a GitHub account, then please file it under https://github.com/unitsofmeasurement/uom-systems/issues if you can and we'll address the issue as soon as possible. Or file a PR.

Werner Keil
  • 592
  • 5
  • 12
  • So far PARSEC exists at least in the UCUM unit system, which has the specialty of dedicated `UnitFormat` implementations, see [UCUMDemoSE](https://github.com/unitsofmeasurement/uom-demos/blob/master/console/systems/ucum/src/main/java/tec/uom/demo/systems/ucum/UCUMDemoSE.java). Calling toString() on one of those falls back to the implementation via `SimpleUnitformat`. Which currently isn't aware of all the UCUM strings. It could be done via Label, but probably caused some redundancy or "reflection" magic. – Werner Keil Aug 25 '16 at 22:22