Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.util;
  
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Set;

This Map will remove entries when the value in the map has been cleaned from garbage collection

Parameters:
<K> the key type
<V> the value type
Author(s):
Bill Burke
Adrian Brock
Ales Justin
See also:
JBoss Common Core source code for origins of this class
 
 public final class WeakValueHashMap<K, V> extends java.util.AbstractMap<K, V> {

   
Hash table mapping keys to ref values
 
    private Map<K, ValueRef<K, V>> map;

   
Reference queue for cleared RefKeys
 
    private ReferenceQueue<V> queue = new ReferenceQueue<V>();

   
Constructs a new, empty WeakValueHashMap with the given initial capacity and the given load factor.

Parameters:
initialCapacity The initial capacity of the WeakValueHashMap
loadFactor The load factor of the WeakValueHashMap
Throws:
java.lang.IllegalArgumentException If the initial capacity is less than zero, or if the load factor is nonpositive
 
    public WeakValueHashMap(int initialCapacityfloat loadFactor) {
        = createMap(initialCapacityloadFactor);
    }

   
Constructs a new, empty WeakValueHashMap with the given initial capacity and the default load factor, which is 0.75.

Parameters:
initialCapacity The initial capacity of the WeakValueHashMap
Throws:
java.lang.IllegalArgumentException If the initial capacity is less than zero
 
    public WeakValueHashMap(int initialCapacity) {
        = createMap(initialCapacity);
    }

   
Constructs a new, empty WeakValueHashMap with the default initial capacity and the default load factor, which is 0.75.
 
    public WeakValueHashMap() {
        = createMap();
    }

   
Constructs a new WeakValueHashMap with the same mappings as the specified Map. The WeakValueHashMap is created with an initial capacity of twice the number of mappings in the specified map or 11 (whichever is greater), and a default load factor, which is 0.75.

Parameters:
t the map whose mappings are to be placed in this map.
Since:
1.3
 
    public WeakValueHashMap(Map<K, V> t) {
       this(Math.max(2 * t.size(), 11), 0.75f);
       putAll(t);
    }

   
Create new value ref instance.

Parameters:
key the key
value the value
q the ref queue
Returns:
new value ref instance
 
    private ValueRef<K, V> create(K key, V valueReferenceQueue<V> q) {
       return WeakValueRef.create(keyvalueq);
    }

   
Create map.

Parameters:
initialCapacity the initial capacity
loadFactor the load factor
Returns:
new map instance
   private Map<K, ValueRef<K, V>> createMap(int initialCapacityfloat loadFactor) {
      return new HashMap<K, ValueRef<K, V>>(initialCapacityloadFactor);
   }

   
Create map.

Parameters:
initialCapacity the initial capacity
Returns:
new map instance
   private Map<K, ValueRef<K, V>> createMap(int initialCapacity) {
      return new HashMap<K, ValueRef<K, V>>(initialCapacity);
   }

   
Create map.

Returns:
new map instance
   protected Map<K, ValueRef<K, V>> createMap() {
      return new HashMap<K, ValueRef<K, V>>();
   }
   public int size() {
      processQueue();
      return .size();
   }
   public boolean containsKey(Object key) {
      processQueue();
      return .containsKey(key);
   }
   public V get(Object key) {
      processQueue();
      ValueRef<K, V> ref = .get(key);
      if (ref != null)
         return ref.get();
      return null;
   }
   public V put(K key, V value) {
      processQueue();
      ValueRef<K, V> ref = create(keyvalue);
      ValueRef<K, V> result = .put(keyref);
      if (result != null)
         return result.get();
      return null;
   }
   public V remove(Object key) {
      processQueue();
      ValueRef<K, V> result = .remove(key);
      if (result != null)
         return result.get();
      return null;
   }
   public Set<Entry<K, V>> entrySet() {
      processQueue();
      return new EntrySet();
   }
   public void clear() {
      processQueue();
      .clear();
   }
   public String toString() {
      return .toString();
   }

   
Remove all entries whose values have been discarded.
   @SuppressWarnings("unchecked")
   private void processQueue() {
      ValueRef<K, V> ref = (ValueRef<K, V>) .poll();
      while (ref != null) {
         // only remove if it is the *exact* same WeakValueRef
         if (ref == .get(ref.getKey()))
            .remove(ref.getKey());
         ref = (ValueRef<K, V>) .poll();
      }
   }

   
EntrySet.
   private class EntrySet extends AbstractSet<Entry<K, V>> {
      @Override
      public Iterator<Entry<K, V>> iterator() {
         return new EntrySetIterator(.entrySet().iterator());
      }
      @Override
      public int size() {
         return WeakValueHashMap.this.size();
      }
   }

   
EntrySet iterator
   private class EntrySetIterator implements Iterator<Entry<K, V>> {

      
The delegate
      private Iterator<Entry<K, ValueRef<K, V>>> delegate;

      
Create a new EntrySetIterator.

Parameters:
delegate the delegate
      public EntrySetIterator(Iterator<Entry<K, ValueRef<K, V>>> delegate) {
         this. = delegate;
      }
      public boolean hasNext() {
         return .hasNext();
      }
      public Entry<K, V> next() {
         Entry<K, ValueRef<K, V>> next = .next();
         return next.getValue();
      }
      public void remove() {
         throw new UnsupportedOperationException("remove");
      }
   }

   
Weak value ref.

Parameters:
<K> the key type
<V> the value type
Author(s):
Bill Burke
Adrian Brock
Ales Justin
   private static class WeakValueRef<K, V>
         extends WeakReference<V> implements ValueRef<K, V> {

      
The key
      public K key;

      
Safely create a new WeakValueRef

Parameters:
<K> the key type
<V> the value type
key the key
val the value
q the reference queue
Returns:
the reference or null if the value is null
      static <K, V> WeakValueRef<K, V> create(K key, V valReferenceQueue<V> q) {
         if (val == null)
            return null;
         else
            return new WeakValueRef<K, V>(keyvalq);
      }

      
Create a new WeakValueRef.

Parameters:
key the key
val the value
q the reference queue
      private WeakValueRef(K key, V valReferenceQueue<V> q) {
         super(valq);
         this. = key;
      }
      public K getKey() {
         return ;
      }
      public V getValue() {
         return get();
      }
      public V setValue(V value) {
         throw new UnsupportedOperationException("setValue");
      }
      @Override
      public String toString() {
         return String.valueOf(get());
      }
   }
   public interface ValueRef<K, V> extends Map.Entry<K, V> {

      
Get underlying value.

Returns:
the value
      V get();
   }
New to GrepCode? Check out our FAQ X