-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.java
114 lines (100 loc) · 2.93 KB
/
a.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
package com.pingan.finance.xyd.entry.common;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
public abstract class AbstractTransfer<Source, Target> implements
Transfer<Source, Target> {
private Class<Source> sourceClazz;
private Class<Target> targetClazz;
public AbstractTransfer() {
Type[] c = ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments();
sourceClazz = (Class) c[0];
targetClazz = (Class) c[1];
}
@Override
public void convertExt(Source source, Target target) {
}
@Override
public void reverseConvertExt(Target target, Source source) {
}
@Override
public List<Source> reverseConvert(List<Target> targetList) {
if (CollectionUtils.isEmpty(targetList)) {
return null;
}
List<Source> sourceList = new ArrayList<Source>(targetList.size());
for (Target target : targetList) {
Source source;
try {
source = (Source) sourceClazz.newInstance();
} catch (IllegalAccessException illegalAccessException) {
throw new RuntimeException();
} catch (InstantiationException illegalAccessException) {
throw new RuntimeException();
}
BeanUtils.copyProperties(target, source);
reverseConvertExt(target, source);
sourceList.add(source);
}
return sourceList;
}
@Override
public List<Target> convert(List<Source> sourceList) {
if (CollectionUtils.isEmpty(sourceList)) {
return null;
}
List<Target> targetList = new ArrayList<Target>(sourceList.size());
for (Source source : sourceList) {
Target target;
try {
target = (Target) targetClazz.newInstance();
} catch (IllegalAccessException illegalAccessException) {
throw new RuntimeException();
} catch (InstantiationException illegalAccessException) {
throw new RuntimeException();
}
BeanUtils.copyProperties(source, target);
convertExt(source, target);
targetList.add(target);
}
return targetList;
}
@Override
public Target convert(Source source) {
if (source == null) {
return null;
}
Target target;
try {
target = (Target) targetClazz.newInstance();
} catch (IllegalAccessException illegalAccessException) {
throw new RuntimeException();
} catch (InstantiationException illegalAccessException) {
throw new RuntimeException();
}
BeanUtils.copyProperties(source, target);
convertExt(source, target);
return target;
}
@Override
public Source reverseConvert(Target target) {
if (target == null) {
return null;
}
Source source;
try {
source = (Source) sourceClazz.newInstance();
} catch (IllegalAccessException illegalAccessException) {
throw new RuntimeException();
} catch (InstantiationException illegalAccessException) {
throw new RuntimeException();
}
BeanUtils.copyProperties(target, source);
reverseConvertExt(target, source);
return source;
}
}