简体   繁体   English

Java 错误:不兼容的类型:不存在类型变量 T 的实例,因此可选<t>符合 Iterable<availablelicence></availablelicence></t>

[英]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 之类的东西OptionalSupplier ,尽管已经阅读了它们的工作原理,但我不确定它们是否会导致问题。 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每个人查看。

不兼容的类型:不存在类型变量 F、T 的实例,因此 java.util.Collection<t> 符合 java.util.Set <java.lang.long< div><div id="text_translate"><p> 我正在尝试将ComplexItem列表转换为它们对应的 ID Long列表。 但是即使在使用(Collection&lt;ComplexItem&gt;)对getCollection()调用进行类型转换后,也不会出现上述错误 go</p><pre> Set&lt;Long&gt; ids = Collections2.transform( getComplexItems(), new Function&lt;ComplexItem, Long&gt;() { @Override public Long apply(final ComplexItem item) { return item.id; } })); public List&lt;ComplexItem&gt; getComplexItems() {.............. }</pre> </div></java.lang.long<></t> - incompatible types: no instance(s) of type variable(s) F,T exist so that java.util.Collection<T> conforms to java.util.Set<java.lang.Long

暂无
暂无

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

相关问题 不兼容的类型:不存在类型变量 T 的实例,因此 Matcher<t> 符合 ArgumentMatcher<long></long></t> - incompatible types: no instance(s) of type variable(s) T exist so that Matcher<T> conforms to ArgumentMatcher<Long> Java错误:不兼容的类型:不存在类型变量R的实例,因此Stream <R> 符合布尔值 - Java Error: incompatible types: no instance(s) of type variable(s) R exist so that Stream<R> conforms to boolean 不兼容的类型:不存在类型变量 F、T 的实例,因此 java.util.Collection<t> 符合 java.util.Set <java.lang.long< div><div id="text_translate"><p> 我正在尝试将ComplexItem列表转换为它们对应的 ID Long列表。 但是即使在使用(Collection&lt;ComplexItem&gt;)对getCollection()调用进行类型转换后,也不会出现上述错误 go</p><pre> Set&lt;Long&gt; ids = Collections2.transform( getComplexItems(), new Function&lt;ComplexItem, Long&gt;() { @Override public Long apply(final ComplexItem item) { return item.id; } })); public List&lt;ComplexItem&gt; getComplexItems() {.............. }</pre> </div></java.lang.long<></t> - incompatible types: no instance(s) of type variable(s) F,T exist so that java.util.Collection<T> conforms to java.util.Set<java.lang.Long 不存在类型变量的实例,因此T符合注释 - No instance(s) of type variable(s) exist so that T conforms to Annotation 没有类型变量T的实例存在以便List <T> 符合Integer - No instance(s) of type variable(s) T exist so that List<T> conforms to Integer 原因:不存在类型变量 T 的实例,因此 void 符合 T - reason: no instance(s) of type variable(s) T exist so that void conforms to T 原因:不存在类型变量 T 的实例,因此 void 符合使用 mockito - reason: no instance(s) of type variable(s) T exist so that void conforms to using mockito 不存在类型变量T的实例,因此ID符合Comparable <? super T> - No instance of type variable(s) T exist so that ID conforms to Comparable<? super T> Java泛型不兼容的类型(不存在类型变量T的实例) - Java generics incompatible types (no instance(s) of type variable(s) T exist) 不存在类型变量U的实例,因此Optional <U>符合Response</u> - No instance(s) of type variable(s) U exist so that Optional<U> conforms to Response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM