com.jme.util
Class WeakIdentityCache<K,V>

java.lang.Object
  extended by com.jme.util.WeakIdentityCache<K,V>

public class WeakIdentityCache<K,V>
extends java.lang.Object

This class is not unsimiliar to the Map interface. It provides methods for fast storing and retrieving object with a key, based on hashes and key/value pairs. However there are some important distinctions from a normal HashMap.
Keys are not compared using object-equality but using reference-equality. This means you can only use the original object to retrieve any values from this cache. It also means the equals() method of your key is never used.
This allows system identy hashes to be used instead of normal hashes, which means the potentially slow hashCode() method of your objects (eg. Buffer objects) are never used.
Finally, the key itself is stored through a WeakReference. Once the key object becomes weakly referable, this reference will be added to the internal ReferenceQueue of this cache. This queue is polled everytime when any of the methods of this are invoked. After the reference is polled the key/value pair is removed from the map, and both key and value can be collected. (In case of the value, if no other references to it exist)

Version:
$Id: WeakIdentityCache.java 4131 2009-03-19 20:15:28Z blaine.dev $
Author:
Tijl Houtbeckers, Joshua Slack - added generics support
See Also:

This is an implementation from scratch, but some of the concepts came from other implementations, most notably WeakIdenityHashMap from the jBoss project. NOTE: this implementation is not synchronized.

Constructor Summary
WeakIdentityCache()
          Create a new WeakIdenityCache (see main javadoc entry for this class)
 
Method Summary
 void clear()
          Clear the cache of all entries it has.
 void expunge()
          Removes all key/value pairs from keys who've become weakly reachable from this cache.
 V get(K key)
          Returns value for this key.
 V put(K key, V value)
          Put a value in this cache with key.
 V remove(K key)
          Removes the value for this key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WeakIdentityCache

public WeakIdentityCache()
Create a new WeakIdenityCache (see main javadoc entry for this class)

Method Detail

get

public V get(K key)
Returns value for this key.


put

public V put(K key,
             V value)
Put a value in this cache with key.
Both key and value should not be null.


remove

public V remove(K key)
Removes the value for this key.


clear

public void clear()
Clear the cache of all entries it has.


expunge

public void expunge()
Removes all key/value pairs from keys who've become weakly reachable from this cache. This method is called from every other method in this class as well, but can be called seperatly to ensure all (in)direct weak reference are removed, when none of the other methods of this class are called frequently enough.
Note that this method is relativly cheap (espc. if the queue is empty) but does most likely involve synchronization.

See Also:
ReferenceQueue.poll()