From 26ab78a7eb67a2e9f8657f75b0adc064183e6fd9 Mon Sep 17 00:00:00 2001
From: yanzg <yanzuoguang@qq.com>
Date: Wed, 6 May 2020 16:17:11 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=96=87=E6=A1=A3=E7=9A=84?=
 =?UTF-8?q?=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../com/yanzuoguang/util/vo/DataDaoKey.java   |  2 +-
 .../util/vo/DataDaoKeyConvert.java            | 17 ++++++
 .../com/yanzuoguang/util/vo/DataDaoVo.java    | 53 +++++++++++++------
 3 files changed, 56 insertions(+), 16 deletions(-)
 create mode 100644 yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoKeyConvert.java

diff --git a/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoKey.java b/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoKey.java
index 11959b86..554260dc 100644
--- a/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoKey.java
+++ b/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoKey.java
@@ -6,7 +6,7 @@ package com.yanzuoguang.util.vo;
  * @param <T>
  * @author 颜佐光
  */
-public interface DataDaoKey<T extends Object> {
+public interface DataDaoKey<T> {
     /**
      * 获取关键字
      *
diff --git a/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoKeyConvert.java b/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoKeyConvert.java
new file mode 100644
index 00000000..71b25729
--- /dev/null
+++ b/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoKeyConvert.java
@@ -0,0 +1,17 @@
+package com.yanzuoguang.util.vo;
+
+/**
+ * 获取关键字
+ *
+ * @param <T>
+ * @author 颜佐光
+ */
+public interface DataDaoKeyConvert<T, M> extends DataDaoKey<T> {
+    /**
+     * 获取关键字
+     *
+     * @param from 来源对象
+     * @return
+     */
+    T convert(M from);
+}
diff --git a/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoVo.java b/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoVo.java
index 42878feb..94185e9f 100644
--- a/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoVo.java
+++ b/yzg-util-base/src/main/java/com/yanzuoguang/util/vo/DataDaoVo.java
@@ -25,31 +25,54 @@ public class DataDaoVo<T> {
      * @param keyFunc
      */
     public static <T> DataDaoVo<T> init(List<T> hisitories, List<T> nows, DataDaoKey<T> keyFunc) {
-        Map<String, T> mapHistory = new HashMap<>((hisitories.size() + nows.size()) / 2);
+        // 定义缓存集合
+        Map<String, T> mapHistory = new HashMap<>(10);
         // 历史数据处理
-        for (T his : hisitories) {
-            String key = keyFunc.getKey(his);
-            T t = mapHistory.get(key);
-            mapHistory.put(key, t);
+        if (hisitories != null) {
+            for (T his : hisitories) {
+                String key = keyFunc.getKey(his);
+                T t = mapHistory.get(key);
+                mapHistory.put(key, t);
+            }
         }
         DataDaoVo<T> res = new DataDaoVo<T>();
         // 返回集
-        for (T now : nows) {
-            String key = keyFunc.getKey(now);
-            T his = mapHistory.get(key);
-            if (his == null) {
-                res.creates.add(now);
-                res.mapNow.put(key, now);
-            } else {
-                keyFunc.set(his, now);
-                res.updates.add(his);
-                res.mapNow.put(key, his);
+        if (nows != null) {
+            for (T now : nows) {
+                String key = keyFunc.getKey(now);
+                T his = mapHistory.get(key);
+                if (his == null) {
+                    res.creates.add(now);
+                    res.mapNow.put(key, now);
+                } else {
+                    keyFunc.set(his, now);
+                    res.updates.add(his);
+                    res.mapNow.put(key, his);
+                }
             }
         }
         res.removes.addAll(mapHistory.values());
 
         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) {
+        List<T> toNows = new ArrayList<>(10);
+        if (nows != null) {
+            for (M m : nows) {
+                T t = keyFunc.convert(m);
+                toNows.add(t);
+            }
+        }
+        return init(hisitories, toNows, keyFunc);
     }
 
     /**
-- 
2.18.1