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:
Arjen Poutsma
2021-04-30 15:49:00 +02:00
parent 399e7ebf22
commit b18cf3c873
2 changed files with 9 additions and 1 deletions

View File

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

View File

@@ -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");
}