KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > rolap > SmartMemberListCache


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

10 package mondrian.rolap;
11
12 import mondrian.rolap.cache.SmartCache;
13 import mondrian.rolap.cache.SoftSmartCache;
14 import mondrian.rolap.sql.SqlConstraint;
15
16 /**
17  * Uses a {@link mondrian.rolap.cache.SmartCache} to store lists of members,
18  * where the key depends on a {@link mondrian.rolap.sql.SqlConstraint}.
19  * <p>
20  * Example 1
21  * <pre>
22  * select ...
23  * [Customer].[Name].members on rows
24  * ...
25  * </pre>
26  * Example 2
27  * <pre>
28  * select ...
29  * NON EMPTY [Customer].[Name].members on rows
30  * ...
31  * WHERE ([Store#14], [Product].[Product#1])
32  * </pre>
33  *
34  * The first set, <em>all</em> customers are computed, in the second only those, who
35  * have bought Product#1 in Store#14. We want to put both results into the cache. Then the
36  * key for the cache entry is the Level that the members belong to <em>plus</em> the
37  * costraint that restricted the amount of members fetched. For Level.Members the key
38  * consists of the Level and the cacheKey of the {@link mondrian.rolap.sql.SqlConstraint}
39  *
40  * @see mondrian.rolap.sql.SqlConstraint#getCacheKey
41  *
42  * @author av
43  * @since Nov 21, 2005
44  * @version $Id: //open/mondrian/src/main/mondrian/rolap/SmartMemberListCache.java#5 $
45  */

46 public class SmartMemberListCache <K, V> {
47     SmartCache<Key2<K, Object JavaDoc>, V> cache;
48
49     /**
50      * a HashMap key that consists of two components.
51      */

52     static class Key2 <T1, T2> {
53         T1 o1;
54         T2 o2;
55
56         public Key2(T1 o1, T2 o2) {
57             this.o1 = o1;
58             this.o2 = o2;
59         }
60
61         public boolean equals(Object JavaDoc obj) {
62             if (!(obj instanceof Key2)) {
63                 return false;
64             }
65             Key2 that = (Key2) obj;
66             return equals(this.o1, that.o1) && equals(this.o2, that.o2);
67         }
68
69         private boolean equals(Object JavaDoc o1, Object JavaDoc o2) {
70             return o1 == null ? o2 == null : o1.equals(o2);
71         }
72
73         public int hashCode() {
74             int c = 1;
75             if (o1 != null) {
76                 c = o1.hashCode();
77             }
78             if (o2 != null) {
79                 c = 31 * c + o2.hashCode();
80             }
81             return c;
82         }
83
84         public String JavaDoc toString() {
85             return "key(" + o1 + "," + o2 + ")";
86         }
87     }
88
89     public SmartMemberListCache() {
90         cache = new SoftSmartCache<Key2<K, Object JavaDoc>, V>();
91     }
92
93     public Object JavaDoc put(K key, SqlConstraint constraint, V value) {
94         Object JavaDoc cacheKey = constraint.getCacheKey();
95         if (cacheKey == null) {
96             return null;
97         }
98         Key2<K, Object JavaDoc> key2 = new Key2<K, Object JavaDoc>(key, cacheKey);
99         return cache.put(key2, value);
100     }
101
102     public V get(K key, SqlConstraint constraint) {
103         Key2<K, Object JavaDoc> key2 = new Key2<K, Object JavaDoc>(key, constraint.getCacheKey());
104         return cache.get(key2);
105     }
106
107     public void clear() {
108         cache.clear();
109     }
110
111     SmartCache getCache() {
112         return cache;
113     }
114
115     void setCache(SmartCache<Key2<K, Object JavaDoc>, V> cache) {
116         this.cache = cache;
117     }
118 }
119
120 // End SmartMemberListCache.java
121
Popular Tags