gu.sql2java.IBeanConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sql2java-base Show documentation
Show all versions of sql2java-base Show documentation
sql2java common class package
package gu.sql2java;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import static com.google.common.base.Preconditions.*;
/**
*
* @author guyadong
*
* @param left type
* @param right type
*/
public interface IBeanConverter {
/**
* Default abstract implementation of {@link IBeanConverter}
*
* @author guyadong
*
* @param left type
* @param right type
*/
public static abstract class AbstractHandle implements IBeanConverter {
/** L type */
protected final Class> leftType;
/** R type */
protected final Class> rightType;
private static Class> getRawClass(Type type){
if(type instanceof Class>){
return (Class>) type;
} else if(type instanceof ParameterizedType){
return getRawClass(((ParameterizedType) type).getRawType());
} else{
throw new IllegalArgumentException("invalid type");
}
}
public AbstractHandle() {
Type superClass = getClass().getGenericSuperclass();
this.leftType = getRawClass(((ParameterizedType) superClass).getActualTypeArguments()[0]);
this.rightType = getRawClass(((ParameterizedType) superClass).getActualTypeArguments()[1]);
}
public AbstractHandle(Class leftClass,Class rightClass) {
this.leftType = checkNotNull(leftClass,"leftClass is null");
this.rightType = checkNotNull(rightClass,"rightClass is null");
}
/**
* copy right TO left, left and right must not be null
* @param left
* @param right
*/
protected abstract void doFromRight(L left, R right);
/**
* copy left TO right, left and right must not be null
* @param left
* @param right
*/
protected abstract void doToRight(L left, R right);
/**
* Creates a new L instance by calling constructor with an empty argument list
* you must override the method if the L class haven't default constructor.
* @return L instance
*/
@SuppressWarnings("unchecked")
protected L newInstanceL(){ return (L) newInstance(this.leftType); }
/**
* Creates a new R instance by calling constructor with an empty argument list
* you must override the method if the R class haven't default constructor.
* @return R instance
*/
@SuppressWarnings("unchecked")
protected R newInstanceR(){ return (R) newInstance(this.rightType); }
protected static T newInstance(Classclazz){
try {
return (T) clazz.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Override
public L fromRight(L left, R right) {
if(null != right && null != left){
this.doFromRight(left, right);
}
return left;
}
@Override
public R toRight(L left, R right) {
if(null != left && null != right){
this.doToRight(left, right);
}
return right;
}
@Override
public L fromRight(R bean) {
return null == bean? null : fromRight(newInstanceL(),bean);
}
@Override
public R toRight(L bean) {
return null == bean? null : toRight(bean,newInstanceR());
}
@Override
public R[] toRight(L[] lefts, R[] rights) {
if(null != lefts && null != rights){
if( lefts.length != rights.length){
throw new IllegalArgumentException("mismatched length between left and right array");
}
for(int i=0;i toRight(Collection beans) {
if(null==beans){
return null;
}
ArrayList rights = new ArrayList(beans.size());
for(L g:beans){
rights.add(this.toRight(g));
}
return rights;
}
@Override
public List fromRight(Collection beans) {
if(null==beans){
return null;
}
ArrayList lefts = new ArrayList(beans.size());
for(R n:beans){
lefts.add(this.fromRight(n));
}
return lefts;
}
@Override
public List toRight(List lefts, List rights) {
if(null != lefts && null != rights){
if( lefts.size() != rights.size()){
throw new IllegalArgumentException("mismatched length between left and right list");
}
for(int i=0;i fromRight(List lefts, List rights) {
if(null != rights && null != lefts){
if( lefts.size() != rights.size()){
throw new IllegalArgumentException("mismatched length between left and right list");
}
for(int i=0;i toRight(List lefts) {
List rights = null;
if(null != lefts ){
rights = new ArrayList<>();
for(L l:lefts){
rights.add(toRight(l));
}
}
return rights;
}
@Override
public List fromRight(List rights) {
List lefts = null;
if(null != rights ){
lefts = new ArrayList<>();
for(R r:rights){
lefts.add(fromRight(r));
}
}
return lefts;
}
@Override
public Map toRightKey(Map lmap) {
if(null == lmap){
return null;
}
HashMap rmap = new HashMap(16);
for(Entry entry:lmap.entrySet()){
rmap.put(this.toRight(entry.getKey()),entry.getValue());
}
return rmap;
}
@Override
public Map toRightValue(Map lmap) {
if(null == lmap){
return null;
}
HashMap rmap = new HashMap(16);
for(Entry entry:lmap.entrySet()){
rmap.put(entry.getKey(), this.toRight(entry.getValue()));
}
return rmap;
}
@Override
public Map fromRightKey(Map rmap) {
if(null == rmap){
return null;
}
HashMap lmap = new HashMap(16);
for(Entry entry:rmap.entrySet()){
lmap.put(this.fromRight(entry.getKey()),entry.getValue());
}
return lmap;
}
@Override
public Map fromRightValue(Map rmap) {
if(null == rmap){
return null;
}
HashMap lmap = new HashMap(16);
for(Entry entry:rmap.entrySet()){
lmap.put(entry.getKey(), this.fromRight(entry.getValue()));
}
return lmap;
}
@Override
public Map toRight(Map lmap) {
if(null == lmap){
return null;
}
HashMap rmap = new HashMap(16);
for(Entry entry:lmap.entrySet()){
rmap.put(this.toRight(entry.getKey()),this.toRight(entry.getValue()));
}
return rmap;
}
@Override
public Map fromRight(Map rmap) {
if(null == rmap){
return null;
}
HashMap lmap = new HashMap(16);
for(Entry entry:rmap.entrySet()){
lmap.put(this.fromRight(entry.getKey()),this.fromRight(entry.getValue()));
}
return lmap;
}
}
/**
* copy right TO left
* @param left
* @param right
* @return left,or new instance if left is null
*/
public L fromRight(L left, R right);
/**
* copy left TO right
* @param left
* @param right
* @return right,or new instance if right is null
*/
public R toRight(L left, R right);
/**
* return an new instance converted from R bean
* @param bean
* @return L bean
*/
public L fromRight(R bean);
/**
* return an new instance converted from L bean
* @param bean
* @return R bean
*/
public R toRight( L bean);
/**
* copy rights TO lefts
* @param lefts
* @param rights
* @return lefts,or new array if lefts is null
*/
public L[] fromRight(L[] lefts,R[] rights);
/**
* copy lefts TO rights
* @param lefts
* @param rights
* @return rights,or new array if rights is null
*/
public R[] toRight(L[] lefts,R[] rights);
/**
* return an new array converted from R beans
* @param beans
* @return L bean array
*/
public L[] fromRight(R[] beans);
/**
* an new array converted from L beans
* @param beans
* @return R bean array
*/
public R[] toRight(L[] beans);
/**
* copy rights TO lefts
* @param lefts
* @param rights
* @return lefts,or new array if lefts is null
*/
public List fromRight(List lefts,List rights);
/**
* copy lefts TO rights
* @param lefts
* @param rights
* @return rights,or new array if rights is null
*/
public List toRight(List lefts,List rights);
/**
* return an new list converted from R beans
* @param beans
* @return L bean list
*/
public List fromRight(List beans);
/**
* return an new list converted from L beans
* @param beans
* @return R bean list
*/
public List toRight(List beans);
/**
* return an new list converted from R beans
* @param beans
* @return L bean list
*/
public List fromRight(Collection beans);
/**
* an new list converted from L beans
* @param beans
* @return R bean list
*/
public List toRight(Collection beans);
/**
* return an new map with R key converted from map with L key
* @param lmap
* @return Map with R key
*/
public Map toRightKey(Map lmap);
/**
* return an new map with R value converted from map with L value
* @param lmap
* @return Map with R value
*/
public Map toRightValue(Map lmap);
/**
* return an new map with L key converted from map with R key
* @param rmap
* @return Map with L key
*/
public Map fromRightKey(Map rmap);
/**
* return an new map with L value converted from map with R value
* @param rmap
* @return Map with L value
*/
public Map fromRightValue(Map rmap);
/**
* an new map with R key and R value converted from map with L key and L value
* @param lmap
* @return Map with R key and value
*/
public Map toRight(Map lmap);
/**
* return an new map with L key and L value converted from map with R key and R value
* @param rmap
* @return Map with L key and value
*/
public Map fromRight(Map rmap);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy