Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions drv/OpenHashMap.drv
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,15 @@ public class OPEN_HASH_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENE
@Override
public VALUE_GENERIC_TYPE putIfAbsent(final KEY_GENERIC_TYPE k, final VALUE_GENERIC_TYPE v) {
final int pos = find(k);
if (pos >= 0) return value[pos];
if (pos >= 0) {
#if VALUES_REFERENCE
if (value[pos] == null) {
value[pos] = v;
return null;
}
#endif
return value[pos];
}
insert(-pos - 1, k, v);
return defRetValue;
}
Expand Down Expand Up @@ -1186,7 +1194,15 @@ public class OPEN_HASH_MAP KEY_VALUE_GENERIC extends ABSTRACT_MAP KEY_VALUE_GENE
public VALUE_GENERIC_TYPE computeIfAbsent(final KEY_GENERIC_TYPE key, final FUNCTION KEY_SUPER_GENERIC_VALUE_EXTENDS_GENERIC mappingFunction) {
java.util.Objects.requireNonNull(mappingFunction);
final int pos = find(key);
if (pos >= 0) return value[pos];
if (pos >= 0) {
#if VALUES_REFERENCE
if (value[pos] == null) {
if (!mappingFunction.containsKey(key)) return defRetValue;
return value[pos] = mappingFunction.GET_VALUE(key);
}
#endif
return value[pos];
}

if (!mappingFunction.containsKey(key)) return defRetValue;
final VALUE_GENERIC_TYPE newValue = mappingFunction.GET_VALUE(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ public void testCombinationMethodsWithoutDefaultValue() {
assertNull(map.computeIfPresent("def", (a, b) -> "four"));
assertFalse(map.containsKey("def"));
}

@Test
public void testPutIfAbsentReplacesNullValue() {
final Object2ObjectOpenHashMap<String, String> map = new Object2ObjectOpenHashMap<>();
map.put("a", null);

assertNull(map.putIfAbsent("a", "b"));
assertEquals("b", map.get("a"));
}

@Test
public void testComputeIfAbsentReplacesNullValue() {
final Object2ObjectOpenHashMap<String, String> map = new Object2ObjectOpenHashMap<>();
map.put("a", null);

assertEquals("b", map.computeIfAbsent("a", key -> "b"));
assertEquals("b", map.get("a"));
}
}