/** * Clears the map, resetting the size to zero and nullifying references * to avoid garbage collection issues. */ @Override public void clear() { // override to reset the linked list super.clear(); header.before = header.after = header; }
/** * Checks whether the map is currently empty. * * @return true if the map is currently size zero */ @Override public boolean isEmpty() { purgeBeforeRead(); return super.isEmpty(); }
/** * Puts all the values from the specified map into this map. * <p> * This implementation iterates around the specified map and * uses {@link #put(Object, Object)}. * <p> * It is private to allow the constructor to still call it * even when putAll is overriden. * * @param map the map to add * @throws NullPointerException if the map is null */ private void _putAll(final Map<? extends K, ? extends V> map) { final int mapSize = map.size(); if (mapSize == 0) { return; } final int newSize = (int) ((size + mapSize) / loadFactor + 1); ensureCapacity(calculateNewCapacity(newSize)); for (final Map.Entry<? extends K, ? extends V> entry: map.entrySet()) { put(entry.getKey(), entry.getValue()); } }
@Override public boolean remove(final Object key) { final boolean result = parent.containsKey(key); parent.remove(key); return result; }
/** * Adds a new key-value mapping into this map. * <p> * This implementation calls <code>createEntry()</code>, <code>addEntry()</code> * and <code>checkCapacity()</code>. * It also handles changes to <code>modCount</code> and <code>size</code>. * Subclasses could override to fully control adds to the map. * * @param hashIndex the index into the data array to store at * @param hashCode the hash code of the key to add * @param key the key to add * @param value the value to add */ protected void addMapping(final int hashIndex, final int hashCode, final K key, final V value) { modCount++; final HashEntry<K, V> entry = createEntry(data[hashIndex], hashCode, key, value); addEntry(entry, hashIndex); size++; checkCapacity(); }
/** * Puts a key-value mapping into this map. * * @param key the key to add * @param value the value to add * @return the value previously mapped to this key, null if none */ @Override public V put(final K key, final V value) { final Object convertedKey = convertKey(key); final int hashCode = hash(convertedKey); final int index = hashIndex(hashCode, data.length); HashEntry<K, V> entry = data[index]; while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(convertedKey, entry.key)) { final V oldValue = entry.getValue(); updateEntry(entry, value); return oldValue; } entry = entry.next; } addMapping(index, hashCode, key, value); return null; }
private void prepareNext() throws IOException { while (cache.size() < capacity && source.hasTop()) { putCache(source.getTopKey(), source.getTopValue()); source.next(); hitFullCache = true; Key k = source.getTopKey(); Value v = cache.get(k); boolean isNewVal = v == null; v = isNewVal ? source.getTopValue() : combiner.multiply(k, v, source.getTopValue()); iterCache = removeFromMapToEmit(null); ppIn++; cache.put(k, v); source.next(); } else { if (cache.isEmpty()) { log.info(String.format("Performance: cap=%5d hitFullCache=%b in=%6d out=%6d diff=%6d -> %2d%% less", capacity, hitFullCache, ppIn, ppOut, ppIn - ppOut, (int)(100*((float)(ppIn-ppOut))/ppIn)));
/** * Stores the value against the specified multi-key. * * @param key1 the first key * @param key2 the second key * @param key3 the third key * @param key4 the fourth key * @param key5 the fifth key * @param value the value to store * @return the value previously mapped to this combined key, null if none */ public V put(final K key1, final K key2, final K key3, final K key4, final K key5, final V value) { final int hashCode = hash(key1, key2, key3, key4, key5); final int index = decorated().hashIndex(hashCode, decorated().data.length); AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[index]; while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2, key3, key4, key5)) { final V oldValue = entry.getValue(); decorated().updateEntry(entry, value); return oldValue; } entry = entry.next; } decorated().addMapping(index, hashCode, new MultiKey<>(key1, key2, key3, key4, key5), value); return null; }
/** * Gets the size of the map. * * @return the size */ @Override public int size() { if (delegateMap != null) { return delegateMap.size(); } return size; }
if (size() == 0) { return "{}"; final StringBuilder buf = new StringBuilder(32 * size()); buf.append('{'); final MapIterator<K, V> it = mapIterator(); boolean hasNext = it.hasNext(); while (hasNext) {
final int capacity = in.readInt(); final int size = in.readInt(); init(); threshold = calculateThreshold(capacity, loadFactor); data = new HashEntry[capacity]; for (int i = 0; i < size; i++) { final K key = (K) in.readObject(); final V value = (V) in.readObject(); put(key, value);
public void remove() { if (last == null) { throw new IllegalStateException(AbstractHashedMap.REMOVE_INVALID); } if (parent.modCount != expectedModCount) { throw new ConcurrentModificationException(); } parent.remove(last.getKey()); last = null; expectedModCount = parent.modCount; }
private MapIterator<Key, Value> removeFromMapToEmit(MapIterator<Key, Value> iterCache) { if (urm) { if (iterCache == null) iterCache = cache.mapIterator(); iterCache.next(); emitKey = iterCache.getKey(); emitValue = iterCache.getValue(); iterCache.remove(); return iterCache; } else { emitKey = ((LRUMap<Key,Value>)cache).firstKey(); // LRU emitValue = cache.remove(emitKey); return null; } }
@Override public MapIterator<MultiKey<? extends K>, V> mapIterator() { return decorated().mapIterator(); }
/** * Converts the flat map data to a map. */ private void convertToMap() { delegateMap = createDelegateMap(); switch (size) { // drop through case 3: delegateMap.put(key3, value3); case 2: delegateMap.put(key2, value2); case 1: delegateMap.put(key1, value1); case 0: break; default: throw new IllegalStateException("Invalid map index: " + size); } size = 0; hash1 = hash2 = hash3 = 0; key1 = key2 = key3 = null; value1 = value2 = value3 = null; }
public V get(final Object key) { if (delegateMap != null) { return delegateMap.get(key);
/** * Puts all the values from the specified map into this map. * <p> * This implementation iterates around the specified map and * uses {@link #put(Object, Object)}. * * @param map the map to add * @throws NullPointerException if the map is null */ @Override public void putAll(final Map<? extends K, ? extends V> map) { _putAll(map); }
/** * Stores the value against the specified multi-key. * * @param key1 the first key * @param key2 the second key * @param value the value to store * @return the value previously mapped to this combined key, null if none */ public V put(final K key1, final K key2, final V value) { final int hashCode = hash(key1, key2); final int index = decorated().hashIndex(hashCode, decorated().data.length); AbstractHashedMap.HashEntry<MultiKey<? extends K>, V> entry = decorated().data[index]; while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(entry, key1, key2)) { final V oldValue = entry.getValue(); decorated().updateEntry(entry, value); return oldValue; } entry = entry.next; } decorated().addMapping(index, hashCode, new MultiKey<>(key1, key2), value); return null; }
@Override public int size() { return parent.size(); }