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
package com.yanzuoguang.util.vo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 数据库操作对象
*
* @param <T>
* @author 颜佐光
*/
public class DataDaoVo<T> {
private List<T> creates = new ArrayList<>();
private List<T> updates = new ArrayList<>();
private List<T> removes = new ArrayList<>();
private Map<String, T> mapNow = new HashMap<>();
/**
* 将历史数据映射为HashMap
*
* @param hisitories 历史数据
* @param keyFunc 获取主键函数
* @return
*/
private Map<String, T> getMapHistory(List<T> hisitories, DataDaoKey<T> keyFunc) {
// 定义缓存集合
Map<String, T> mapHistory = new HashMap<>(10);
// 历史数据处理
if (hisitories != null) {
for (T his : hisitories) {
String key = keyFunc.getKey(his);
mapHistory.put(key, his);
}
}
return mapHistory;
}
/**
* 获取结果
*
* @param mapHistory 历史数据映射
* @param nows 当前数据
* @param keyFunc 获取主键函数
* @return
*/
private void addResult(Map<String, T> mapHistory, List<T> nows, DataDaoKey<T> keyFunc) {
// 返回集
if (nows != null) {
for (T now : nows) {
String key = keyFunc.getKey(now);
T his = mapHistory.get(key);
if (his == null) {
this.creates.add(now);
this.mapNow.put(key, now);
} else {
mapHistory.remove(key);
boolean isChange = keyFunc.set(his, now);
if (isChange) {
this.updates.add(his);
}
this.mapNow.put(key, his);
}
}
}
this.removes.addAll(mapHistory.values());
}
/**
* 初始话对象
*
* @param hisitories
* @param nows
* @param keyFunc
*/
public void add(List<T> hisitories, List<T> nows, DataDaoKey<T> keyFunc) {
Map<String, T> mapHistory = getMapHistory(hisitories, keyFunc);
addResult(mapHistory, nows, keyFunc);
}
/**
* 初始话对象
*
* @param hisitories
* @param nows
* @param keyFunc
*/
public <M> void add(List<T> hisitories, List<M> nows, DataDaoKeyConvert<T, M> keyFunc) {
Map<String, T> mapHistory = getMapHistory(hisitories, keyFunc);
List<T> toNows = new ArrayList<>(10);
if (nows != null) {
for (M m : nows) {
T t = keyFunc.convert(mapHistory, m);
toNows.add(t);
}
}
addResult(mapHistory, toNows, keyFunc);
}
/**
* 初始话对象
*
* @param hisitories
* @param nows
* @param keyFunc
*/
public static <T> DataDaoVo<T> init(List<T> hisitories, List<T> nows, DataDaoKey<T> keyFunc) {
DataDaoVo<T> res = new DataDaoVo<T>();
res.add(hisitories, nows, keyFunc);
return res;
}
/**
* 初始话对象
*
* @param hisitories
* @param nows
* @param keyFunc
*/
public static <T, M> DataDaoVo<T> init(List<T> hisitories, List<M> nows, DataDaoKeyConvert<T, M> keyFunc) {
DataDaoVo<T> res = new DataDaoVo<T>();
res.add(hisitories, nows, keyFunc);
return res;
}
/**
* 需要创建的对象
*
* @return
*/
public List<T> getCreates() {
return creates;
}
/**
* 需要修改的对象
*
* @return
*/
public List<T> getUpdates() {
return updates;
}
/**
* 需要删除的对象
*
* @return
*/
public List<T> getRemoves() {
return removes;
}
/**
* 所有需要修改和更新的对象
*
* @return
*/
public Map<String, T> getMapNow() {
return mapNow;
}
}