]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/cache/WeakCachedMapProvider.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / cache / WeakCachedMapProvider.java
diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/cache/WeakCachedMapProvider.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/cache/WeakCachedMapProvider.java
new file mode 100644 (file)
index 0000000..2aab35c
--- /dev/null
@@ -0,0 +1,76 @@
+/*******************************************************************************\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.lang.ref.WeakReference;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+/**\r
+ * WeakCachedProvider provides values and stores the results in a cache.\r
+ * Cached values are held with weak references. Values are removed\r
+ * automatically as they are disposed.\r
+ *\r
+ * Keys are referenced with strong references.\r
+ *\r
+ * @param <K> key type\r
+ * @param <V> value type\r
+ */\r
+public class WeakCachedMapProvider<K, V> implements IMapProvider<K, V> {\r
+\r
+       private Map<K, WeakReference<V>> cache = \r
+               new HashMap<K, WeakReference<V>>();\r
+       \r
+       private final IMapProvider<K, V> provider;\r
+       \r
+       /**\r
+        * Constructs new weak cache.\r
+        * \r
+        * @param provider provider of values \r
+        */\r
+       public WeakCachedMapProvider(IMapProvider<K, V> provider)\r
+       {\r
+               assert(provider!=null);\r
+               this.provider = provider;\r
+       }       \r
+       \r
+       @Override\r
+       public synchronized V get(K key) \r
+       {\r
+               WeakReference<V> ref = cache.get(key);\r
+               if (ref!=null) {\r
+                       V result = ref.get();\r
+                       if (result!=null) return result;\r
+                       cache.remove(ref);\r
+               }\r
+               \r
+               V value = provider.get(key);\r
+               assert(value!=null);\r
+               \r
+               ref = new WeakReference<V>(value);\r
+               cache.put(key, ref);\r
+               return value;           \r
+       }\r
+       \r
+       /**\r
+        * Release all weak references\r
+        */\r
+       public synchronized void clear()\r
+       {\r
+               cache.clear();\r
+       }       \r
+\r
+}\r