public boolean containsKey(Object key) { processQueue(); return map.containsKey(key); }
public Set<K> keySet() { processQueue(); return map.keySet(); }
public boolean isEmpty() { processQueue(); return map.isEmpty(); }
public int size() { processQueue(); // throw out garbage collected values first return map.size(); }
public void clear() { strongReferencesLock.lock(); try { strongReferences.clear(); } finally { strongReferencesLock.unlock(); } processQueue(); // throw out garbage collected values map.clear(); }
public void putAll(Map<? extends K, ? extends V> m) { if (m == null || m.isEmpty()) { processQueue(); return; } for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) { put(entry.getKey(), entry.getValue()); } }
public V remove(Object key) { processQueue(); // throw out garbage collected values first SoftValue<V, K> raw = map.remove(key); return raw != null ? raw.get() : null; }
public boolean containsValue(Object value) { processQueue(); Collection values = values(); return values != null && values.contains(value); }
public Collection<V> values() { processQueue(); Collection<K> keys = map.keySet(); if (keys.isEmpty()) { //noinspection unchecked return Collections.EMPTY_SET; } Collection<V> values = new ArrayList<V>(keys.size()); for (K key : keys) { V v = get(key); if (v != null) { values.add(v); } } return values; }
public Set<Map.Entry<K, V>> entrySet() { processQueue(); // throw out garbage collected values first Collection<K> keys = map.keySet(); if (keys.isEmpty()) { //noinspection unchecked return Collections.EMPTY_SET; } Map<K, V> kvPairs = new HashMap<K, V>(keys.size()); for (K key : keys) { V v = get(key); if (v != null) { kvPairs.put(key, v); } } return kvPairs.entrySet(); }
public V get(Object key) { processQueue(); V result = null; SoftValue<V, K> value = map.get(key); if (value != null) { //unwrap the 'real' value from the SoftReference result = value.get(); if (result == null) { //The wrapped value was garbage collected, so remove this entry from the backing map: //noinspection SuspiciousMethodCalls map.remove(key); } else { //Add this value to the beginning of the strong reference queue (FIFO). addToStrongReferences(result); } } return result; }
/** * Creates a new entry, but wraps the value in a SoftValue instance to enable auto garbage collection. */ public V put(K key, V value) { processQueue(); // throw out garbage collected values first SoftValue<V, K> sv = new SoftValue<V, K>(value, key, queue); SoftValue<V, K> previous = map.put(key, sv); addToStrongReferences(value); return previous != null ? previous.get() : null; }
public boolean containsKey(Object key) { processQueue(); return map.containsKey(key); }
public void clear() { strongReferencesLock.lock(); try { strongReferences.clear(); } finally { strongReferencesLock.unlock(); } processQueue(); // throw out garbage collected values map.clear(); }
public V remove(Object key) { processQueue(); // throw out garbage collected values first SoftValue<V, K> raw = map.remove(key); return raw != null ? raw.get() : null; }
public V remove(Object key) { processQueue(); // throw out garbage collected values first SoftValue<V, K> raw = map.remove(key); return raw != null ? raw.get() : null; }
public void putAll(Map<? extends K, ? extends V> m) { if (m == null || m.isEmpty()) { processQueue(); return; } for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) { put(entry.getKey(), entry.getValue()); } }
public boolean containsValue(Object value) { processQueue(); Collection values = values(); return values != null && values.contains(value); }
/** * Creates a new entry, but wraps the value in a SoftValue instance to enable auto garbage collection. */ public V put(K key, V value) { processQueue(); // throw out garbage collected values first SoftValue<V, K> sv = new SoftValue<V, K>(value, key, queue); SoftValue<V, K> previous = map.put(key, sv); addToStrongReferences(value); return previous != null ? previous.get() : null; }
/** * Creates a new entry, but wraps the value in a SoftValue instance to enable auto garbage collection. */ public V put(K key, V value) { processQueue(); // throw out garbage collected values first SoftValue<V, K> sv = new SoftValue<V, K>(value, key, queue); SoftValue<V, K> previous = map.put(key, sv); addToStrongReferences(value); return previous != null ? previous.get() : null; }