ReferenceCountedHashMap extends HashMap<K, V> { public synchronized V put(K key, V value) { Integer currentCount = this.updateReference(key, 1); if (currentCount == 1) { return super.put(key, value); } return super.get(key); } ReferenceCountedHashMap();  ReferenceCountedHashMap(Callable<V> cleanerCallable); synchronized V put(K key, V value); synchronized V remove(Object key); void clear(); Object clone(); void putAll(Map<? extends K, ? extends V> m);  }
@Test public void test_insertionWorks() { Map<String, Integer> testMap = new ReferenceCountedHashMap<>(); testMap.put(KEYS[0], VALUES[0]); Assert.assertEquals(testMap.get(KEYS[0]), VALUES[0]); testMap.put(KEYS[1], VALUES[1]); Assert.assertEquals(testMap.get(KEYS[0]), VALUES[0]); Assert.assertEquals(testMap.get(KEYS[1]), VALUES[1]); Assert.assertEquals(testMap.size(), 2); }
@Test public void test_multipleInsertionDoesNotUpdate() { Map<String, Integer> testMap = new ReferenceCountedHashMap<>(); testMap.put(KEYS[0], VALUES[0]); testMap.put(KEYS[0], null); Assert.assertEquals(testMap.get(KEYS[0]), VALUES[0]); testMap.put(KEYS[0], -1); Assert.assertEquals(testMap.get(KEYS[0]), VALUES[0]); Assert.assertEquals(testMap.size(), 1); }