This commit is contained in:
Looly 2022-01-11 10:17:29 +08:00 committed by lsx0310
parent c7f0f167fe
commit 56265d1fb3
4 changed files with 43 additions and 3 deletions

View File

@ -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)

View File

@ -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<K, V> extends MapWrapper<K, V> {
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<? super K, ? super V, ? extends V> remappingFunction) {
//noinspection unchecked
return super.computeIfPresent((K) customKey(key), remappingFunction);
}
@Override
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
//noinspection unchecked
return super.compute((K) customKey(key), remappingFunction);
}
@Override
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
//noinspection unchecked
return super.merge((K) customKey(key), value, remappingFunction);
}
//---------------------------------------------------------------------------- Override default methods end
/**
* 自定义键
*

View File

@ -16,7 +16,6 @@ import java.util.function.Function;
* @param <K> 键类型
* @param <V> 值类型
* @author looly
* @author looly
* @since 4.3.3
*/
public class MapWrapper<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Serializable, Cloneable {
@ -179,6 +178,7 @@ public class MapWrapper<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, S
return raw.computeIfAbsent(key, mappingFunction);
}
// 重写默认方法的意义在于如果被包装的Map自定义了这些默认方法包装类就可以保持这些行为的一致性
//---------------------------------------------------------------------------- Override default methods start
@Override
public V getOrDefault(Object key, V defaultValue) {

View File

@ -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<String, String> 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<String, String> 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<String, String> b = new Pair<>("a", "value");
Pair<String, String> a = new Pair<>("A", "value");
final CaseInsensitiveMap<Object, Object> 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());
}
}