/** Returns {@code true} if this map contains a mapping for the specified key. */ @Override public boolean containsKey(final Object key) { return hash.containsKey(replaceNull(key)); }
/** * Declares that the value for the specified key must be retained by hard reference. If there is * already {@link #hardReferencesCount} hard references, then this method replaces the oldest * hard reference by a soft one. */ @SuppressWarnings("unchecked") private void retainStrongly(final K key) { /* * In highly concurrent environments, fields' values (e.g. the size of 'hardCache') * may differ slightly from what expected. */ hardCache.add((K) replaceNull(key)); if (hardCache.size() > hardReferencesCount) { // Remove the last entry if list longer than hardReferencesCount final K toRemove = hardCache.poll(); final Object value = hash.get(replaceNull(toRemove)); final V v = (V) value; if (v instanceof Reference) // check if v is already a Reference: it can happen in concurrent // environments hash.put((K) replaceNull(toRemove), v); else hash.put( (K) replaceNull(toRemove), new Reference<K, V>(hash, (K) replaceNull(toRemove), v, cleaner)); } }
/** * Removes the mapping for this key from this map if present. * * @param key Key whose mapping is to be removed from the map. * @return previous value associated with specified key, or {@code null} if there was no entry * for key. */ @Override public V remove(final Object key) { Object oldValue = hash.remove(replaceNull(key)); if (oldValue instanceof Reference) { oldValue = ((Reference) oldValue).getAndClear(); } else if (oldValue != null) { /* * See the comment in the 'put' method. * In highly concurrent environments, key could have been already removed. */ hardCache.remove(replaceNull(key)); } @SuppressWarnings("unchecked") final V v = (V) oldValue; return v; }
@Override public V get(final Object key) { Object value = hash.get(replaceNull(key)); if (value instanceof Reference) { retainStrongly((K) replaceNull(k)); } else { hash.remove(replaceNull(key));
public V put(final K key, final V value) { ensureNotNull(value); Object oldValue = hash.put((K) replaceNull(key), value); if (oldValue instanceof Reference) { oldValue = ((Reference) oldValue).getAndClear(); hardCache.remove((K) replaceNull(key)); retainStrongly((K) replaceNull(key)); final V v = (V) oldValue; return v;