[英]Java error: incompatible types: no instance(s) of type variable(s) T exist so that Optional<T> conforms to Iterable<AvailableLicence>
Normally I'm a C# developer, but I'm trying to write some code in Java (first time in years) that uses generics to create a 'Retry' method that allows me to use a lambda expression to retry any block of code, basically. Normally I'm a C# developer, but I'm trying to write some code in Java (first time in years) that uses generics to create a 'Retry' method that allows me to use a lambda expression to retry any block of code,基本上。 Here is my retry method:
这是我的重试方法:
public static <T> Optional<T> runWithRetry(final Supplier<T> t, int retryAttempts, long retryDelayInMilliseconds) throws InterruptedException {
for(int retry = retryAttempts; retry >= 0; retry--) {
try{
return Optional.of(t.get());
}
catch(Exception e) {
if(retry == 0) {
throw e;
} else if (retryDelayInMilliseconds > 0) {
Thread.sleep(retryDelayInMilliseconds);
}
}
}
return Optional.empty();
}
And I'm attempting to call this method like so:我试图这样调用这个方法:
Iterable<AvailableLicence> licences = Helpers.runWithRetry(() -> {
return licensor.findAvailableLicences(licenseOptions);
}, 3, 5000);
but the Java compiler is throwing this error:但 Java 编译器抛出此错误:
error: incompatible types: no instance(s) of type variable(s) T exist so that Optional<T> conforms to Iterable<AvailableLicence>
Iterable<AvailableLicence> licences = Helpers.runWithRetry(() -> {
^
where T is a type-variable:
T extends Object declared in method <T>runWithRetry(Supplier<T>,int,long)
I think what this error is trying to tell me is that I am not specifying the type for <T>
somewhere but I'm not entirely sure.我认为这个错误试图告诉我的是我没有在某处指定
<T>
的类型,但我并不完全确定。 I'm also combining some Java things like Optional
and Supplier
that I'm not sure if they could be causing the issue or not, despite having read up on how they work.我还结合了一些 Java 之类的东西
Optional
和Supplier
,尽管已经阅读了它们的工作原理,但我不确定它们是否会导致问题。 Can anyone tell what I'm doing wrong here?谁能告诉我在这里做错了什么? I feel like it's a simple syntax issue I'm missing here.
我觉得这是我在这里遗漏的一个简单的语法问题。
I would spare the caller of runWithRetry()
having to handle the checked InterruptedException
.我会免除
runWithRetry()
的调用者必须处理检查的InterruptedException
。 For example like this …比如像这样……
public static <T> Optional<T> runWithRetry(final Supplier<T> t, int retryAttempts, long retryDelayInMilliseconds) {
return handleSleep(t, retryAttempts, retryDelayInMilliseconds);
}
private static <T> Optional<T> handleSleep(final Supplier<T> t, int retryAttempts, long retryDelayInMilliseconds){
Optional<T> retVal = Optional.empty();
for(int retry = retryAttempts; retry >= 0; retry--) {
try{
retVal = Optional.of(t.get());
}
catch(Exception e) {
if(retry == 0) {
throw e;
} else if (retryDelayInMilliseconds > 0) {
try{
Thread.sleep(retryDelayInMilliseconds);
} catch(InterruptedException ie){ /*TODO: Handle or log warning */ }
}
}
}
return retVal;
}
See how I used that in my demo …看看我在演示中是如何使用它的……
...
Optional<Iterable<AvailableLicense>> licenses = Deduper.runWithRetry( ()->
{ return licensor.findAvailableLicenses(licenseOptions); }, 3, 5000 );
licenses.ifPresent(out::println);
...
Because I've implemented AvailableLicense.toString()
, my demo outputs…因为我已经实现了
AvailableLicense.toString()
,所以我的演示输出......
[AvailableLicense[ type: To Kill!]]
Optional - The Mother of all Bikesheds from @StuartMarks — one of the Oracle core devs that implemented Optional
is recommended viewing for everybody using Optional
.可选 - 来自@StuartMarks 的所有 Bikesheds 之母- 实现
Optional
的 Oracle 核心开发人员之一建议使用Optional
的每个人查看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.