I have a list declared as below into which I intend to add objects.
List<RecursableAction<Recursable, AbstractContext>> actions = new ArrayList<>();
I have an IdGeneratorItemAction that looks like the below:
public class IdGeneratorItemAction implements RecursableAction<Item, IdGeneratorContext> {
private IdGeneratorContext context;
@Override
public void act(Item recursable) {
}
}
The interface RecursableAction looks like this:
public interface RecursableAction<R extends Recursable, C extends AbstractContext> {
void act(R recursable);
}
I try to create an instance of IdGeneratorItemAction and add it to the actions list as follows:
RecursableAction<Recursable, AbstractContext> action = new IdGeneratorItemAction();
actions.add(action);
When I attempt to do that, I get the below compilation error:
RecursableAction<Recursable, AbstractContext> action = new IdGeneratorItemAction();
^
Type mismatch: cannot convert from IdGeneratorItemAction to RecursableAction<Recursable,AbstractContext>
I tried to change the delcaration of actions to
List<RecursableAction<? extends Recursable, ? extends AbstractContext>> actions = new ArrayList<>();
But when I do that, I am no longer able to iterate on the actions.
for (RecursableAction<? extends Recursable, ? extends AbstractContext> action : actions) {
action.act(recursable);
^
The method act(capture#1-of ? extends Recursable) in the type RecursableAction<capture#1-of ? extends Recursable,capture#2-of ? extends AbstractContext> is not applicable for the arguments (Recursable)
}
How to solve this problem?
Without knowing the context it’s hard to advise. Clearly you can’t add an
IdGeneratorItemActionto aList<RecursableAction<Recursable, AbstractContext>>becauseIdGeneratorItemActionis aRecursableAction<Item, IdGeneratorContext>and these are not compatible (in the same way as you can’t assign aList<String>to a variable that expects aList<Object>). But likewise you can’t callact(recursable)on aRecursableAction<? extends Recursable, ? extends AbstractContext>because you don’t know what kind ofRecursableit expects.If you are in a context where you know all the actions are compatible then you’re OK, for example
but if not then I don’t think you can achieve what you want without a cast somewhere or other.