Unit of measurement API in Java?
Asked Answered
T

4

8

JSR-275 has been rejected, the Units of Measurement API for Java project is a set of interfaces, but haven't found an open source implementation.

On this post: Which jsr-275 units implementation should be used? the project owner mentions the implementation was going to be ready by the end of last year on JScience, but didn't find anything there to convert between weight or length units and when I looked for JScience on https://maven.java.net/, I found it, but the JAR wasn't even in the directory https://maven.java.net/content/repositories/snapshots/org/jscience/jscience/5.0-SNAPSHOT/, so I had to get it from somewhere else.

Has this project been left behind? And is there currently an implementation for conversion of Units of measurement in Java and even perhaps a Maven repo?

Tod answered 24/6, 2011 at 23:16 Comment(1)
anyone have an update on this? I can't seem to find the 5.0-SNAPSHOT in a public repo.Woollen
N
3

As far as I know, JScience is very much alive. The project is currently being migrated to Java.net, and the migration is not complete. That is most likely the reason why you are unable to see snapshot JARs for 5.0. In fact, the most recent snapshot was prepared only after resolution of a particular configuration problem reported in the Java.net JIRA. May be you ought to wait a few days, or probably send a mail to the project administrator on what JScience POM must be used in the interim.

Nertie answered 24/6, 2011 at 23:56 Comment(0)
H
6

Unit-API (unitsofmeasurement.org) is the successor to JSR-275.

The most active implementation at the moment is Eclipse UOMo

Haveman answered 23/1, 2013 at 21:15 Comment(4)
I know that I'm almost 2 years late to the party at this point but; is there any way to use UOMo from a static jar file? Or do you have to use the Eclipse plugin framework as the tutorial shows? It looks perfect for our use, but I'm having a real hard time of integrating this into our current project. I also have a question posted about it here:#26870568Wallas
Andy, Thanks for your interest. Given a "new party" just started last April with JSR 363 approved and looking at bigger support thanks to a general IoT trend, you might want to check out JSR 363 and related projects on unitsofmeasurement.github.ioHaveman
Link is now broken. I think the replacement site is unitsofmeasurement.github.ioActuary
Yes you're correct, this was originally from 2013, about a year later even before unitsofmeasurement.org was discontinued JSR 363 and with it unitsofmeasurement.github.io (or aliases like uom.technology) became available as the new home to the project.Haveman
N
3

As far as I know, JScience is very much alive. The project is currently being migrated to Java.net, and the migration is not complete. That is most likely the reason why you are unable to see snapshot JARs for 5.0. In fact, the most recent snapshot was prepared only after resolution of a particular configuration problem reported in the Java.net JIRA. May be you ought to wait a few days, or probably send a mail to the project administrator on what JScience POM must be used in the interim.

Nertie answered 24/6, 2011 at 23:56 Comment(0)
A
0

Update on this JSR-363 Units of Measurement API was completed in 2016 and provides a fairly complete UoM API. This has now been superseded by JSR-385 Units of Measurement API 2.0. You can find the code behind the API and implementation on Github here https://github.com/unitsofmeasurement.

Here is a simple conversion example using UoM API 2.0

import tech.units.indriya.quantity.Quantities;
import javax.measure.Quantity;
import javax.measure.quantity.Length;

import static javax.measure.MetricPrefix.CENTI;
import static tech.units.indriya.unit.Units.METRE;

class SimpleUnitExample {
    public static void main(String[] args) {
        Quantity<Length> lengthQuantity = Quantities.getQuantity(25, METRE);
        System.out.println(lengthQuantity.to(CENTI(METRE)));
    }
}

With a dependency on tech.units:indriya:2.0.4 will print 2500 cm.

You can find lots more examples in this repo uom-demos.

Actuary answered 9/6, 2021 at 19:42 Comment(0)
W
0

If you look for something simple, you can check out my project. 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
TemperatureUnit unit = temperature.getUnit();                  // CELSIUS
TemperatureUnit 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);         // true                       
greaterTemp.isGreaterThan(smallerTemp);       // true

// 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.
  • I have provided 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 hope this helps.

Willem answered 16/1 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.