Results 1 to 2 of 2
  1. #1
    Jack2011 is offline Member
    Join Date
    Sep 2011
    Posts
    8
    Rep Power
    0

    Default 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());

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

    Java 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
    Last edited by Jack2011; 10-25-2011 at 06:54 PM.

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default 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.
    The Java Tutorial. Read it.

Similar Threads

  1. Calculator Functions
    By youngdemetre in forum New To Java
    Replies: 5
    Last Post: 05-01-2011, 02:53 PM
  2. Replies: 7
    Last Post: 01-17-2011, 08:02 AM
  3. JSP Functions
    By sysout in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-07-2009, 05:10 AM
  4. 2 functions
    By baze7 in forum New To Java
    Replies: 3
    Last Post: 08-14-2009, 05:41 AM
  5. Functions in jsp
    By samson in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 03-25-2009, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •