01: /*
02: * User: Michael Rettig
03: * Date: Sep 20, 2002
04: * Time: 8:48:27 PM
05: */
06: package net.sourceforge.jaxor.util;
07:
08: import net.sourceforge.jaxor.QueryParams;
09: import net.sourceforge.jaxor.api.EntityInterface;
10: import net.sourceforge.jaxor.api.QueryCache;
11:
12: import java.io.IOException;
13: import java.io.ObjectInputStream;
14: import java.util.*;
15:
16: public class MethodCache implements QueryCache {
17:
18: private transient Map _cacheByClass = createMap();
19:
20: public static boolean USE_WEAK_REFERENCES = true;
21:
22: private void readObject(ObjectInputStream in) throws IOException,
23: ClassNotFoundException {
24: in.defaultReadObject();
25: _cacheByClass = createMap();
26: }
27:
28: private static Map createMap() {
29: if (USE_WEAK_REFERENCES)
30: return new WeakHashMap();
31: return new HashMap();
32: }
33:
34: public List get(Class clzz, String method, QueryParams param) {
35: Map paramCache = getMethodCache(clzz, method);
36: return (List) paramCache.get(param);
37: }
38:
39: public void put(Class cl, String method, QueryParams param,
40: List result) {
41: Map paramCache = getMethodCache(cl, method);
42: paramCache.put(param, result);
43: }
44:
45: private Map getMethodCache(Class cl, String method) {
46: Map classMethodCache = getClassCache(cl);
47: Map methodCache = (Map) classMethodCache.get(method);
48: if (methodCache == null) {
49: methodCache = createMap();
50: classMethodCache.put(method, methodCache);
51: }
52: return methodCache;
53: }
54:
55: private Map getClassCache(Class clzz) {
56: Map val = (Map) _cacheByClass.get(clzz);
57: if (val == null) {
58: val = createMap();
59: _cacheByClass.put(clzz, val);
60: }
61: return val;
62: }
63:
64: public void remove(EntityInterface abstractEntity) {
65: Iterator iter = _cacheByClass.values().iterator();
66: while (iter.hasNext()) {
67: Map classCache = (Map) iter.next();
68: Iterator caches = classCache.values().iterator();
69: while (caches.hasNext()) {
70: Map methodCache = (Map) caches.next();
71: Iterator methodIt = methodCache.values().iterator();
72: while (methodIt.hasNext()) {
73: List list = (List) methodIt.next();
74: list.remove(abstractEntity);
75: }
76: }
77: }
78: }
79:
80: public void clear(Class aClass) {
81: _cacheByClass.remove(aClass);
82: }
83:
84: public void clear() {
85: _cacheByClass.clear();
86: }
87: }
|