0

I could not quickly find a solution, I decided to do it myself and share it.
Spring, fill setters of object from getters other object

Standart Java-based code:

public class Mediator{

    public static Contact getFullContact(){
        Contact contact = new Contact();
        RandomData randomData = new RandomData();
        contact.setName(randomData.getName());
        contact.setPhone(randomData.getPhone());
        contact.setEmail(randomData.getEmail());
        return contact;
    }
}

public class Test {

    public static void main(String[] args) {
        for(int i=0; i<5; i++) {
            Contact contact = Mediator.getFullContact();
            System.out.println(contact + " / " + contact.getName() + " / " + contact.getPhone() + " / " + contact.getEmail());
        }
    }
}

Spring-based variant:
pom.xml
CGLIB is used to extend JAVA classes and implements interfaces at runtime.

<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.2.6</version>
</dependency>

Spring ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="contact" class="yourPackage.contacts.Contact" scope="prototype"></bean>
    <bean id="randomData" class="yourPackage.contacts.RandomData" scope="prototype"></bean>

    <bean id="conveyor" class="yourPackage.contacts.MediatorConveyor">
        <lookup-method name="createContact" bean="contact"/> <!-- CGLIB is substitute dynamicaly-created Contact class -->
        <lookup-method name="createRandomData" bean="randomData"/> <!-- CGLIB is substitute dynamicaly-created RandomData class -->
    </bean>

</beans>

Test class:

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        MediatorConveyor conveyor = (MediatorConveyor)context.getBean("conveyor");
        for(int i=0; i<5; i++) {
            Contact contact = conveyor.getContact();
            System.out.println(contact + " / " + contact.getName() + " / " + contact.getPhone() + " / " + contact.getEmail());
        }
    }
}

Application's classes:
MediatorConveyor class:

public abstract class MediatorConveyor{

    protected abstract Contact createContact();

    protected abstract RandomData createRandomData();

    public Contact getContact() {
        Contact contact = createContact();
        RandomData randomData = createRandomData();
        contact.setName(randomData.getName());
        contact.setPhone(randomData.getPhone());
        contact.setEmail(randomData.getEmail());
        return contact;
    }
}

Contact class:

public class Contact {
    private String name = null;
    private String phone = null;
    private String email = null;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

}

RandomData class:

public class RandomData {

    private List<String> alphabetS = new ArrayList<>(Arrays.asList("b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,x,z".split(",")));
    private List<String> alphabetG = new ArrayList<>(Arrays.asList("a,e,i,o,u,y".split(",")));
    private Random random = new Random();
    private String name = "";
    private String phone = "";

    RandomData(){
        createName();
        createNumberPhone();
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public String getEmail() {
        return name.toLowerCase()+"@mail.ru";
    }

    private void createName() {
        int countsymbol = 0;
        while(countsymbol < 5) {
            countsymbol = random.nextInt(10);
        }

        int gls = random.nextInt(2);
        for(int i=0; i<countsymbol; i++) {
            List<String> list = null;
            if(gls == 0) {list = alphabetS; gls=1;}
            else if(gls == 1) {list = alphabetG; gls=0;}

            if (i==0) {name += list.get(random.nextInt(list.size())).toUpperCase();}
            name += list.get(random.nextInt(list.size())); 
        }

    }

    private void createNumberPhone() {
        for(int i=0; i<10; i++) {
            if(i==3 || i==6 || i==8) {phone += "-";}
            phone += random.nextInt(10);
        }
    }

}
Andrew H.
  • 1
  • 1
  • Do you mean you have 2 different objects which share the same fields and you want to transfer the values from one object to the other? – Herr Derb May 25 '18 at 08:23
  • please convert that non-english text to english – rawwar May 25 '18 at 08:27
  • Yes, 2 different objects, transfer the values from one object to the other. Objects must be unique each time – Andrew H. May 25 '18 at 08:28
  • Is your end goal to generate random Contact object or to copy from another object having superset of properties? – Sourabh May 25 '18 at 08:29
  • Got it. I guess this library (http://dozer.sourceforge.net/documentation/gettingstarted.html) might work for you. Does it? – Sourabh May 25 '18 at 08:30
  • generate random Contact and copy properties – Andrew H. May 25 '18 at 08:32
  • If you wish to directly generate a randomly filled contact object, you could use [random-beans](https://github.com/benas/random-beans) lib and do something like `Contact contact = random(Contact.class);`. And to copy, like i mentioned in another comment, you could use dozer. – Sourabh May 25 '18 at 08:37
  • Sourabh Mahajan, thank you, I will review this libraries more – Andrew H. May 25 '18 at 08:42
  • Sourabh, when I create Contact by Contact contact = mapper.map(randomData, Contact.class); Contact is unique, but data from RandomData isn't unique. – Andrew H. May 25 '18 at 09:10

0 Answers0