Browse Source
Merge in DESIGN/design from ~OBO/design1:release/11.0 to release/11.0 * commit 'd81b21f80f3442d055be45c033898b294330131c': REPORT-107992 移动下单测位置 REPORT-107992 改一下实现 & 格式优化 REPORT-107992 【日志记录数据连接操作】设计器修改数据连接和数据集名称后操作日志会记录大片不好区分release/11.0
Obo-王学仁
1 year ago
4 changed files with 21 additions and 162 deletions
@ -1,93 +0,0 @@
|
||||
package com.fr.design.data; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
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中有哪些是新增的、删除的或者被修改的,并分别进行处理 |
||||
* |
||||
* 对比时默认用equals方法来判断是否为 EntryEventKind#UPDATED |
||||
* |
||||
* @param orig 原始map |
||||
* @param other 参考的新map |
||||
* @param eventHandler 有区别时的事件处理器 |
||||
* @param <K> K |
||||
* @param <V> V |
||||
*/ |
||||
public static <K, V> void contrastMapEntries(@NotNull Map<K, V> orig, @NotNull Map<K, V> other, @NotNull EventHandler<K, V> eventHandler) { |
||||
|
||||
contrastMapEntries(orig, other, eventHandler, UpdateRule.DEFAULT); |
||||
} |
||||
|
||||
/** |
||||
* 对比两个map 查找出相比orig,other中有哪些是新增的、删除的或者被修改的,并分别进行处理 |
||||
* |
||||
* 对比时用自定义的规则来判断是否为 EntryEventKind#UPDATED |
||||
* |
||||
* @param orig 原始map |
||||
* @param other 参考的新map |
||||
* @param eventHandler 有区别时的事件处理器 |
||||
* @param updateRule 自定义的Update事件判定规则 |
||||
* @param <K> |
||||
* @param <V> |
||||
*/ |
||||
public static <K, V> void contrastMapEntries(@NotNull Map<K, V> orig, @NotNull Map<K, V> other, @NotNull EventHandler<K, V> eventHandler, @NotNull UpdateRule<K, V> updateRule) { |
||||
|
||||
Map<K, V> copiedOrig = new LinkedHashMap<>(orig); |
||||
|
||||
other.forEach((k, v) -> { |
||||
V existedV = copiedOrig.remove(k); |
||||
if (existedV != null) { |
||||
if (updateRule.needUpdate(existedV, v)) { |
||||
eventHandler.on(EntryEventKind.UPDATED, k, v); |
||||
} |
||||
} else { |
||||
eventHandler.on(EntryEventKind.ADDED, k, v); |
||||
} |
||||
}); |
||||
|
||||
copiedOrig.forEach((k, v) -> eventHandler.on(EntryEventKind.REMOVED, k, v)); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 事件处理器,对应比较后的三种结果的事件处理 |
||||
* @param <K> |
||||
* @param <V> |
||||
*/ |
||||
public interface EventHandler<K, V> { |
||||
void on(EntryEventKind entryEventKind, K k, V v); |
||||
} |
||||
|
||||
/** |
||||
* 数据被修改(EntryEventKind.UPDATED) 的判定规则 |
||||
* @param <K> |
||||
* @param <V> |
||||
*/ |
||||
public interface UpdateRule<K, V> { |
||||
|
||||
EntryEventKind eventKind = EntryEventKind.UPDATED; |
||||
|
||||
UpdateRule DEFAULT = new UpdateRule() {}; |
||||
|
||||
default boolean needUpdate(V origin, V v) { |
||||
return !v.equals(origin); |
||||
} |
||||
} |
||||
|
||||
public enum EntryEventKind { |
||||
ADDED, |
||||
REMOVED, |
||||
UPDATED; |
||||
} |
||||
} |
@ -1,57 +0,0 @@
|
||||
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<String, String> orig = new LinkedHashMap<>(); |
||||
|
||||
orig.put("aaa", "aaa"); |
||||
orig.put("bbb", "bbb"); |
||||
orig.put("ccc", "ccc"); |
||||
orig.put("ddd", "ddd"); |
||||
|
||||
|
||||
Map<String, String> 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<String, String>() { |
||||
@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(); |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
} |
Loading…
Reference in new issue