How can you factorise two functions with loop in one ?
Hi,
I am working in 1.4 without generics. We dont support 1.5 yet.
I would to factorise some elements of these 2 functions.
This first fonction is not complete. I did not add all the lines like this one :
vEnvoiKoForm.setSomething(((JournalEnvoiKoVO) vJournalEnvoiKo.get(j)).getSomething());
Code:
public static List ListEnvoiKoVoToListAvisEnvoiKoForm(List vJournalEnvoiKo, List vEnvoiKo) {
ArrayList vListEnvoiKoForm = new ArrayList();
if (vJournalEnvoiKo != null) {
for (int j = 0; j < vJournalEnvoiKo.size(); j++) {
JournalEnvoiKOForm vEnvoiKoForm = new JournalEnvoiKOForm();
if (((JournalEnvoiKoVO) vJournalEnvoiKo.get(j)).getDateEnvoi() != null)
vEnvoiKoForm.setDate(((JournalEnvoiKoVO) vJournalEnvoiKo.get(j)).getDateEnvoi()); //)));
vEnvoiKoForm.setDestinataire((((JournalEnvoiKoVO) vJournalEnvoiKo.get(j)).getDestinataires()));
vEnvoiKoForm.setNip(((JournalEnvoiKoVO) vJournalEnvoiKo.get(j)).getNip());
....
vListEnvoiKoForm.add(vEnvoiKoForm);
}
}
return vListEnvoiKoForm;
}
}
}
The other function is similar but with different objects :
Code:
public static List listEnvoiAvisVoToListEnvoiAvisForm(List vJournalEnvoiAvis) {
ArrayList vListEnvoiAvisForm = new ArrayList();
if (vJournalEnvoiAvis != null) {
for (int j = 0; j < vJournalEnvoiAvis.size(); j++) {
EnvoiAvisForm vEnvoiAvisForm = new EnvoiAvisForm();
if (((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getDateEntreeHospit() != null)
vEnvoiAvisForm.setDateEntree(ChainesUtil.formateDate(((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getDateEntreeHospit()));
if (((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getDateEnvoiAvis() != null)
vEnvoiAvisForm.setDateEnvoiAvis(ChainesUtil.formateDate(((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getDateEnvoiAvis()));
if (((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getDateNaissancePatient() != null)
vEnvoiAvisForm.setDateNaissance(ChainesUtil.formateDate(((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getDateNaissancePatient()));
if (((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getDateSortieHospit() != null)
vEnvoiAvisForm.setDateSortie(ChainesUtil.formateDate(((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getDateSortieHospit()));
vEnvoiAvisForm.setNda(((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getNda());
vEnvoiAvisForm.setNip(((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getNip());
vEnvoiAvisForm.setLibelleMT(((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getLibelleMedecin());
vEnvoiAvisForm.setNomPatient(((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getNomPatient());
vEnvoiAvisForm.setPrenomPatient(((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getPrenomPatient());
vEnvoiAvisForm.setCodeService(((JournalEnvoiAvisVO) vJournalEnvoiAvis.get(j)).getCodeService());
vListEnvoiAvisForm.add(vEnvoiAvisForm);
}
}
return vListEnvoiAvisForm;
}
I think this functions are ugly. There is probably a way to factorise some element and create a superclass from these 2 functions. But i am not sure how to do it yet.
PS : there is at least 8 other functions similar to these functions in the code. It would be nice to make factorisation with all these functions.
Thanks
Re: How can you factorise two functions with loop in one ?
Sometimes brute force ends up being the most elegant solution. In other words, I think you will end up with a confusing mess if you try to combine those functions. I can't really see what they are doing, but they seem to have only passing similarities.