gpt4 book ai didi

java - 泛型 hell - 如何将 joda.DateTime 传递给 Hamcrest Matcher.greaterThan?

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:54 28 4
gpt4 key购买 nike

JodaTime 有

public final class DateTime extends BaseDateTime {...}

一直到

public interface ReadableInstant extends Comparable<ReadableInstant>

哈姆克雷斯特有

public static <T extends java.lang.Comparable<T>> org.hamcrest.Matcher<? super T>
greaterThan(T value) {...}

如果我尝试

greaterThan(new DateTime());

然后我得到一个编译错误(Eclipse 给出了大部分线索)

The generic method greaterThan(T) of type Matchers is not applicable for the arguments (DateTime). The inferred type DateTime is not a valid substitute for the bounded parameter >

我认为 greaterThan 的签名实际上应该是

public static <T extends java.lang.Comparable<? super T>> org.hamcrest.Matcher<? super T>     
greaterThan(T value)

?除了转换为原始 Comparable 之外,有没有办法将它们组合在一起?

最佳答案

是的,在我看来这是一个更好的签名。

您是否尝试过明确指定比较类型?

Matchers.<ReadableInstant>greaterThan(new DateTime());

不幸的是,我认为您不能使用静态导入并指定类型参数来调用它 - 但这可能不会太困难。

当然,另一种方法是转换参数:

greaterThan((ReadableInstant) new DateTime());

我手边没有 Hamcrest,但上面的代码对我来说效果很好,使用你给我的签名,在测试类型中。

关于java - 泛型 hell - 如何将 joda.DateTime 传递给 Hamcrest Matcher.greaterThan?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7970163/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com