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