简体   繁体   English

数量计量单位过滤器

[英]unit-of-measurement filter by quantity

we are using the great unit-of-measurement framework to manage units dynamically. 我们正在使用出色的计量单位框架来动态管理单位。 I have the requirement to filter a list of units by quantity. 我需要按数量过滤单位列表。 Eg display all mass (tonne, kg ....). 例如,显示所有质量(吨,千克....)。 The list results in an generic capture list (which is not ideal - i know). 该列表会生成一个通用的捕获列表(不理想-我知道)。 The generic information does not exist at runtime (only at compile time). 通用信息在运行时不存在(仅在编译时)。 However for the unit interface an implementation exists to check the compatibility exists: 但是对于单元接口,存在一个检查兼容性的实现:

  • boolean isCompatible(Unit that); boolean isCompatible(Unit that);

     @Test public void testCompatible_ByUnit(){ Unit<Mass> kilogram = Units.KILOGRAM; Unit<Mass> tonne = NonSI.TONNE; assertTrue(kilogram.isCompatible(tonne)); } 

Is there any interface to check the compatiblity by quantity ? 有没有按数量检查兼容性的接口?

Non working example with quantity Mass . 数量为Mass的非工作示例。

    @Test
    public void testCompatible_FilterByQuantityAndCapture(){
        Unit<?> kilogram = Units.KILOGRAM;
        // wish: interface does not exist
        assertTrue(kilogram.isCompatible(Mass.class));
    }

This is a common problem of the Java language because Java so far does not offer Reified Generics . 这是Java语言的常见问题,因为到目前为止Java尚未提供Reified Generics

I'm not aware that this feature made it into Java SE 9, so we likely won't see it before Java 10 or later. 我不知道此功能已被纳入Java SE 9中,因此我们很可能在Java 10或更高版本之前看不到它。

<Q extends Quantity<Q>> Unit<Q> getUnit(Class<Q> quantityType);

returns the default (or system) unit for a given Quantity class. 返回给定Quantity类的默认(或系统)单位。 And although Dimension is more of an "umbrella" to quantities, you may also get a list of all units under a particular dimension. 尽管Dimension更像是数量上的“伞”,但您也可能会获得特定维度下所有单位的列表。

Set<? extends Unit<?>> getUnits(Dimension dimension);

In a future version of the API/SPI SystemOfUnits may also offer something similar for a Quantity class, but it is quite possible this kind of additional method won't make much sense before the underlying Java platform offers reified generics or some other way of better type information than it does toeday. 在API / SPI的未来版本中, SystemOfUnits可能还会为“ Quantity类提供类似的功能,但是在基础Java平台提供经过改进的泛型或其他更好的方法之前,这种附加方法很可能就没有多大意义了。输入的信息比实际输入的信息多。

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

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