/** * Does the map contain a value for a given key which is not {@link #initialValue()}. * * @param key the key to check. * @return true if the map contains the key with a value other than {@link #initialValue()}, false otherwise. */ public boolean containsKey(final int key) { return get(key) != initialValue; }
/** * Try get a value for a key and if not present then apply mapping function. * * @param key to search on. * @param mappingFunction to provide a value if the get returns null. * @return the value if found otherwise the missing value. */ public int computeIfAbsent(final int key, final IntUnaryOperator mappingFunction) { int value = get(key); if (value == initialValue) { value = mappingFunction.applyAsInt(key); if (value != initialValue) { put(key, value); } } return value; }
@Test public void getShouldReturnInitialValueWhenEmpty() { assertEquals(INITIAL_VALUE, map.get(1)); }
@Test public void shouldComputeIfAbsent() { final int testKey = 7; final int testValue = 7; final IntUnaryOperator function = (i) -> testValue; assertEquals(map.initialValue(), map.get(testKey)); assertThat(map.computeIfAbsent(testKey, function), is(testValue)); assertThat(map.get(testKey), is(testValue)); }
@Test public void shouldResizeWhenMoreElementsAreAdded() { IntStream .range(1, 100) .forEach((key) -> { final int value = key * 2; assertEquals(INITIAL_VALUE, map.put(key, value)); assertEquals(value, map.get(key)); }); }
@Test public void getShouldReturnPutValues() { map.put(1, 1); assertEquals(1, map.get(1)); }
@Test public void getShouldReturnInitialValueWhenThereIsNoElement() { map.put(1, 1); assertEquals(INITIAL_VALUE, map.get(2)); }
@Test public void shouldResultInNotEmptyAfterIncAndDecWhenHaveKey() { map.put(1, 1); map.incrementAndGet(1); map.decrementAndGet(1); assertEquals(1, map.get(1)); assertEquals(1, map.size()); }
@Test public void shouldRemoveEntryAfterIncToInitialVal() { map.put(1, INITIAL_VALUE - 1); map.incrementAndGet(1); assertEquals(INITIAL_VALUE, map.get(1)); assertTrue(map.isEmpty()); assertFalse(map.containsKey(1)); }
@Test public void shouldRemoveEntryAfterDecToInitialVal() { map.put(1, INITIAL_VALUE + 1); map.decrementAndGet(1); assertEquals(INITIAL_VALUE, map.get(1)); assertTrue(map.isEmpty()); assertFalse(map.containsKey(1)); }
@Test public void shouldAllowInitialValueAsKey() { map.put(INITIAL_VALUE, 1); assertEquals(1, map.get(INITIAL_VALUE)); assertTrue(map.containsKey(INITIAL_VALUE)); assertEquals(1, map.size()); final int[] tuple = new int[2]; map.forEach((k, v) -> { tuple[0] = k; tuple[1] = v; }); assertEquals(INITIAL_VALUE, tuple[0]); assertEquals(1, tuple[1]); assertEquals(1, map.remove(INITIAL_VALUE)); assertEquals(0, map.size()); assertEquals(INITIAL_VALUE, map.get(INITIAL_VALUE)); }
/** * Does the map contain a value for a given key which is not {@link #initialValue()}. * * @param key the key to check. * @return true if the map contains the key with a value other than {@link #initialValue()}, false otherwise. */ public boolean containsKey(final int key) { return get(key) != initialValue; }
/** * Try get a value for a key and if not present then apply mapping function. * * @param key to search on. * @param mappingFunction to provide a value if the get returns null. * @return the value if found otherwise the missing value. */ public int computeIfAbsent(final int key, final IntUnaryOperator mappingFunction) { int value = get(key); if (value == initialValue) { value = mappingFunction.applyAsInt(key); if (value != initialValue) { put(key, value); } } return value; }