Java深浅拷贝工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package cn.avicnet.uap.common.core.utils;

import net.sf.cglib.beans.BeanCopier;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;


public class CloneUtils {

/**
* 拷贝对象
*
* @param source 拷贝源
* @param classTargetType 对象类型
* @return 返回对象
*/
public static <T, E> E clone(T source, Class<E> classTargetType) {

if (source == null) {
return null;
}
E targetInstance;
try {
targetInstance = classTargetType.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
BeanUtils.copyProperties(source, targetInstance);
return targetInstance;
}

/**
* 拷贝数组对象
* @param sourceList 拷贝源
* @param classType 对象类型
* @return 返回对象
*/
public static <T, E> List<E> batchClone(List<T> sourceList, Class<E> classType) {
if (sourceList == null) {
return null;
}
List<E> result = new ArrayList<>();
for (T t : sourceList) {
result.add(clone(t, classType));
}
return result;
}

/**
* 浅拷贝对象
*
* @param sourceObj 被拷贝对象
* @param targetClass 拷贝类型
*/
public static <T> T convertToTarget(Object sourceObj, Class<T> targetClass) {
T targetObj;
try {
targetObj = targetClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}

BeanUtils.copyProperties(sourceObj, targetObj);
return targetObj;
}

/**
* 浅拷贝数组对象
*
* @param sourceList 被拷贝数组
* @param targetClass 拷贝类型
*/
public static <T> List<T> convertToTargetList(List<?> sourceList, Class<T> targetClass) {
if (CollectionUtils.isEmpty(sourceList)) {
return Collections.emptyList();
}
if (targetClass == null) {
return null;
}
List<T> targetList = new ArrayList<>();
for (Object sourceObj : sourceList) {
T targetObj = convertToTarget(sourceObj, targetClass);
targetList.add(targetObj);
}
return targetList;
}

/**
* 浅拷贝对象(性能++)
*
* @param source 被拷贝数组
* @param dest 拷贝类型
*/
public static <S, T> T convert(S source, Class<T> dest, Function<T, T> function) {
if (source == null) {
return null;
}
try {
T result = dest.newInstance();
final BeanCopier copier = BeanCopier.create(source.getClass(), dest, false);
copier.copy(source, result, null);
if (function != null) {
function.apply(result);
}
return result;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}

/**
* 浅拷贝对象(性能++)
*
* @param copier 代理对象
* @param dest 目标类型
* @param function 功能校验
*/
public static <S, T> T convert(BeanCopier copier, S source, Class<T> dest, Function<T, T> function) {
if (source == null) {
return null;
}
try {
T result = dest.newInstance();
copier.copy(source, result, null);
if (function != null) {
function.apply(result);
}
return result;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}

/**
* 浅拷贝数组(性能++)
* @param sourceList 被拷贝数组
* @param dest 目标类型
* @param function 功能校验
* @return 目标类型数组
*/
public static <S, T> List<T> convertList(List<S> sourceList, Class<T> dest, Function<T, T> function) {
if (CollectionUtils.isEmpty(sourceList)) {
return Collections.emptyList();
}
try {
List<T> resultList = new ArrayList<>();
final BeanCopier copier = BeanCopier.create(sourceList.get(0).getClass(), dest, false);
for (S source : sourceList) {
T result = dest.newInstance();
copier.copy(source, result, null);
resultList.add(result);
}
if (function != null) {
resultList.forEach(function::apply);
}
return resultList;
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}

/**
* 创建代理对象
* @param source 源类型
* @param dest 目标类型
* @return 代理对象
*/
public static <S, T> BeanCopier createCopier(Class<S> source, Class<T> dest) {
return BeanCopier.create(source, dest, false);
}
}