Are there any Java libraries dealing with units of measurement except for JSR 275 (rejected and abandoned) and Units of Measure API (which doesn't seem to have any production-quality implementations)?
Java units of measurement libraries other than JSR-275 and Units of Measure API
1k views Asked by Alexey Romanov At
3
There are 3 answers
1
On
https://github.com/unitsofmeasurement/uom-se from JSR 363
https://mvnrepository.com/artifact/org.unitsofmeasurement/unit-api/0.6.2-RC1
Hopefully your problem got solved approx. 4 yrs ago!
1
On
I have created an opensource Unitility for working with physical quantities and units of measurements including easy conversion between them. Example of usage:
// Creating temperature instance of specified units
Temperature temperature = Temperature.ofCelsius(20.5); // {20.50 °C}
// Getting converted value for calculations
double valueInCelsius = temperature.getInCelsius(); // 20.50 °C
double valueInFahrenheits = temperature.getInFahrenheits(); // 68.90 °F
// Checking property current unit, value, and value in base unit
TemperatureUnits unit = temperature.getUnit(); // CELSIUS
TemperatureUnits baseUnit = unit.getBaseUnit(); // KELVIN
// Changing property unit and converting back to base unit
Temperature tempInFahrenheits = temperature.toUnit(TemperatureUnits.FAHRENHEIT);
Temperature tempInKelvins = temperature.toBaseUnit();
Additionally, logical and basic math operations are supported within quantities of the same type:
// Logic operation example:
Temperature smallerTemp = Temperature.ofCelsius(-20.0);
Temperature greaterTemp = Temperature.ofCelsius(0.0);
smallerTemp.isLowerThan(greaterTemp);
greaterTemp.isGreaterThan(smallerTemp);
// Math operation example:
Temperature sourceTemperature = Temperature.ofCelsius(20);
Temperature temperatureToAdd = Temperature.ofKelvins(293.15);
Temperature actualTemperature = sourceTemperature.add(temperatureToAdd); // {40°C}
Maybe this will be helpful if you are looking for something simple. It supports a selection of most typical physical quantities and its units for SI and IMPERIAL units. If you need any specific unit or quantity to be added - just let me know.
Edit: 2024.01.16
- it now includes modules for Spring and Quarkus support to preconfigure object mappers for easier use in web applications
- maven repository: https://mvnrepository.com/artifact/com.synerset/unitility-core
I have written a units library that does not use static typesetting (as in many practical applications I encountered this would have been more cumbersome that I would like such a library to be). It is designed to handle string based units as well as sharper defined units. Some of the supported features include:
conversions of values, e.g.:
would both return
3000
.simplification of string based units, e.g.:
would return
"J^2 T"
.finding the names of a unit in a specific context, e.g.:
would return a navigable set containing
("luminous exposure")
.supports SI units, binary units, imperial units, US customary units, atomic units, planck units and many more. The user can also easily define own units.
fully supports arbitrary logarithmic units, e.g.
supports SI prefixes, binary prefixes and allows the user to easily implement own prefixes
Can handle unknown units if not relevant, e.g.:
would return
3e6
, as the unknown unitthis_is_not_a_unit
is the same on both sides of the conversion.for performance critical parts of the code one can obtain the conversion factor (if the conversion is purely multiplicative), e.g.:
will return
1e-3
.Allows to check for equivalence, e.g.
will return false, as
1min
is not the same as1s
. On the other hand, checking for convertibilitywill return
true
.tightly integrated in the coordinates library (as of Java 16 this library still requires preview-features, but as of Java 17 it will production ready)
The constants are implemented via a
Constant
interface that supports e.g.:definition of own constants, e.g.
chaining commands, e.g.
(simple) uncertainty propagation
the
Constant
interface provides default implementations for theTexable
interface from the jatex module, such that a constant can easily return proper LaTeX code.properly documented implementations for most of the physics constants as defined by NIST, as well as some mathematical constants.