**
* @author yong.liu
* @date 2021/2/26
*/
public class BeanConvertUtils {
private static Logger logger = LoggerFactory.getLogger(BeanConvertUtils.class);
/**
* 对象拷贝
*
* @param dest 目标对象
* @param source 源对象
*/
public static <T1, T2> void convert(T1 dest, T2 source) throws ConvertException {
convert(dest, source, new DefaultConvert());
}
public static <T1, T2> void convert(T1 dest, T2 source, Converter converter) throws ConvertException {
if(null == dest || null == source){
return;
}
try {
BeanCopier copier = BeanCopier.create(source.getClass(), dest.getClass(), converter != null);
copier.copy(source, dest, converter);
} catch (Exception e) {
logger.error("", e);
throw new ConvertException("","转换失败");
}
}
/**
* 批量转换
*
* @param data
* @param clazz
* @return
*/
public static <T2, T1> List<T2> convertList(List<T1> data, Class<T2> clazz) throws ConvertException {
return convertList(data, clazz, new DefaultConvert());
}
/**
* 批量转换
*
* @param data
* @param clazz
* @param converter
* @return
*/
public static <T2, T1> List<T2> convertList(List<T1> data, Class<T2> clazz, Converter converter) throws ConvertException {
if(data == null){
return null;
}
List<T2> t2 = new ArrayList<T2>();
if(null != data && data.size() > 0){
BeanCopier copier = BeanCopier.create(data.get(0).getClass(), clazz, converter != null);
for (T1 t1 : data) {
T2 _t2;
try {
_t2 = clazz.newInstance();
} catch (Exception e) {
throw new ConvertException("","转换异常");
}
copier.copy(t1, _t2, converter);
t2.add(_t2);
}
}
return t2;
}
static class DefaultConvert implements Converter {
@Override
public Object convert(Object value, Class target, Object context) {
if (value instanceof Integer) {
return Integer.valueOf(value.toString());
} else if (value instanceof Float) {
return Float.valueOf(value.toString());
} else if (value instanceof Double) {
return Double.valueOf(value.toString());
} else if (value instanceof Short) {
return Short.valueOf(value.toString());
} else if (value instanceof BigDecimal) {
return (BigDecimal)value;
}
return value;
}
}
}
public interface IError {
public String getErrorCode();
public String getErrorMessage();
}
public class ErrorCodeException extends RuntimeException implements IError {
private static final long serialVersionUID = 4038040954783731576L;
protected String errorCode;
public ErrorCodeException(String errorCode, String errorInfo) {
super(errorInfo);
this.errorCode = errorCode;
}
public ErrorCodeException(IError errorObj) {
this(errorObj.getErrorCode(),errorObj.getErrorMessage());
}
public ErrorCodeException(String errorCode, String errorInfo, Throwable e) {
super(errorInfo,e);
this.errorCode = errorCode;
}
public ErrorCodeException(String errorCode, Throwable e) {
super(e);
this.errorCode = errorCode;
}
@Override
public String getErrorCode() {
return this.errorCode;
}
@Override
public String getErrorMessage() {
return getMessage();
}
}
public class ConvertException extends ErrorCodeException{
public ConvertException(String errorCode, String errorInfo) {
super(errorCode, errorInfo);
}
public ConvertException(IError errorObj) {
super(errorObj);
}
public ConvertException(String errorCode, String errorInfo, Throwable e) {
super(errorCode, errorInfo, e);
}
public ConvertException(String errorCode, Throwable e) {
super(errorCode, e);
}
}