X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.datastructures%2Fsrc%2Forg%2Fsimantics%2Futils%2Fdatastructures%2Fcache%2FCachedMapProvider.java;fp=bundles%2Forg.simantics.utils.datastructures%2Fsrc%2Forg%2Fsimantics%2Futils%2Fdatastructures%2Fcache%2FCachedMapProvider.java;h=aefc753e16bd991a482419467a75a5e819ef9226;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/cache/CachedMapProvider.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/cache/CachedMapProvider.java new file mode 100644 index 000000000..aefc753e1 --- /dev/null +++ b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/cache/CachedMapProvider.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +/* + * + * @author Toni Kalajainen + */ +package org.simantics.utils.datastructures.cache; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.simantics.utils.datastructures.disposable.AbstractDisposable; + +/** + * CachedProvider provides values and stores the results in a cache. + * Cached values must be explicitly released with clear, retain or remove. + * Values are held with strong references. + * + * @param the key + * @param the value + */ +public class CachedMapProvider extends AbstractDisposable implements IMapProvider { + + private Map cache; + + private final IMapProvider provider; + + /** + * Constructs new strong cache + * + * @param provider provider of values + */ + public CachedMapProvider(IMapProvider provider) + { + assert(provider!=null); + this.provider = provider; + this.cache = new HashMap(); + } + + /** + * Constructs new strong cache + * + * @param provider provider of values + * @param cacheMap the map to use + */ + public CachedMapProvider(IMapProvider provider, Map cacheMap) + { + assert(provider!=null); + this.provider = provider; + this.cache = cacheMap; + } + + @Override + public synchronized V get(K key) { + assertNotDisposed(); + V value = cache.get(key); + if (value!=null) return value; + value = provider.get(key); + assert(value!=null); + cache.put(key, value); + return value; + } + + public synchronized void clear() + { + cache.clear(); + } + + public synchronized void retain(Set keys) + { + cache.keySet().retainAll(keys); + } + + public synchronized void remove(Set keys) + { + cache.keySet().removeAll(keys); + } + + public synchronized void load(Set keys) + { + for (K key : keys) + get(key); + } + + public synchronized void addAll(CachedMapProvider anotherCache) + { + assertNotDisposed(); + cache.putAll(anotherCache.cache); + } + + public IMapProvider getProvider() { + return provider; + } + + public synchronized Map getAll() + { + assertNotDisposed(); + return new HashMap(cache); + } + + @Override + protected void doDispose() { + clear(); + } + +}