spring-projects / spring-projects/spring-batch

Should ClassifierCompositeItemWriter use covariance (extends "keyword" instead "super") in the output type of classifier property?

Open
#4,592 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: feature
Dominant language
Java
Stars
3k
Forks
2.5k
Avg merge
6d 53m
Merged PRs (30d)
3

Description

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:
image

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ClassifierCompositeItemWriter classifier declaration and compare its generic type with SubclassClassifier and the PetAction example in the issue. Check whether the proposed covariance compiles for the two specialized writers without breaking existing ItemWriter usage; done means the intended type-checking scenario works and relevant tests cover it.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.