Commit 44e7a702 authored by yanzg's avatar yanzg

增加了值的写入方法

parent 741604d5
......@@ -396,21 +396,29 @@ public class ObjectHelper {
/**
* 对象转换(一般将map转为javabean)
*
* @param to 转换后的对象
* @param from 要转换的对象
* @param to 转换后的对象
* @param from 要转换的对象
* @return 转换后的值 to
*/
public static Object writeWithToClass(Object to, Object from) {
if (to == null) {
return null;
return writeWithToClass(false, to, from);
}
/**
* 对象转换(一般将map转为javabean)
*
* @param emptyWrite 为空时是否写入
* @param to 转换后的对象
* @param from 要转换的对象
* @return 转换后的值 to
*/
public static Object writeWithToClass(boolean emptyWrite, Object to, Object from) {
if (from == null || to == null) {
return to;
}
HashMap<String, MethodField> mapField = getInitTypeField(to.getClass());
for (Map.Entry<String, MethodField> field : mapField.entrySet()) {
String name = field.getValue().name;
Object fromValue = ObjectHelper.get(from, name);
ObjectHelper.set(to, name, fromValue);
}
return to;
return writeWithClass(emptyWrite, to, from, mapField);
}
/**
......@@ -420,33 +428,91 @@ public class ObjectHelper {
* @param froms 来源对象
*/
public static void writeWithFrom(Object to, Object... froms) {
for (Object model : froms) {
if (model instanceof Map) {
Map map = (Map) model;
for (Object key : map.keySet()) {
ObjectHelper.set(to, StringHelper.toString(key), map.get(key));
}
for (Object from : froms) {
if (from instanceof Map) {
writeWithFromMap(to, (Map) from);
} else {
ObjectHelper.writeWithFromClass(to, model);
ObjectHelper.writeWithFromClass(to, from);
}
}
}
/**
* 根据来源类型的字段,将数据写入目标类型的数据
* 将值吸入到到目标数据
*
* @param to 目标类型的数据
* @param from 来源类型的数据
* @return 写入之后的值
*/
public static Object writeWithFromMap(Object to, Map from) {
return writeWithFromMap(false, to, from);
}
/**
* 将值吸入到到目标数据
*
* @param emptyWrite 为空时是否写入
* @param to 目标类型的数据
* @param from 来源类型的数据
* @return 写入之后的值
*/
public static Object writeWithFromMap(boolean emptyWrite, Object to, Map from) {
for (Object key : from.keySet()) {
Object fromValue = from.get(key);
if (StringHelper.isEmpty(fromValue)) {
continue;
}
ObjectHelper.set(to, StringHelper.toString(key), fromValue);
}
return to;
}
/**
* 根据来源类型的字段,将数据写入目标类型的数据,为空时不写入
*
* @param to 目标类型的数据
* @param from 来源类型的数据
* @return 写入之后的值
*/
public static Object writeWithFromClass(Object to, Object from) {
if (from == null) {
return writeWithFromClass(false, to, from);
}
/**
* 根据来源类型的字段,将数据写入目标类型的数据
*
* @param emptyWrite 为空时是否写入
* @param to 目标类型的数据
* @param from 来源类型的数据
* @return 写入之后的值
*/
public static Object writeWithFromClass(boolean emptyWrite, Object to, Object from) {
if (from == null || to == null) {
return to;
}
HashMap<String, MethodField> mapField = getInitTypeField(from.getClass());
return writeWithClass(emptyWrite, to, from, mapField);
}
/**
* 根据字段类型写入值
*
* @param emptyWrite 为空时是否写入
* @param to 目标类型的数据
* @param from 来源类型的数据
* @param mapField 需要写入的字段
* @return 写入之后的值
*/
private static Object writeWithClass(boolean emptyWrite, Object to, Object from, HashMap<String, MethodField> mapField) {
if (from == null || to == null) {
return to;
}
for (Map.Entry<String, MethodField> field : mapField.entrySet()) {
String name = field.getValue().name;
Object fromValue = ObjectHelper.get(from, name);
if (StringHelper.isEmpty(fromValue)) {
continue;
}
ObjectHelper.set(to, name, fromValue);
}
return to;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment