Follow contract of computeIfAbsent LinkedCaseInsensitiveMap
This commit makes sure that LinkedCaseInsensitiveMap::computeIfAbsent honors the contract of the method, and also replaces the old entry if that mapped to null. Closes gh-26868
This commit is contained in:
@@ -227,7 +227,13 @@ public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable
|
||||
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);
|
||||
V oldKeyValue = this.targetMap.get(oldKey);
|
||||
if (oldKeyValue != null) {
|
||||
return oldKeyValue;
|
||||
}
|
||||
else {
|
||||
key = oldKey;
|
||||
}
|
||||
}
|
||||
return this.targetMap.computeIfAbsent(key, mappingFunction);
|
||||
}
|
||||
|
||||
@@ -102,6 +102,8 @@ class LinkedCaseInsensitiveMapTests {
|
||||
|
||||
assertThat(map.put("null", null)).isNull();
|
||||
assertThat(map.putIfAbsent("NULL", "value")).isNull();
|
||||
assertThat(map.put("null", null)).isEqualTo("value");
|
||||
assertThat(map.computeIfAbsent("NULL", s -> "value")).isEqualTo("value");
|
||||
assertThat(map.get("null")).isEqualTo("value");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user