org.eclipse.xtext.util
Class SimpleCache<Key,Value>

java.lang.Object
  extended by org.eclipse.xtext.util.SimpleCache<Key,Value>

public class SimpleCache<Key,Value>
extends java.lang.Object

A primitive cache implementation. The SimpleCache allows to cache lazily computable values. Subsequent calls to the computation algorithm with equal parameters have to yield equal results. Attention: The algorithm may not depend on itself in a circular manner. E.g. the following will lead to a stack overflow:

 SimpleCache cache = new SimpleCache(new Function() {
   public V apply(K k) {
     // DON'T DO THIS
     if (k == k1) {
       return cache.get(k2).method();
     } else if (k == k2) {
       return cache.get(k1).method();
     }
     return null;
   }
 });
 
The cache uses weak references to the keys but the values are strongly referenced. This leads to the conclusion, that the computed values should not refer to the keys because no cache entry will be reclaimend automatically. In such cases, clients have to discard the values for a key explicitly. Please note that Function.apply(Object) may be invoked concurrently while the cache itself is threadsafe.

Author:
Sebastian Zarnekow - Initial contribution and API

Constructor Summary
SimpleCache(com.google.common.base.Function<Key,Value> f)
           
 
Method Summary
 void clear()
           
 void discard(Key k)
           
 Value get(Key k)
           
 int getSize()
           
 boolean hasCachedValue(Key key)
           
 boolean isEmpty()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleCache

public SimpleCache(com.google.common.base.Function<Key,Value> f)
Method Detail

get

public Value get(Key k)

clear

public void clear()

discard

public void discard(Key k)

hasCachedValue

public boolean hasCachedValue(Key key)

getSize

public int getSize()

isEmpty

public boolean isEmpty()