]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/cache/CachedMapProvider.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / cache / CachedMapProvider.java
index aefc753e16bd991a482419467a75a5e819ef9226..4cccefbf7b77fb1f85dca662f538988b368bbe7a 100644 (file)
-/*******************************************************************************\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
+/*******************************************************************************
+ * 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 <K> the key
+ * @param <V> the value
+ */
+public class CachedMapProvider<K, V> extends AbstractDisposable implements IMapProvider<K, V> {
+
+       private Map<K, V> cache;
+       
+       private final IMapProvider<K, V> provider;
+       
+       /**
+        * Constructs new strong cache
+        * 
+        * @param provider provider of values
+        */
+       public CachedMapProvider(IMapProvider<K, V> provider)
+       {
+               assert(provider!=null);
+               this.provider = provider;
+               this.cache = new HashMap<K, V>();
+       }               
+
+       /**
+        * Constructs new strong cache
+        * 
+        * @param provider provider of values
+        * @param cacheMap the map to use
+        */
+       public CachedMapProvider(IMapProvider<K, V> provider, Map<K, V> 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<K> keys)
+       {
+               cache.keySet().retainAll(keys);
+       }
+
+       public synchronized void remove(Set<K> keys)
+       {
+               cache.keySet().removeAll(keys);
+       }
+       
+       public synchronized void load(Set<K> keys)
+       {
+               for (K key : keys)
+                       get(key);
+       }
+       
+       public synchronized void addAll(CachedMapProvider<K, V> anotherCache)
+       {
+               assertNotDisposed();
+               cache.putAll(anotherCache.cache);
+       }       
+       
+       public IMapProvider<K, V> getProvider() {
+               return provider;
+       }
+       
+       public synchronized Map<K, V> getAll()
+       {
+               assertNotDisposed();
+               return new HashMap<K, V>(cache);
+       }
+
+       @Override
+       protected void doDispose() {
+               clear();
+       }
+       
+}