LinkedCaseInsensitiveMap explicitly implements put/computeIfAbsent

Issue: SPR-16926
This commit is contained in:
Juergen Hoeller
2018-06-11 14:10:12 +02:00
parent 646d7f9e57
commit b790bd1fd6
2 changed files with 45 additions and 20 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import org.springframework.lang.Nullable;
@@ -151,6 +152,7 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
}
@Override
@Nullable
public V getOrDefault(Object key, V defaultValue) {
if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
@@ -162,12 +164,15 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
}
@Override
@Nullable
public V put(String key, @Nullable V value) {
String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
V oldKeyValue = null;
if (oldKey != null && !oldKey.equals(key)) {
this.targetMap.remove(oldKey);
oldKeyValue = this.targetMap.remove(oldKey);
}
return this.targetMap.put(key, value);
V oldValue = this.targetMap.put(key, value);
return (oldKeyValue != null ? oldKeyValue : oldValue);
}
@Override
@@ -178,6 +183,26 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
map.forEach(this::put);
}
@Override
@Nullable
public V putIfAbsent(String key, @Nullable V value) {
String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
if (oldKey != null) {
return this.targetMap.get(oldKey);
}
return this.targetMap.putIfAbsent(key, value);
}
@Override
@Nullable
public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) {
String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
if (oldKey != null) {
return this.targetMap.get(oldKey);
}
return this.targetMap.computeIfAbsent(key, mappingFunction);
}
@Override
@Nullable
public V remove(Object key) {