diff --git a/CHANGELOG.md b/CHANGELOG.md index 781abf05e..c80268955 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * 【core 】 修复setter重载导致匹配错误(issue#2082@Github) * 【core 】 修复RegexPool汉字匹配范围小问题(pr#2081@Github) * 【core 】 修复OS中的拼写错误(pr#500@Gitee) +* 【core 】 修复CustomKeyMap的merge失效问题(issue#2086@Github) ------------------------------------------------------------------------------------------------------------- # 5.7.19 (2022-01-07) diff --git a/hutool-core/src/main/java/cn/hutool/core/map/CustomKeyMap.java b/hutool-core/src/main/java/cn/hutool/core/map/CustomKeyMap.java index 66c79de78..4584943a5 100644 --- a/hutool-core/src/main/java/cn/hutool/core/map/CustomKeyMap.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/CustomKeyMap.java @@ -1,6 +1,7 @@ package cn.hutool.core.map; import java.util.Map; +import java.util.function.BiFunction; /** * 自定义键的Map,默认HashMap实现 @@ -67,6 +68,31 @@ public abstract class CustomKeyMap extends MapWrapper { return super.replace((K) customKey(key), value); } + //---------------------------------------------------------------------------- Override default methods start + @Override + public V getOrDefault(Object key, V defaultValue) { + return super.getOrDefault(customKey(key), defaultValue); + } + + @Override + public V computeIfPresent(K key, BiFunction remappingFunction) { + //noinspection unchecked + return super.computeIfPresent((K) customKey(key), remappingFunction); + } + + @Override + public V compute(K key, BiFunction remappingFunction) { + //noinspection unchecked + return super.compute((K) customKey(key), remappingFunction); + } + + @Override + public V merge(K key, V value, BiFunction remappingFunction) { + //noinspection unchecked + return super.merge((K) customKey(key), value, remappingFunction); + } + //---------------------------------------------------------------------------- Override default methods end + /** * 自定义键 * diff --git a/hutool-core/src/main/java/cn/hutool/core/map/MapWrapper.java b/hutool-core/src/main/java/cn/hutool/core/map/MapWrapper.java index f1e40316a..000b15ea6 100644 --- a/hutool-core/src/main/java/cn/hutool/core/map/MapWrapper.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/MapWrapper.java @@ -16,7 +16,6 @@ import java.util.function.Function; * @param 键类型 * @param 值类型 * @author looly - * @author looly * @since 4.3.3 */ public class MapWrapper implements Map, Iterable>, Serializable, Cloneable { @@ -179,6 +178,7 @@ public class MapWrapper implements Map, Iterable>, S return raw.computeIfAbsent(key, mappingFunction); } + // 重写默认方法的意义在于,如果被包装的Map自定义了这些默认方法,包装类就可以保持这些行为的一致性 //---------------------------------------------------------------------------- Override default methods start @Override public V getOrDefault(Object key, V defaultValue) { diff --git a/hutool-core/src/test/java/cn/hutool/core/map/CaseInsensitiveMapTest.java b/hutool-core/src/test/java/cn/hutool/core/map/CaseInsensitiveMapTest.java index b07d4b6db..a62372f12 100644 --- a/hutool-core/src/test/java/cn/hutool/core/map/CaseInsensitiveMapTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/map/CaseInsensitiveMapTest.java @@ -1,10 +1,11 @@ package cn.hutool.core.map; +import cn.hutool.core.lang.Pair; import org.junit.Assert; import org.junit.Test; public class CaseInsensitiveMapTest { - + @Test public void caseInsensitiveMapTest() { CaseInsensitiveMap map = new CaseInsensitiveMap<>(); @@ -12,7 +13,7 @@ public class CaseInsensitiveMapTest { Assert.assertEquals("OK", map.get("aaa")); Assert.assertEquals("OK", map.get("AAA")); } - + @Test public void caseInsensitiveLinkedMapTest() { CaseInsensitiveLinkedMap map = new CaseInsensitiveLinkedMap<>(); @@ -20,4 +21,16 @@ public class CaseInsensitiveMapTest { Assert.assertEquals("OK", map.get("aaa")); Assert.assertEquals("OK", map.get("AAA")); } + + @Test + public void mergeTest(){ + //https://github.com/dromara/hutool/issues/2086 + Pair b = new Pair<>("a", "value"); + Pair a = new Pair<>("A", "value"); + final CaseInsensitiveMap map = new CaseInsensitiveMap<>(); + map.merge(b.getKey(), b.getValue(), (A, B) -> A); + map.merge(a.getKey(), a.getValue(), (A, B) -> A); + + Assert.assertEquals(1, map.size()); + } }