KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > atlassian > seraph > util > CachedPathMapper


1 package com.atlassian.seraph.util;
2
3 import java.util.Map JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.Collection JavaDoc;
6
7 /*
8  * Atlassian Source Code Template.
9  * User: anton
10  * Date: 16/12/2003
11  * Time: 11:28:09
12  * CVS Revision: $Revision: 1.1 $
13  * Last CVS Commit: $Date: 2003/12/16 05:13:45 $
14  * Author of last CVS Commit: $Author: amazkovoi $
15  */

16
17 /**
18  * Caches the results of the {@link PathMapper}
19  */

20 public class CachedPathMapper extends PathMapper
21 {
22     private Map JavaDoc cacheMap;
23     private Map JavaDoc cacheAllMap;
24
25     /**
26      * Creates a CachedPathMapper object that will use cacheMap to cache the
27      * results of the {@link #get(String)} calls and cacheAllMap to cache the
28      * results of the {@link #getAll(String)} class.
29      * @param cacheMap
30      * @param cacheAllMap
31      */

32     public CachedPathMapper(Map JavaDoc cacheMap, Map JavaDoc cacheAllMap)
33     {
34         // Synchronize access to the map for muti-threaded access
35
this.cacheMap = Collections.synchronizedMap(cacheMap);
36         this.cacheAllMap = Collections.synchronizedMap(cacheAllMap);
37     }
38
39     public String JavaDoc get(String JavaDoc path)
40     {
41         // Check the cache
42
if (cacheMap.containsKey(path))
43         {
44             // The result for this path is cached, return the value
45
return (String JavaDoc) cacheMap.get(path);
46         }
47         // Get the result from PathMapper
48
final String JavaDoc result = super.get(path);
49         // Cache the result
50
cacheMap.put(path, result);
51         return result;
52     }
53
54     public Collection JavaDoc getAll(String JavaDoc path)
55     {
56         // Check the cache
57
if (cacheAllMap.containsKey(path))
58         {
59             // The result for this key is cached, return the value
60
return (Collection JavaDoc) cacheAllMap.get(path);
61         }
62         // Get the result from PathMapper
63
final Collection JavaDoc result = super.getAll(path);
64         // Cache the result
65
cacheAllMap.put(path, result);
66         return result;
67     }
68
69     public void put(String JavaDoc key, String JavaDoc pattern)
70     {
71         // Check if we have the entry in the caches
72
if (cacheMap.containsKey(key))
73         {
74             cacheMap.remove(key);
75         }
76         if (cacheAllMap.containsKey(key))
77         {
78             cacheAllMap.remove(key);
79         }
80         // Let PathMapper update the patterns
81
super.put(key, pattern);
82     }
83 }
84
Popular Tags