mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix bug
This commit is contained in:
parent
c7f0f167fe
commit
56265d1fb3
@ -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)
|
||||
|
@ -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
|
||||
|
||||
/**
|
||||
* 自定义键
|
||||
*
|
||||
|
@ -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) {
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user