com.sun.hk2.component
Class SoftCache<K,V>

java.lang.Object
  extended by com.sun.hk2.component.SoftCache<K,V>

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

A very primitive cache of at most one entry, that uses both SoftReference(s) to the key as well as the value it caches. Useful to provide minimal caching without imposing on heap.

Since:
3.1
Author:
Jeff Trent

Field Summary
protected  java.lang.ref.SoftReference<K> key
           
protected  java.lang.ref.SoftReference<V> value
           
 
Constructor Summary
SoftCache()
           
 
Method Summary
protected  V cacheValue(java.lang.ref.SoftReference<V> value2, V thisV)
           
 void clear()
          Clears the cache
 V get(K k, java.util.concurrent.Callable<V> callable)
          Returns either the cached value if hasn't been reclaimed by GC.
protected  V usingCacheValue(V thisV)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

key

protected java.lang.ref.SoftReference<K> key

value

protected java.lang.ref.SoftReference<V> value
Constructor Detail

SoftCache

public SoftCache()
Method Detail

get

public V get(K k,
             java.util.concurrent.Callable<V> callable)
Returns either the cached value if hasn't been reclaimed by GC. Otherwise invokes the callable to refresh the cached value, and caching it again in the process.

Parameters:
key - the key; must be non-null
callable - the callable in case the cache is empty; must be non-null
Returns:
the value (either from cache or populated by the callable as a last resort)

cacheValue

protected V cacheValue(java.lang.ref.SoftReference<V> value2,
                       V thisV)

usingCacheValue

protected V usingCacheValue(V thisV)

clear

public void clear()
Clears the cache



Copyright © 2010 Oracle Corporation. All Rights Reserved.