Expected Behavior
The ClassifierCompositeItemWriter should use covariance for classify the items. In the class the classifier property should be defined with an output type of ItemWriter<? extends T>. This way I can use a SubclassClassifier to classify the items.
Current Behavior
ClassifierCompositeItemWriter 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);
Context
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 the type checking fails:
Can someone explain to me the motivation behind this choice, shouldn't it be possible to achieve it?
Expected Behavior The ClassifierCompositeItemWriter should use covariance for classify the items. In the class the classifier property should be defined with an output type of
ItemWriter<? extends T>
. This way I can use a SubclassClassifier to classify the items.Current Behavior
ClassifierCompositeItemWriter use contravariance for classify the items. In the class the classifier property is defined with an output type of
ItemWriter<? super T>
:Context
I want to classify 2 type of items :
PetToCreate extends PetAction{}
andPetToUpdate extends PetAction{}
and delegate to the corresponding writers:CreatePetWriter implements ItemWriter<PetToCreate>
orUpdatePetWriter implements ItemWriter<PetToUpdate>
.So I use a SubclassClassifier:
But the type checking fails:
Can someone explain to me the motivation behind this choice, shouldn't it be possible to achieve it?
I also opened a question on StackOverflow: https://stackoverflow.com/questions/78459916/why-classifiercompositeitemwriter-use-contravariance-for-classify-the-items