简体   繁体   English

如何比较两个 JSR-363 Quantity 对象?

[英]How to compare two JSR-363 Quantity objects?

I'm trying to use JSR-363 Quantity to manage some quantities in my application.我正在尝试使用 JSR-363 Quantity 来管理我的应用程序中的一些数量。 I have some code similar to the following that I would like to convert to use the Quantity class.我有一些类似于以下的代码,我想将其转换为使用数量 class。

Double volume1 = 14d;
Double volume2 = 18d;

Assert.isTrue(volume1 < volume2);

Using Quantity, I'm trying to find a way to compare two volumes, but there doesn't seem to be anything in the API that's equivalent to the simple comparison above!使用 Quantity,我试图找到一种方法来比较两个卷,但 API 中似乎没有任何内容相当于上面的简单比较!

Quantity<Volume> volume1 = Quantities.getQuantity(14d, Units.LITRE);
Quantity<Volume> volume2 = Quantities.getQuantity(18d, Units.LITRE);

Assert.isTrue(volume1 < volume2); <--- operator < doesn't exist

What am I missing?我错过了什么?

< only works with primitive number types (and boxed equivalents). <仅适用于原始数字类型(和盒装等效项)。 You cannot use it with objects.您不能将它与对象一起使用。

Use volume1.substract(volume2).getValue().doubleValue() < 0 instead.使用volume1.substract(volume2).getValue().doubleValue() < 0代替。

It turns out that there's a newer specification for units of measurements and a reference implementation that provides ComparableQuantity class that implements java.lang.Comparable .事实证明,有一个更新的测量单位规范和一个提供ComparableQuantity class 的参考实现,它实现了java.lang.Comparable

Then newer standard is JSR-385 and its reference implementation is Indriya .然后更新的标准是JSR-385 ,它的参考实现是Indriya

With this one can do:有了这个可以做到:

ComparableQuantity<Volume> volume1 = Quantities.getQuantity(14d, Units.LITRE);
ComparableQuantity<Volume> volume2 = Quantities.getQuantity(18d, Units.LITRE);

Assert.isTrue(volume1.isGreaterThan(volume2));
Assert.isTrue(volume2.isLessThan(volume1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM