unitsofmeasurement/uom-systems

Maven: dependency conflict lead to invalid computation

dr29bart opened this issue · 5 comments

maven project A pom.xml (indriya:2.0.3 and systems-common:2.0.2 are included ):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>unitcheck</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>tech.units</groupId>
            <artifactId>indriya</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>systems.uom</groupId>
            <artifactId>systems-common</artifactId>
            <version>2.0.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

project B pom.xml (only systems-common:2.0.2 is included ):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>unitcheck</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>systems.uom</groupId>
            <artifactId>systems-common</artifactId>
            <version>2.0.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

There is a main class:

import javax.measure.quantity.Length;
import javax.measure.quantity.Speed;
import javax.measure.quantity.Time;

import tech.units.indriya.ComparableQuantity;
import tech.units.indriya.quantity.Quantities;

import static systems.uom.common.USCustomary.MILE;
import static systems.uom.common.USCustomary.MILE_PER_HOUR;
import static tech.units.indriya.unit.Units.SECOND;

public class UnitCheck {

    public static void main(String[] args) {
        ComparableQuantity<Time> oneSecond = Quantities.getQuantity(1.0, SECOND);

        double distance = Quantities.getQuantity(4, MILE_PER_HOUR)
            .to(MILE.divide(SECOND).asType(Speed.class))
            .multiply(oneSecond)
            .asType(Length.class).getValue().doubleValue();
        System.out.println(distance);
    }
}

Actual result:
Output for the project A is: 1.78816
Output for the project B is: 0.0011111111111111111

Expected result
Both projects outputs 0.0011111111111111111

Indriya 2.0.3 does convert the calculation results above to System Units, so the result in your example is in units of Meter.
So you need to explicitely convert to Mile here ...

double distance = Quantities.getQuantity(4, USCustomary.MILE_PER_HOUR)
          .to(USCustomary.MILE.divide(Units.SECOND).asType(Speed.class))
          .multiply(oneSecond)
          .asType(Length.class)
          .to(USCustomary.MILE) // <-- explicit conversion to MILE
          .getValue()
          .doubleValue();

Thanks for example.
The was unexpected part for me, because with it was working fine with indriya:2.0.2 and systems-common:2.0.1.
Closing...

keilw commented

So it works as designed. Thanks @andi-huber for the quick response.
I guess from the next Indriya release we might also like to provide some release notes or pointers to the Wiki etc. in such a case ;-)

I did change behavior with Indriya 2.0.3 for multiplication. So that the above use-case seemingly produces a surprising result. However, I think its strongly recommended to do calculations without assumptions about the resulting unit. An explicit conversion to the desired result type (unit) is highly recommended any way.

keilw commented

I will try to include this into uom-demos 2.0.1 to be released soon. Hope that's OK with everyone? We didn't do that outside systems-ucum so far, but we'd be more than happy to also add a "contributors" file to certain parts of uom-demos.