]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/cache/CachedMapProvider.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / cache / CachedMapProvider.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  *\r
14  * @author Toni Kalajainen\r
15  */\r
16 package org.simantics.utils.datastructures.cache;\r
17 \r
18 import java.util.HashMap;\r
19 import java.util.Map;\r
20 import java.util.Set;\r
21 \r
22 import org.simantics.utils.datastructures.disposable.AbstractDisposable;\r
23 \r
24 /**\r
25  * CachedProvider provides values and stores the results in a cache.\r
26  * Cached values must be explicitly released with clear, retain or remove.\r
27  * Values are held with strong references.\r
28  *\r
29  * @param <K> the key\r
30  * @param <V> the value\r
31  */\r
32 public class CachedMapProvider<K, V> extends AbstractDisposable implements IMapProvider<K, V> {\r
33 \r
34         private Map<K, V> cache;\r
35         \r
36         private final IMapProvider<K, V> provider;\r
37         \r
38         /**\r
39          * Constructs new strong cache\r
40          * \r
41          * @param provider provider of values\r
42          */\r
43         public CachedMapProvider(IMapProvider<K, V> provider)\r
44         {\r
45                 assert(provider!=null);\r
46                 this.provider = provider;\r
47                 this.cache = new HashMap<K, V>();\r
48         }               \r
49 \r
50         /**\r
51          * Constructs new strong cache\r
52          * \r
53          * @param provider provider of values\r
54          * @param cacheMap the map to use\r
55          */\r
56         public CachedMapProvider(IMapProvider<K, V> provider, Map<K, V> cacheMap)\r
57         {\r
58                 assert(provider!=null);\r
59                 this.provider = provider;\r
60                 this.cache = cacheMap;\r
61         }               \r
62         \r
63         @Override\r
64         public synchronized V get(K key) {\r
65                 assertNotDisposed();\r
66                 V value = cache.get(key);\r
67                 if (value!=null) return value;\r
68                 value = provider.get(key);\r
69                 assert(value!=null);\r
70                 cache.put(key, value);                  \r
71                 return value;\r
72         }\r
73         \r
74         public synchronized void clear()\r
75         {\r
76                 cache.clear();\r
77         }\r
78         \r
79         public synchronized void retain(Set<K> keys)\r
80         {\r
81                 cache.keySet().retainAll(keys);\r
82         }\r
83 \r
84         public synchronized void remove(Set<K> keys)\r
85         {\r
86                 cache.keySet().removeAll(keys);\r
87         }\r
88         \r
89         public synchronized void load(Set<K> keys)\r
90         {\r
91                 for (K key : keys)\r
92                         get(key);\r
93         }\r
94         \r
95         public synchronized void addAll(CachedMapProvider<K, V> anotherCache)\r
96         {\r
97                 assertNotDisposed();\r
98                 cache.putAll(anotherCache.cache);\r
99         }       \r
100         \r
101         public IMapProvider<K, V> getProvider() {\r
102                 return provider;\r
103         }\r
104         \r
105         public synchronized Map<K, V> getAll()\r
106         {\r
107                 assertNotDisposed();\r
108                 return new HashMap<K, V>(cache);\r
109         }\r
110 \r
111         @Override\r
112         protected void doDispose() {\r
113                 clear();\r
114         }\r
115         \r
116 }\r