--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ *\r
+ * @author Toni Kalajainen\r
+ */\r
+package org.simantics.utils.datastructures.cache;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.utils.datastructures.disposable.AbstractDisposable;\r
+\r
+/**\r
+ * CachedProvider provides values and stores the results in a cache.\r
+ * Cached values must be explicitly released with clear, retain or remove.\r
+ * Values are held with strong references.\r
+ *\r
+ * @param <K> the key\r
+ * @param <V> the value\r
+ */\r
+public class CachedMapProvider<K, V> extends AbstractDisposable implements IMapProvider<K, V> {\r
+\r
+ private Map<K, V> cache;\r
+ \r
+ private final IMapProvider<K, V> provider;\r
+ \r
+ /**\r
+ * Constructs new strong cache\r
+ * \r
+ * @param provider provider of values\r
+ */\r
+ public CachedMapProvider(IMapProvider<K, V> provider)\r
+ {\r
+ assert(provider!=null);\r
+ this.provider = provider;\r
+ this.cache = new HashMap<K, V>();\r
+ } \r
+\r
+ /**\r
+ * Constructs new strong cache\r
+ * \r
+ * @param provider provider of values\r
+ * @param cacheMap the map to use\r
+ */\r
+ public CachedMapProvider(IMapProvider<K, V> provider, Map<K, V> cacheMap)\r
+ {\r
+ assert(provider!=null);\r
+ this.provider = provider;\r
+ this.cache = cacheMap;\r
+ } \r
+ \r
+ @Override\r
+ public synchronized V get(K key) {\r
+ assertNotDisposed();\r
+ V value = cache.get(key);\r
+ if (value!=null) return value;\r
+ value = provider.get(key);\r
+ assert(value!=null);\r
+ cache.put(key, value); \r
+ return value;\r
+ }\r
+ \r
+ public synchronized void clear()\r
+ {\r
+ cache.clear();\r
+ }\r
+ \r
+ public synchronized void retain(Set<K> keys)\r
+ {\r
+ cache.keySet().retainAll(keys);\r
+ }\r
+\r
+ public synchronized void remove(Set<K> keys)\r
+ {\r
+ cache.keySet().removeAll(keys);\r
+ }\r
+ \r
+ public synchronized void load(Set<K> keys)\r
+ {\r
+ for (K key : keys)\r
+ get(key);\r
+ }\r
+ \r
+ public synchronized void addAll(CachedMapProvider<K, V> anotherCache)\r
+ {\r
+ assertNotDisposed();\r
+ cache.putAll(anotherCache.cache);\r
+ } \r
+ \r
+ public IMapProvider<K, V> getProvider() {\r
+ return provider;\r
+ }\r
+ \r
+ public synchronized Map<K, V> getAll()\r
+ {\r
+ assertNotDisposed();\r
+ return new HashMap<K, V>(cache);\r
+ }\r
+\r
+ @Override\r
+ protected void doDispose() {\r
+ clear();\r
+ }\r
+ \r
+}\r