01: package org.makumba.analyser;
02:
03: import java.util.Arrays;
04: import java.util.HashMap;
05: import java.util.HashSet;
06: import java.util.Iterator;
07: import java.util.LinkedList;
08: import java.util.List;
09:
10: /**
11: * Cache for the page analysis. It is passed along during analysis and holds useful caches. This class provides two
12: * methods to add/retrieve caches throughout the analysis process.
13: *
14: * @author Cristian Bogdan
15: * @author Manuel Gay
16: * @author Rudolf Mayer
17: */
18: public class PageCache {
19:
20: private HashMap<String, HashMap<Object, Object>> caches = new HashMap<String, HashMap<Object, Object>>();
21:
22: private HashMap<String, HashSet<Object>> setCaches = new HashMap<String, HashSet<Object>>();
23:
24: /**
25: * Caches an object in a specific cache
26: *
27: * @param cacheName
28: * the name of the cache we want to work with
29: * @param key
30: * the key of the object in this cache
31: * @param value
32: * the value of the object to be cached
33: */
34: public void cache(String cacheName, Object key, Object value) {
35: HashMap<Object, Object> cache = (HashMap<Object, Object>) caches
36: .get(cacheName);
37: if (cache == null) {
38: cache = new HashMap<Object, Object>();
39: caches.put(cacheName, cache);
40: }
41: cache.put(key, value);
42: }
43:
44: /**
45: * Retrieves an object of a specific cache
46: *
47: * @param cacheName
48: * the name of the cache we want to work with
49: * @param key
50: * the key of the object in this cache
51: * @return the object corresponding to the cache entry, <code>null</code> if no entry exists
52: */
53: public Object retrieve(String cacheName, Object key) {
54: HashMap cache = (HashMap) caches.get(cacheName);
55: if (cache == null)
56: return null;
57: return cache.get(key);
58: }
59:
60: /**
61: * Caches several objects in a specific cache, using sets, i.e. not keeping duplicate values.
62: */
63: public void cacheSetValues(String cacheName, Object[] value) {
64: HashSet<Object> hashSet = setCaches.get(cacheName);
65: if (hashSet == null) {
66: hashSet = new HashSet<Object>();
67: setCaches.put(cacheName, hashSet);
68: }
69:
70: hashSet.addAll(Arrays.asList(value));
71: }
72:
73: /** Retrieves a set from a specific set cache. */
74: public HashSet<Object> retrieveSetValues(String cacheName) {
75: return setCaches.get(cacheName);
76: }
77: }
|