'How to create custom units and quantities with Javax measure API

As there is no clear tutorial or documentation on how to actually make use of the Units of Measurement API, I've been having some trouble in defining my own units and quantities.

For my case, just as we can define a mass quantity like that:

Quantity<Mass> m1 = Quantities.getQuantity(1.5f, Units.KILOGRAM);

I want to do the same with my own one, which let's call Linear Density. I.e. it should be a Mass per unit length kind of quantity, having for e.g. kg/m as a unit.

How could I easily do that so that I could for e.g. multiply such quantities with Lenth quantities to get a Mass, etc.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source