Skip to content
Permalink
Browse files
8259622: TreeMap.computeIfAbsent deviates from spec
Reviewed-by: smarks
  • Loading branch information
Tagir F. Valeev committed Jan 15, 2021
1 parent d701bab commit 2c8e337dff4c84fb435cafac8b571f94e161f074
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -575,8 +575,12 @@ public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction
t = t.left;
else if (cmp > 0)
t = t.right;
else
else {
if (t.value == null) {
t.value = callMappingFunctionWithCheck(key, mappingFunction);
}
return t.value;
}
} while (t != null);
} else {
Objects.requireNonNull(key);
@@ -589,8 +593,12 @@ else if (cmp > 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
else {
if (t.value == null) {
t.value = callMappingFunctionWithCheck(key, mappingFunction);
}
return t.value;
}
} while (t != null);
}
newValue = callMappingFunctionWithCheck(key, mappingFunction);
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -257,6 +257,20 @@ void testComputeIfAbsentNull(String desc, Supplier<Map<Object, Object>> ms, Obje
testComputeIfAbsent(map, desc, keys, (k) -> null);
}

@Test(dataProvider = "nullValueFriendlyMaps")
void testComputeIfAbsentOverwriteNull(String desc, Supplier<Map<Object, Object>> ms) {
Map<Object, Object> map = ms.get();
map.put("key", null);
assertEquals(map.size(), 1, desc + ": size != 1");
assertTrue(map.containsKey("key"), desc + ": does not have key");
assertNull(map.get("key"), desc + ": value is not null");
Object result = map.computeIfAbsent("key", k -> "value"); // must rewrite
assertEquals(result, "value", desc + ": computeIfAbsent result is not 'value'");
assertEquals(map.size(), 1, desc + ": size != 1");
assertTrue(map.containsKey("key"), desc + ": does not have key");
assertEquals(map.get("key"), "value", desc + ": value is not 'value'");
}

private static <T> void testComputeIfPresent(Map<T, T> map, String desc, T[] keys,
BiFunction<T, T, T> mappingFunction) {
// remove a third of the keys

1 comment on commit 2c8e337

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 2c8e337 Jan 15, 2021

Please sign in to comment.