From f746d3b29939f847e7d2e3b9b8fdc31eef2ed710 Mon Sep 17 00:00:00 2001 From: rinoux Date: Mon, 28 Mar 2022 16:06:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?REPORT-68607=20=E6=95=B0=E6=8D=AE=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=85=A8=E9=87=8F=E4=BF=9D=E5=AD=98=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=A2=9E=E9=87=8F=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/design/data/MapCompareUtils.java | 51 +++++++++++++++++++ .../datapane/connect/ConnectionListPane.java | 37 ++++++++++---- 2 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/design/data/MapCompareUtils.java diff --git a/designer-base/src/main/java/com/fr/design/data/MapCompareUtils.java b/designer-base/src/main/java/com/fr/design/data/MapCompareUtils.java new file mode 100644 index 0000000000..0d7bc6b441 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/data/MapCompareUtils.java @@ -0,0 +1,51 @@ +package com.fr.design.data; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * @author rinoux + * @version 10.0 + * Created by rinoux on 2022/3/28 + */ +public final class MapCompareUtils { + + + /** + * 对比两个map 查找出相比orig,other中有哪些是新增的、删除的或者被修改的,并分别进行处理 + * + * @param orig 原始map + * @param other 参考的新map + * @param eventHandler 有区别时的事件处理器 + * @param K + * @param V + */ + public static void contrastMapEntries(Map orig, Map other, EventHandler eventHandler) { + + Map copiedOrig = new LinkedHashMap<>(orig); + + other.forEach((k, v) -> { + V existedV = copiedOrig.remove(k); + if (existedV != null) { + if (!v.equals(existedV)) { + eventHandler.on(EntryEventKind.UPDATED, k, v); + } + } else { + eventHandler.on(EntryEventKind.ADDED, k, v); + } + }); + + copiedOrig.forEach((k, v) -> eventHandler.on(EntryEventKind.REMOVED, k, v)); + } + + + public interface EventHandler { + void on(EntryEventKind entryEventKind, K k, V v); + } + + public enum EntryEventKind { + ADDED, + REMOVED, + UPDATED; + } +} diff --git a/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java b/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java index daa408ab6c..c0d23ed466 100644 --- a/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java +++ b/designer-base/src/main/java/com/fr/design/data/datapane/connect/ConnectionListPane.java @@ -4,6 +4,7 @@ import com.fr.data.impl.Connection; import com.fr.data.impl.JDBCDatabaseConnection; import com.fr.data.impl.JNDIDatabaseConnection; import com.fr.design.ExtraDesignClassManager; +import com.fr.design.data.MapCompareUtils; import com.fr.design.dialog.FineJOptionPane; import com.fr.design.fun.ConnectionProvider; import com.fr.design.gui.controlpane.JListControlPane; @@ -14,6 +15,7 @@ import com.fr.design.i18n.Toolkit; import com.fr.file.ConnectionConfig; import com.fr.general.ComparatorUtils; import com.fr.general.NameObject; +import com.fr.log.FineLoggerFactory; import com.fr.stable.ArrayUtils; import com.fr.stable.Nameable; import com.fr.stable.StringUtils; @@ -23,6 +25,7 @@ import javax.swing.SwingUtilities; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -34,7 +37,8 @@ import java.util.UUID; public class ConnectionListPane extends JListControlPane implements ConnectionShowPane { public static final String TITLE_NAME = Toolkit.i18nText("Fine-Design_Basic_Server_Define_Data_Connection"); private boolean isNamePermitted = true; - private HashMap renameMap = new HashMap(); + private final HashMap renameMap = new HashMap<>(); + private final Map populatedConnectionsSnapshot = new LinkedHashMap<>(); public ConnectionListPane() { renameMap.clear(); @@ -138,11 +142,16 @@ public class ConnectionListPane extends JListControlPane implements ConnectionSh */ public void populate(ConnectionConfig connectionConfig) { List nameObjectList = new ArrayList(); + populatedConnectionsSnapshot.clear(); for (Map.Entry entry : connectionConfig.getConnections().entrySet()) { nameObjectList.add(new NameObject(entry.getKey(), entry.getValue())); + try { + populatedConnectionsSnapshot.put(entry.getKey(), (Connection) entry.getValue().clone()); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } } this.populate(nameObjectList.toArray(new NameObject[nameObjectList.size()])); - } /** @@ -151,15 +160,21 @@ public class ConnectionListPane extends JListControlPane implements ConnectionSh public void update(ConnectionConfig connectionConfig) { // Nameable[]居然不能强转成NameObject[],一定要这么写... Nameable[] res = this.update(); - NameObject[] res_array = new NameObject[res.length]; - java.util.Arrays.asList(res).toArray(res_array); - - connectionConfig.removeAllConnection(); - - for (int i = 0; i < res_array.length; i++) { - NameObject nameObject = res_array[i]; - connectionConfig.addConnection(nameObject.getName(), (Connection) nameObject.getObject()); - } + Map updatedMap = new LinkedHashMap<>(); + Arrays.stream(res).map(n -> (NameObject) n).forEach(no -> updatedMap.put(no.getName(), (Connection) no.getObject())); + + MapCompareUtils.contrastMapEntries(populatedConnectionsSnapshot, updatedMap, (entryEventKind, s, connection) -> { + switch (entryEventKind) { + case REMOVED: + connectionConfig.removeConnection(s); + break; + case ADDED: + case UPDATED: + connectionConfig.addConnection(s, connection); + default: + break; + } + }); } @Override From 24f93e7d99efab056e834e30c91890bb2680b885 Mon Sep 17 00:00:00 2001 From: rinoux Date: Mon, 28 Mar 2022 16:13:43 +0800 Subject: [PATCH 2/2] updated --- .../com/fr/design/data/MapCompareUtils.java | 4 +- .../fr/design/data/MapCompareUtilsTest.java | 57 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 designer-base/src/test/java/com/fr/design/data/MapCompareUtilsTest.java diff --git a/designer-base/src/main/java/com/fr/design/data/MapCompareUtils.java b/designer-base/src/main/java/com/fr/design/data/MapCompareUtils.java index 0d7bc6b441..f7492aefcc 100644 --- a/designer-base/src/main/java/com/fr/design/data/MapCompareUtils.java +++ b/designer-base/src/main/java/com/fr/design/data/MapCompareUtils.java @@ -1,5 +1,7 @@ package com.fr.design.data; +import org.jetbrains.annotations.NotNull; + import java.util.LinkedHashMap; import java.util.Map; @@ -20,7 +22,7 @@ public final class MapCompareUtils { * @param K * @param V */ - public static void contrastMapEntries(Map orig, Map other, EventHandler eventHandler) { + public static void contrastMapEntries(@NotNull Map orig, @NotNull Map other, @NotNull EventHandler eventHandler) { Map copiedOrig = new LinkedHashMap<>(orig); diff --git a/designer-base/src/test/java/com/fr/design/data/MapCompareUtilsTest.java b/designer-base/src/test/java/com/fr/design/data/MapCompareUtilsTest.java new file mode 100644 index 0000000000..1f1e60e2be --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/data/MapCompareUtilsTest.java @@ -0,0 +1,57 @@ +package com.fr.design.data; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * @author rinoux + * @version 10.0 + * Created by rinoux on 2022/3/28 + */ +public class MapCompareUtilsTest { + + @Test + public void contrastMapEntries() { + + + Map orig = new LinkedHashMap<>(); + + orig.put("aaa", "aaa"); + orig.put("bbb", "bbb"); + orig.put("ccc", "ccc"); + orig.put("ddd", "ddd"); + + + Map other = new LinkedHashMap<>(); + + other.put("aaa", "111"); + other.put("bbb", "bbb"); + other.put("ccc", "ccc"); + other.put("eee", "eee"); + + + MapCompareUtils.contrastMapEntries(orig, other, new MapCompareUtils.EventHandler() { + @Override + public void on(MapCompareUtils.EntryEventKind entryEventKind, String s, String s2) { + switch (entryEventKind) { + case UPDATED: + Assert.assertEquals(s, "aaa"); + Assert.assertEquals(s2, "111"); + break; + case REMOVED: + Assert.assertEquals(s, "ddd"); + break; + case ADDED: + Assert.assertEquals(s, "eee"); + Assert.assertEquals(s2, "eee"); + break; + default: + Assert.fail(); + } + } + }); + } +} \ No newline at end of file