KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > HashMapObjectCache


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/HashMapObjectCache.java,v 1.11 2004/07/28 09:34:29 ib Exp $
3  * $Revision: 1.11 $
4  * $Date: 2004/07/28 09:34:29 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.util;
25
26 /**
27  * Object cache implementation using Keith Visco's HashMap structure.
28  *
29  * @version $Revision: 1.11 $ $Date: 2004/07/28 09:34:29 $
30  * @deprecated The cache may grew beyond max size or not cache not at all.
31  * see <a HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=13142">Bug 13142</a>
32  */

33 public class HashMapObjectCache extends AbstractObjectCache {
34     
35     
36     // ----------------------------------------------------------- Constructors
37

38     
39     /**
40      * Constructor.
41      * <p>
42      * Warning (blinking) : That constructor is to be used really carefully
43      * as the cache maximum size is not limited.
44      */

45     public HashMapObjectCache() {
46         super();
47         this.cache = new HashMap(this.initialSize);
48     }
49     
50     
51     /**
52      * Constructor.
53      *
54      * @param initialSize Initial size of the cache
55      * @param maxSize Maximum size of the cache
56      * @param desiredHitRatio Desired cache hit ratio
57      */

58     public HashMapObjectCache(int initialSize, int maxSize,
59                               double desiredHitRatio) {
60         super(initialSize, maxSize, desiredHitRatio);
61         this.cache = new HashMap(this.initialSize / 3);
62     }
63     
64     
65     // ----------------------------------------------------- Instance Variables
66

67     
68     /**
69      * Cache structure.
70      */

71     protected HashMap cache;
72     
73     
74     // ---------------------------------------------------- ObjectCache methods
75

76     
77     /**
78      * Get the object associated with the key.
79      *
80      * @param key Object's key
81      * @return Object null if there is no object associated with that key in
82      * the cache, or the object value otherwise
83      */

84     public Object JavaDoc get(Object JavaDoc key) {
85         synchronized (cache) {
86             Object JavaDoc result = cache.get(key);
87             this.cacheRequests += 1;
88             
89             if (result != null) {
90                 this.cacheHits += 1;
91             } else {
92                 shouldResize();
93             }
94             return result;
95         }
96     }
97     
98     
99     /**
100      * Add an object to the cache, or overwrite its value.
101      *
102      * @param key Object's key
103      * @param value Object's value
104      */

105     public void put(Object JavaDoc key, Object JavaDoc value) {
106         synchronized (cache) {
107             cache.put(key, value);
108         }
109     }
110     
111     
112     /**
113      * Remove object associated with the given key. Doesn't do anything if the
114      * key wasn't associated with any object.
115      *
116      * @param key Object's key
117      */

118     public void remove(Object JavaDoc key) {
119         synchronized (cache) {
120             cache.remove(key);
121         }
122     }
123     
124     
125     /**
126      * Clear object cache.
127      */

128     public void clear() {
129         synchronized (cache) {
130             cache.clear();
131         }
132     }
133     
134     
135     // ------------------------------------------------------ Protected Methods
136

137     
138     /**
139      * Removes some elements from the cache. The selection of the objects to
140      * remove, along with the number are left to the implementation.
141      */

142     protected void removeSomeObjects() {
143         clear();
144     }
145     
146     
147     /**
148      * Get cache size.
149      *
150      * @return int Current cache size
151      */

152     protected int getSize() {
153         return cache.size();
154     }
155     
156     
157     /**
158      * Resize cache.
159      */

160     protected void resize() {
161         synchronized (cache) {
162             if (getSize() < maxSize) {
163                 int size = getSize();
164                 cache.clear();
165                 cache = new HashMap(size / 3 * 2);
166             }
167         }
168     }
169     
170     
171 }
172
Popular Tags