I cannot understand why ClassifierCompositeItemWriter in Spring Batch use contravariance for classify the items.
In the class the classifier property is defined with an output type of ItemWriter<? super T>:
public class ClassifierCompositeItemWriter<T> implements ItemWriter<T> {
private Classifier<T, ItemWriter<? super T>> classifier = new ClassifierSupport<>(null);
For example I want to classify 2 type of items : PetToCreate extends PetAction{}
and PetToUpdate extends PetAction{}
and delegate to the corresponding writers: CreatePetWriter implements ItemWriter<PetToCreate>
or UpdatePetWriter implements ItemWriter<PetToUpdate>
.
So I use a SubclassClassifier:
var typeMap = Map.of(
PetToCreate.class, createPetWriter,
PetToUpdate.class, updatePetWriter
);
SubclassClassifier<PetAction, ItemWriter<? extends PetAction>> petActionItemWriterSubclassClassifier = new SubclassClassifier<>(typeMap, null);
But I can’t understand why cannot use this classifier for my ClassifierCompositeItemWriter<PetAction>
due to the output type of the classifier property that is defined with a type ? super T
.
Can someone explain to me the motivation behind this choice, shouldn’t it be possible to achieve it?
Thank in advance.