net.sf.juffrou.reflect.BeanConverter Maven / Gradle / Ivy
Show all versions of juffrou-reflect Show documentation
package net.sf.juffrou.reflect;
import java.util.HashMap;
import java.util.Map;
/**
* Utility class to convert between two beans
* Given any two beans and a map that establishes which properties in bean 1 correspond to properties in bean 2,
* this class can be used to automatically obtain bean 1 from an instance of bean 2 and vice-versa.
*
* @author cemartins
*
* @param bean 1 type
* @param bean 2 type
*/
public class BeanConverter {
private Map b1b2BindingMap;
private Map b2b1BindingMap;
private JuffrouBeanWrapper bw1;
private JuffrouBeanWrapper bw2;
/**
* @param clazz1 bean 1 class
* @param clazz2 bean 2 class
* @param propertyBindingMap map that establishes which properties in bean 1 correspond to properties in bean 2
*/
public BeanConverter(Class clazz1, Class clazz2, Map propertyBindingMap) {
this.bw1 = new JuffrouBeanWrapper(clazz1);
this.bw2 = new JuffrouBeanWrapper(clazz2);
this.b1b2BindingMap = propertyBindingMap;
b2b1BindingMap = new HashMap();
for(String b1prop : b1b2BindingMap.keySet()) {
b2b1BindingMap.put(b1b2BindingMap.get(b1prop), b1prop);
}
}
/**
* Get bean 1 from an instance of bean 2
* @param bean2
* @return
*/
public T1 getBean1(T2 bean2) {
bw1.setBean(null);
bw2.setBean(bean2);
for(String b2prop : b2b1BindingMap.keySet()) {
bw1.setValue(b2b1BindingMap.get(b2prop), bw2.getValue(b2prop));
}
return (T1) bw1.getBean();
}
/**
* Get bean 2 from an instance of bean 1
* @param bean1
* @return
*/
public T2 getBean2(T1 bean1) {
bw2.setBean(null);
bw1.setBean(bean1);
for(String b1prop : b1b2BindingMap.keySet()) {
bw2.setValue(b1b2BindingMap.get(b1prop), bw1.getValue(b1prop));
}
return (T2) bw2.getBean();
}
}