Skip to content

Commit

Permalink
Ensure that get() complies with the Map contract of returning null if…
Browse files Browse the repository at this point in the history
… the element is missing, fix #98
  • Loading branch information
RichardWarburton committed May 11, 2017
1 parent a99c10d commit 1d30e59
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ public int computeIfAbsent(final int key, final IntUnaryOperator mappingFunction
*/
public Integer get(final Object key)
{
return get((int)key);
final int primitiveResult = get((int) key);
return primitiveResult == missingValue ? null : primitiveResult;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.util.*;
import java.util.Map.Entry;
import java.util.function.IntUnaryOperator;
import java.util.stream.IntStream;

import static org.hamcrest.Matchers.hasItems;
Expand Down Expand Up @@ -49,6 +48,12 @@ public void getShouldReturnMissingValueWhenEmpty()
assertEquals(MISSING_VALUE, map.get(1));
}

@Test
public void boxedGetShouldReturnNull()
{
assertNull(map.get((Integer) 1));
}

@Test
public void getShouldReturnMissingValueWhenThereIsNoElement()
{
Expand Down Expand Up @@ -403,11 +408,21 @@ public void shouldComputeIfAbsent()
final int testKey = 7;
final int testValue = 7;

final IntUnaryOperator function = (i) -> testValue;

assertEquals(map.missingValue(), map.get(testKey));

assertThat(map.computeIfAbsent(testKey, function), is(testValue));
assertThat(map.computeIfAbsent(testKey, (i) -> testValue), is(testValue));
assertThat(map.get(testKey), is(testValue));
}

@Test
public void shouldComputeIfAbsentBoxed()
{
final Map<Integer, Integer> map = this.map;

final int testKey = 7;
final int testValue = 7;

assertThat(map.computeIfAbsent(testKey, (i) -> testValue), is(testValue));
assertThat(map.get(testKey), is(testValue));
}

Expand Down

0 comments on commit 1d30e59

Please sign in to comment.