KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > util > CachingWildcardMap


1 package org.jacorb.notification.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 /**
25  * Add Caching to WildcardMap. If the Keys inside the Map contain the Wildcard Operator '*' the
26  * Operation getWithExpansion is rather timeconsuming. For each Key that contains a '*' a pattern
27  * match must be done. This Decorator adds simple Caching. When a key is looked up the retrieved
28  * value is stored in an internal cache with fixed size. Subsequent calls to getWithExpansion query
29  * the cache first. As soon as a put or remove Operation occurs the Cache is invalidated.
30  *
31  * @author Alphonse Bendt
32  * @version $Id: CachingWildcardMap.java,v 1.5 2005/02/14 00:13:05 alphonse.bendt Exp $
33  */

34
35 public class CachingWildcardMap implements WildcardMap
36 {
37     private final Object JavaDoc[] cachedKeys_;
38
39     private final Object JavaDoc[][] cachedValues_;
40
41     private Object JavaDoc victimKey_;
42
43     private Object JavaDoc[] victimValue_;
44
45     private final int cacheSize_;
46
47     private final WildcardMap delegate_;
48
49     public CachingWildcardMap()
50     {
51         this(4, new DefaultWildcardMap());
52     }
53
54     public CachingWildcardMap(int cacheSize, WildcardMap delegate)
55     {
56         super();
57
58         cachedValues_ = new Object JavaDoc[cacheSize][];
59         cachedKeys_ = new Object JavaDoc[cacheSize];
60         cacheSize_ = cacheSize;
61         delegate_ = delegate;
62     }
63
64     private int calcPosition(String JavaDoc key)
65     {
66         return key.charAt(0) % cacheSize_;
67     }
68
69     private void invalidateCache()
70     {
71         for (int x = 0; x < cacheSize_; ++x)
72         {
73             cachedKeys_[x] = null;
74         }
75         victimKey_ = null;
76     }
77
78     public void clear()
79     {
80         invalidateCache();
81
82         delegate_.clear();
83     }
84
85     public Object JavaDoc remove(Object JavaDoc key)
86     {
87         invalidateCache();
88
89         return delegate_.remove(key);
90     }
91
92     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
93     {
94         invalidateCache();
95
96         return delegate_.put(key, value);
97     }
98
99     public Object JavaDoc[] getWithExpansion(Object JavaDoc key)
100     {
101         String JavaDoc _key = key.toString();
102         int _pos = calcPosition(_key);
103         Object JavaDoc[] _ret;
104
105         if (_key.equals(cachedKeys_[_pos]))
106         {
107             _ret = cachedValues_[_pos];
108         }
109         else if (_key.equals(victimKey_))
110         {
111             _ret = victimValue_;
112         }
113         else
114         {
115             _ret = delegate_.getWithExpansion(key);
116
117             // store old cache entry in victim cache
118
victimKey_ = cachedKeys_[_pos];
119             victimValue_ = cachedValues_[_pos];
120
121             // update cache entry
122
cachedKeys_[_pos] = _key;
123             cachedValues_[_pos] = _ret;
124         }
125
126         return _ret;
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.jacorb.notification.util.WildcardMap#getNoExpansion(java.lang.Object)
133      */

134     public Object JavaDoc getNoExpansion(Object JavaDoc key)
135     {
136         return delegate_.getNoExpansion(key);
137     }
138 }
Popular Tags