/** * Primitive specialised version of {@link #replace(Object, Object)} * * @param key key with which the specified value is associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, or * {@link #missingValue()} if there was no mapping for the key. */ public int replace(final int key, final int value) { int curValue = get(key); if (curValue != missingValue) { curValue = put(key, value); } return curValue; }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") public boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof Map)) { return false; } final Map<Integer, Integer> that = (Map<Integer, Integer>)o; return size == that.size() && entrySet().equals(that.entrySet()); }
@SuppressWarnings("ConstantConditions") @Test public void removeShouldRemoveEntry() { map.put(1, 2); map.remove(1); assertTrue(map.isEmpty()); assertFalse(map.containsKey(1)); assertFalse(map.containsValue(2)); }
@Test public void shouldAllowMissingValueAsKey() { map.put(MISSING_VALUE, 1); assertEquals(1, map.get(MISSING_VALUE)); assertTrue(map.containsKey(MISSING_VALUE)); assertEquals(1, map.size()); final int[] tuple = new int[2]; map.intForEach((k, v) -> { tuple[0] = k; tuple[1] = v; }); assertEquals(MISSING_VALUE, tuple[0]); assertEquals(1, tuple[1]); assertEquals(1, map.remove(MISSING_VALUE)); assertEquals(0, map.size()); assertEquals(MISSING_VALUE, map.get(MISSING_VALUE)); }
@Test public void correctSizeAfterRehash() { final Int2IntHashMap map = new Int2IntHashMap(16, 0.6f, -1); IntStream.range(1, 17).forEach((i) -> map.put(i, i)); assertEquals("Map has correct size", 16, map.size()); final List<Integer> keys = new ArrayList<>(map.keySet()); keys.forEach(map::remove); assertTrue("Map isn't empty", map.isEmpty()); }
/** * Int primitive specialised containsKey. * * @param key the key to check. * @return true if the map contains key as a key, false otherwise. */ public boolean containsKey(final int key) { return get(key) != missingValue; }
@Test public void shouldNotContainMissingValue() { assertFalse(map.containsValue(MISSING_VALUE)); map.put(MISSING_VALUE, 1); assertFalse(map.containsValue(MISSING_VALUE)); }
public void index(DirectBuffer document) { this.document = document; entries.clear(); final int documentLength = document.capacity(); reader.wrap(document, 0, documentLength); final int variables = reader.readMapHeader(); for (int i = 0; i < variables; i++) { final int keyOffset = reader.getOffset(); reader.skipValue(); final int valueOffset = reader.getOffset(); reader.skipValue(); entries.put(keyOffset, valueOffset); } }
@Test public void shouldIterateEntriesBySpecialisedType() { final Map<Integer, Integer> expected = new HashMap<>(); final Int2IntHashMap map = new Int2IntHashMap(Integer.MIN_VALUE); IntStream.range(1, 10).forEachOrdered((i) -> { map.put(i, -i); expected.put(i, -i); }); final Map<Integer, Integer> actual = new HashMap<>(); final Int2IntHashMap.EntryIterator iter = map.entrySet().iterator(); while (iter.hasNext()) { iter.next(); actual.put(iter.getIntKey(), iter.getIntValue()); } assertEquals(expected, actual); }
@Test public void entriesAreAllocatedByEntriesIterator() { map.put(1, 1); map.put(2, 2); final Iterator<Entry<Integer, Integer>> entryIterator = map.entrySet().iterator(); final Entry<Integer, Integer> entry1 = entryIterator.next(); final Entry<Integer, Integer> entry2 = entryIterator.next(); assertNotEquals(entry1, entry2); } }
@Test public void shouldContainKeyOfAPresentKey() { map.put(1, 1); assertTrue(map.containsKey(1)); }
@Test public void forEachShouldLoopOverEveryElement() { map.put(1, 1); map.put(100, 100); final IntIntConsumer mockConsumer = mock(IntIntConsumer.class); map.intForEach(mockConsumer); final InOrder inOrder = inOrder(mockConsumer); inOrder.verify(mockConsumer).accept(1, 1); inOrder.verify(mockConsumer).accept(100, 100); inOrder.verifyNoMoreInteractions(); }