简体   繁体   English

Java泛型导致问题

[英]Java Generics Causing Problems

My IDE is complaining that "NCM_Callable cannot be converted to Callable<ReturnInterface<? extends Object>> on this line this.ph.submitCallable(new NCM_Callable(this, new DeviceGroup(this.deviceGroupNames.get(i)), ph)); In the "fetchDevices()" method 我的IDE抱怨“NCM_Callable无法在此行上转换为Callable<ReturnInterface<? extends Object>> this.ph.submitCallable(new NCM_Callable(this, new DeviceGroup(this.deviceGroupNames.get(i)), ph)); In the "fetchDevices()" method

I just want to be able to pass Callables to my ecs that returns a ReturnInterface containing any type of object. 我只是希望能够将Callables传递给我的ecs,它返回一个包含任何类型对象的ReturnInterface。

I suspect there is something wrong with my usage of <> generic definitions, but I can't seem to figure out what it is. 我怀疑使用<>泛型定义有问题,但我似乎无法弄清楚它是什么。 Any help would be appreciated. 任何帮助,将不胜感激。

  @Override
public void fetchDevices() throws Exception {
    System.out.println("[NCM_Fetcher]fetchingDevices()");
    for (int i = 0; i < this.deviceGroupNames.size(); i++) {
        System.out.println("[NCM_Fetcher]fetchingDevices().submitting DeviceGroup Callable " + i+ " of "+this.deviceGroupNames.size());
        this.ph.submitCallable(new NCM_Callable(this, new DeviceGroup(this.deviceGroupNames.get(i)), ph));
    }
    this.ph.execute();//See progressBarhelper below
}

ProgressBarHelper: I have a strange error at "ecs.submit()". ProgressBarHelper:我在“ecs.submit()”中遇到一个奇怪的错误。 From what I've read, it seems like I may need a helper method? 从我的阅读,似乎我可能需要一个辅助方法? How do I fix? 我该如何解决?

public class ProgressBarHelper extends SwingWorker<Void, ReturnInterface> implements ActionListener {
ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

protected CompletionService<ReturnInterface<?>> ecs;

public final void submitCallable(Callable<? extends ReturnInterface<?>> c) {
    //create a map for this future
    ecs.submit(c);
    this.callables.add(c);//Error here is Callable<CAP#1 cannot be converted to Callable<ReturnInterface<?>>
    System.out.println("[ProgressBarHelper]submitted");

}
}

And Finally, the NCM_Callable class with its Generics. 最后,NCM_Callable类及其泛型。

public class NCM_Callable implements Callable<ReturnInterface<ResultSet>>, ReturnInterface<ResultSet> {

As far as I could check, the problematic line is ecs.submit : 据我ecs.submit ,有问题的行是ecs.submit

public final void submitCallable(Callable<? extends ReturnInterface<?>> c) {
    // create a map for this future
    ecs.submit(c); // Error here is Callable<CAP#1 cannot be converted to Callable<ReturnInterface<?>>
   this.callables.add(c); // this is fine on my machine
}

The problem is that java generics is invariant . 问题是java泛型是不变的 There's a workaround for this: you can replace the wildcard with a named, bound type : 有一个解决方法:您可以使用命名的绑定类型替换通配符:

public final <T extends ReturnInterface<?>> void // this is how you name and bind the type
   submitCallable(Callable<T> c) { // here you are referring to it
    // create a map for this future
    ecs.submit((Callable<ReturnInterface<?>>) c); // here you need to cast. 
    ...
}

The casting is okay in runtime due to type erasure . 由于类型擦除,在运行时铸件是可以的。 But then be very careful on what you do with those Callables . 但是要对那些Callables做什么要非常小心。

Finally, this is how you can invoke this function: 最后,这是你可以调用这个函数的方法:

private static class ReturnInterfaceImpl implements ReturnInterface<String>  {

};

public void foo() {
    Callable<ReturnInterfaceImpl> c = new Callable<ReturnInterfaceImpl>() {
        @Override
        public ReturnInterfaceImpl call() throws Exception {
            return null;
        }
    };
    submitCallable(c);
}

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

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