KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > rolap > cache > SoftSmartCache


1 /*
2 // This software is subject to the terms of the Common Public License
3 // Agreement, available at the following URL:
4 // http://www.opensource.org/licenses/cpl.html.
5 // Copyright (C) 2004-2005 TONBELLER AG
6 // All Rights Reserved.
7 // You must accept the terms of that agreement to use this software.
8 */

9 package mondrian.rolap.cache;
10
11 import java.lang.ref.ReferenceQueue JavaDoc;
12 import java.lang.ref.SoftReference JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * A map with soft references that is cleaned up in regular intervals.
18  * <p>
19  * There is no contains(key) method because it makes no sense - after
20  * contains() returns true, the garbage collector may remove
21  * the value that was contained. Instead the code should call get() and
22  * keep a reference to the value to prevent garbage collection.
23  *
24  * @author av
25  * @since Nov 3, 2005
26  * @version $Id: //open/mondrian/src/main/mondrian/rolap/cache/SoftSmartCache.java#5 $
27  */

28 public class SoftSmartCache <K, V> implements SmartCache <K, V> {
29
30     Map JavaDoc<K, CacheReference> cache = new HashMap JavaDoc<K, CacheReference>();
31     ReferenceQueue JavaDoc<V> queue = new ReferenceQueue JavaDoc<V>();
32
33     /**
34      * an entry in the cache that contains the key for
35      * the cache map to remove the entry when its value
36      * has been garbage collected
37      *
38      * @author rk
39      * @since Nov 7, 2005
40      */

41     class CacheReference extends SoftReference JavaDoc<V> {
42         K key;
43
44         public CacheReference(K key, V value) {
45             super(value, queue);
46             this.key = key;
47         }
48
49         public String JavaDoc toString() {
50             return String.valueOf(get());
51         }
52     }
53
54     /* (non-Javadoc)
55      * @see mondrian.rolap.cache.SmartCache#put(java.lang.Object, java.lang.Object)
56      */

57     public synchronized V put(K key, V value) {
58         // remove garbage collected entries from cache
59
CacheReference ref;
60         while ((ref = (CacheReference) queue.poll()) != null) {
61             cache.remove(ref.key);
62         }
63
64         // put new entry into cache
65
ref = new CacheReference(key, value);
66         ref = cache.put(key, ref);
67         if (ref != null) {
68             return ref.get();
69         }
70         return null;
71     }
72
73     /* (non-Javadoc)
74      * @see mondrian.rolap.cache.SmartCache#get(java.lang.Object)
75      */

76     public synchronized V get(K key) {
77         CacheReference ref = cache.get(key);
78         if (ref == null) {
79             return null;
80         }
81         V value = ref.get();
82         if (value == null) {
83             cache.remove(key);
84         }
85         return value;
86     }
87
88     /* (non-Javadoc)
89      * @see mondrian.rolap.cache.SmartCache#clear()
90      */

91     public void clear() {
92         cache.clear();
93     }
94
95     /* (non-Javadoc)
96      * @see mondrian.rolap.cache.SmartCache#size()
97      */

98     public int size() {
99         return cache.size();
100     }
101
102 }
103
104 // End SoftSmartCache.java
105

106
Popular Tags