/** * 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. * @param resource the resource. If it is <code>null</code>, the provider will be used to compute the value. * @param key the cache key. May not be <code>null</code>. * @param provider the strategy to compute the value if necessary. May not be <code>null</code>. */ public <T> T get(Object key, Resource resource, Provider<T> provider) { if(resource == null) { return provider.get(); } CacheAdapter adapter = getOrCreate(resource); T element = adapter.<T>internalGet(key); if (element==null) { element = provider.get(); cacheMiss(adapter); adapter.set(key, element); } else { cacheHit(adapter); } if (element == CacheAdapter.NULL) { return null; } return element; }
/** * 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 <code>null</code>. * @param resource the resource. May not be <code>null</code>. * @return the cache adapter for the given resource. Never <code>null</code>. */ public CacheAdapter getOrCreate(Resource resource) { CacheAdapter adapter = (CacheAdapter) EcoreUtil.getAdapter(resource.eAdapters(), CacheAdapter.class); if (adapter == null) { adapter = new CacheAdapter(); resource.eAdapters().add(adapter); adapter.setResource(resource); } return adapter; }
@Override public void notifyChanged(Notification notification) { super.notifyChanged(notification); if (ignoreNotificationCounter.get() == 0 && !ignoreNotifications && isSemanticStateChange(notification)) { clearValues(); Iterator<Listener> iter = listeners.iterator(); while(iter.hasNext()) { Listener next = iter.next(); iter.remove(); next.onEvict(this); } } }
protected List<IContainer> getVisibleContainers(Resource resource) { IResourceDescription description = descriptionManager.getResourceDescription(resource); IResourceDescriptions resourceDescriptions = getResourceDescriptions(resource); String cacheKey = getCacheKey("VisibleContainers", resource.getResourceSet()); OnChangeEvictingCache.CacheAdapter cache = new OnChangeEvictingCache().getOrCreate(resource); List<IContainer> result = null; result = cache.get(cacheKey); if (result == null) { result = containerManager.getVisibleContainers(description, resourceDescriptions); // SZ: I'ld like this dependency to be moved to the implementation of the // container manager, but it is not aware of a CacheAdapter if (resourceDescriptions instanceof IResourceDescription.Event.Source) { IResourceDescription.Event.Source eventSource = (Source) resourceDescriptions; DelegatingEventSource delegatingEventSource = new DelegatingEventSource(eventSource); delegatingEventSource.addListeners(Lists.newArrayList(Iterables.filter(result, IResourceDescription.Event.Listener.class))); delegatingEventSource.initialize(); cache.addCacheListener(delegatingEventSource); } cache.set(cacheKey, result); } return result; }
/** * 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. * @param resource the resource. If it is <code>null</code>, the provider will be used to compute the value. * @param key the cache key. May not be <code>null</code>. * @param provider the strategy to compute the value if necessary. May not be <code>null</code>. */ @Override public <T> T get(Object key, Resource resource, Provider<T> provider) { if(resource == null) { return provider.get(); } CacheAdapter adapter = getOrCreate(resource); T element = adapter.<T>internalGet(key); if (element==null) { element = provider.get(); cacheMiss(adapter); adapter.set(key, element); } else { cacheHit(adapter); } if (element == CacheAdapter.NULL) { return null; } return element; }
/** * 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. */ public <Result, Param extends Resource> Result execWithoutCacheClear(Param resource, IUnitOfWork<Result, Param> transaction) throws WrappedException { CacheAdapter cacheAdapter = getOrCreate(resource); try { cacheAdapter.ignoreNotifications(); return transaction.exec(resource); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new WrappedException(e); } finally { cacheAdapter.listenToNotifications(); } }
/** * 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 <code>null</code>. * @param resource the resource. May not be <code>null</code>. * @return the cache adapter for the given resource. Never <code>null</code>. */ public CacheAdapter getOrCreate(Resource resource) { CacheAdapter adapter = (CacheAdapter) EcoreUtil.getAdapter(resource.eAdapters(), CacheAdapter.class); if (adapter == null) { adapter = new CacheAdapter(); resource.eAdapters().add(adapter); adapter.setResource(resource); } return adapter; }
/** * 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. */ public <Result, Param extends Resource> Result execWithoutCacheClear(Param resource, IUnitOfWork<Result, Param> transaction) throws WrappedException { CacheAdapter cacheAdapter = getOrCreate(resource); try { cacheAdapter.ignoreNotifications(); return transaction.exec(resource); } catch (Exception e) { throw new WrappedException(e); } finally { cacheAdapter.listenToNotifications(); } }
public <T> T get(Object name) { T result = internalGet(name); if (result != NULL) return result; return null; }
/** * Announce a cache miss for the internal statistics of the adapter. * @since 2.1 */ protected void cacheMiss(CacheAdapter adapter) { adapter.cacheMiss(); }
@Override protected void afterModelLinked(EObject model, IDiagnosticConsumer diagnosticsConsumer) { super.afterModelLinked(model, diagnosticsConsumer); cache.getOrCreate(model.eResource()).listenToNotifications(); }
@Override public void notifyChanged(Notification notification) { super.notifyChanged(notification); if (ignoreNotificationCounter.get() == 0 && !ignoreNotifications && isSemanticStateChange(notification)) { clearValues(); Iterator<Listener> iter = listeners.iterator(); while(iter.hasNext()) { Listener next = iter.next(); iter.remove(); next.onEvict(this); } } }
/** * Clears the cache of the given resource. */ public void clear(Resource resource) { getOrCreate(resource).clearValues(); }
/** * Announce a cache miss for the internal statistics of the adapter. * @since 2.1 */ protected void cacheMiss(CacheAdapter adapter) { adapter.cacheMiss(); }
/** * Clears the cache of the given resource. */ @Override public void clear(Resource resource) { getOrCreate(resource).clearValues(); }
@Override protected void beforeModelLinked(EObject model, IDiagnosticConsumer diagnosticsConsumer) { discardGeneratedPackages(model); super.beforeModelLinked(model, diagnosticsConsumer); cache.getOrCreate(model.eResource()).ignoreNotifications(); }
public <T> T get(Object name) { T result = internalGet(name); if (result != NULL) return result; return null; }
/** * Announce a cache hit for the internal statistics of the adapter. * @since 2.1 */ protected void cacheHit(CacheAdapter adapter) { adapter.cacheHit(); }
/** * Announce a cache hit for the internal statistics of the adapter. * @since 2.1 */ protected void cacheHit(CacheAdapter adapter) { adapter.cacheHit(); }