First I will try to explain the idea behind this code. I have a bunch of classes (Processors) that can process a certain type of other classes (Processables). I have a List of Processors to execute them in a certain order. I have a Map that will retrieve me the data to process (Processables) for a certain Processor. It looks somehow like this.
public abstract class AbstractProcessable {
...
}
public class DummyProcessable extends AbstractProcessable {
...
}
public abstract class AbstractProcessor<T extends AbstractProcessable> {
public abstract void process(List<T> listOfProcessables);
}
public class DummyProcessor extends AbstractProcessor<DummyProcessable> {
@Override
public void process(List<DummyProcessable> listToProcess) {
...
}
}
This seems to work fine so far. There are no compilation errors. But now I have a class like the following:
public class RandomClass {
private List<AbstractProcessor<? extends AbstractProcessable>> processors;
private Map<Class<? extends AbstractProcessor>, List<? extends AbstractProcessable>> data;
public RandomClass() {
processors = new ArrayList<>();
processors.add(new DummyProcessor());
data = new HashMap<>();
data.put(DummyProcessor.class, new ArrayList<DummyProcessable>());
processAll();
}
private void processAll() {
for (AbstractProcessor<? extends AbstractProcessable> processor : processors) {
List<? extends AbstractProcessable> dataToProcess;
dataToProcess = data.get(processor);
processor.process(dataToProcess); // compile error
}
}
}
Compile error:
The method process(List<capture#4-of ? extends AbstractProcessable>) in the type AbstractProcessor<capture#4-of ? extends AbstractProcessable> is not applicable for the arguments (List<capture#5-of ? extends AbstractProcessable>)
I know it might be a bit difficult to read, but I tried to simplify it as much as possible. I'm also not that good with generics so maybe I used some wildcards wrong? Can anyone help me to solve that problem?
Thanks a lot in advance!