org.eclipse.xtext.util
Class OnChangeEvictingCache

java.lang.Object
  extended by org.eclipse.xtext.util.OnChangeEvictingCache
All Implemented Interfaces:
IResourceScopeCache

public class OnChangeEvictingCache
extends java.lang.Object
implements IResourceScopeCache

A cache implementation that stores its values in the scope of a resource. The values will be discarded as soon as the contents of the resource changes semantically. Clients may override this behavior for certain transactions by means of execWithoutCacheClear(Resource, IUnitOfWork) or execWithTemporaryCaching(Resource, IUnitOfWork).

Author:
Sven Efftinge - Initial contribution and API, Sebastian Zarnekow

Nested Class Summary
static class OnChangeEvictingCache.CacheAdapter
           
static interface OnChangeEvictingCache.Listener
          A simple cache listener.
 
Nested classes/interfaces inherited from interface org.eclipse.xtext.util.IResourceScopeCache
IResourceScopeCache.NullImpl
 
Constructor Summary
OnChangeEvictingCache()
           
 
Method Summary
protected  void cacheHit(OnChangeEvictingCache.CacheAdapter adapter)
          Announce a cache hit for the internal statistics of the adapter.
protected  void cacheMiss(OnChangeEvictingCache.CacheAdapter adapter)
          Announce a cache miss for the internal statistics of the adapter.
 void clear(org.eclipse.emf.ecore.resource.Resource resource)
          Clears the cache of the given resource.
<Result,Param extends org.eclipse.emf.ecore.resource.Resource>
Result
execWithoutCacheClear(Param resource, IUnitOfWork<Result,Param> transaction)
          The transaction will be executed.
<Result,Param extends org.eclipse.emf.ecore.resource.Resource>
Result
execWithTemporaryCaching(Param resource, IUnitOfWork<Result,Param> transaction)
          The transaction will be executed with caching enabled.
<T> T
get(java.lang.Object key, org.eclipse.emf.ecore.resource.Resource resource, com.google.inject.Provider<T> provider)
          Try to obtain the value that is cached for the given key in the given resource.
 OnChangeEvictingCache.CacheAdapter getOrCreate(org.eclipse.emf.ecore.resource.Resource resource)
          Returns the cache adapter that is associated with the resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OnChangeEvictingCache

public OnChangeEvictingCache()
Method Detail

clear

public void clear(org.eclipse.emf.ecore.resource.Resource resource)
Clears the cache of the given resource.

Specified by:
clear in interface IResourceScopeCache

get

public <T> T get(java.lang.Object key,
                 org.eclipse.emf.ecore.resource.Resource resource,
                 com.google.inject.Provider<T> provider)
Try to obtain the value that is cached for the given key in the given resource. If no value is cached, the provider is used to compute it and store it afterwards.

Specified by:
get in interface IResourceScopeCache
Parameters:
resource - the resource. If it is null, the provider will be used to compute the value.
key - the cache key. May not be null.
provider - the strategy to compute the value if necessary. May not be null.
Returns:
the cached value or what the provider returns if no cached value exists

cacheMiss

protected void cacheMiss(OnChangeEvictingCache.CacheAdapter adapter)
Announce a cache miss for the internal statistics of the adapter.

Since:
2.1

cacheHit

protected void cacheHit(OnChangeEvictingCache.CacheAdapter adapter)
Announce a cache hit for the internal statistics of the adapter.

Since:
2.1

getOrCreate

public OnChangeEvictingCache.CacheAdapter getOrCreate(org.eclipse.emf.ecore.resource.Resource resource)
Returns the cache adapter that is associated with the resource. The lifecycle of the cache is strongly connected to the resource and its change notifications. Will not return null.

Parameters:
resource - the resource. May not be null.
Returns:
the cache adapter for the given resource. Never null.

execWithoutCacheClear

public <Result,Param extends org.eclipse.emf.ecore.resource.Resource> Result execWithoutCacheClear(Param resource,
                                                                                                   IUnitOfWork<Result,Param> transaction)
                             throws org.eclipse.emf.common.util.WrappedException
The transaction will be executed. While it is running, any semantic state change in the given resource will be ignored and the cache will not be cleared.

Throws:
org.eclipse.emf.common.util.WrappedException

execWithTemporaryCaching

public <Result,Param extends org.eclipse.emf.ecore.resource.Resource> Result execWithTemporaryCaching(Param resource,
                                                                                                      IUnitOfWork<Result,Param> transaction)
                                throws org.eclipse.emf.common.util.WrappedException
The transaction will be executed with caching enabled. However, all newly cached values will be discarded as soon as the transaction is over.

Throws:
org.eclipse.emf.common.util.WrappedException
Since:
2.1