samedi 5 septembre 2020

mapper interface to map request to JobParameters

I have a component that runs jobs:

currently the job parameter being build is fixed but different feeds should provide their own implementation of how a request should be mapped to JobParameters so I have come up with a generic Mapper interface and one RequestMapper interface that should map MyRequest to JobParameters

@Component
public class MyJobRunner {

    private JobLauncher jobLauncher;
    private JobExplorer jobExplorer;
    private JobLocator jobLocator;
    private RequestMapper requestMapper;

    @Autowired
    public MyJobRunner(JobLocator jobLocator,
                        JobExplorer jobExplorer,
                         JobLauncher jobLauncher,
                         RequestMapper requestMapper) {
        this.jobLocator = jobLocator;
        this.jobExplorer = jobExplorer;
        this.jobLauncher = jobLauncher;
        this.requestMapper = requestMapper;
    }

    public MyResponse run(final MyRequest request, final String id) {
        JobParameters parameters = getJobParameters(request, id);
    }


    private JobParameters getJobParameters(MyRequest request, String id) {
        //current
        JobParametersBuilder parameters = new JobParametersBuilder();
        parameters.addString("name", request.getValue(NAME));
        parameters.addString("date", request.getValue(DATE));
        parameters.addString(ID.getKeyName(), id);
        return parameters.toJobParameters();

       //target is replace the above with requestMapper.map(...) to transform request to JobParameters through the interface
    }

}


public interface Mapper<F, T> {
    T map(F from);
}


public interface RequestMapper extends Mapper {
}

problem is I have id field that should also be in the JobParameters but it is not in the MyRequest object. In that case How should I design the RequestMapper interface and then the implementation of RequestMapper? I am unsure if I can use HashMap as generic type:

public interface RequestMapper<Map<String, MyRequest>> extends Mapper {
}

where the key is id and value is the request object. But perhaps there is a cleaner way of achieving this.

Aucun commentaire:

Enregistrer un commentaire