KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/AbstractObjectCache.java,v 1.10 2004/07/28 09:34:29 ib Exp $
3  * $Revision: 1.10 $
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 abstract implementation.
28  *
29  * @version $Revision: 1.10 $ $Date: 2004/07/28 09:34:29 $
30  */

31 public abstract class AbstractObjectCache implements ObjectCache {
32     
33     
34     // -------------------------------------------------------------- Constants
35

36     
37     /**
38      * Default cache size.
39      */

40     protected static final int DEFAULT_SIZE = 1000;
41     
42     
43     /**
44      * Default desired hit ratio.
45      */

46     protected static final double DEFAULT_HIT_RATIO = 0.8;
47     
48     
49     // ----------------------------------------------------------- Constructors
50

51     
52     /**
53      * Constructor.
54      * <p>
55      * Warning (blinking) : That constructor is to be used really carefully
56      * as the cache maximum size is not limited.
57      */

58     public AbstractObjectCache() {
59         this(DEFAULT_SIZE, Integer.MAX_VALUE, DEFAULT_HIT_RATIO);
60     }
61     
62     
63     /**
64      * Constructor.
65      *
66      * @param initialSize Initial size of the cache
67      * @param maxSize Maximum size of the cache
68      * @param desiredHitRatio Desired cache hit ratio
69      */

70     public AbstractObjectCache(int initialSize, int maxSize,
71                                double desiredHitRatio) {
72         this.initialSize = initialSize;
73         this.maxSize = maxSize;
74         this.desiredHitRatio = desiredHitRatio;
75     }
76     
77     
78     // ----------------------------------------------------- Instance Variables
79

80     
81     /**
82      * Initial cache size.
83      */

84     protected int initialSize;
85     
86     
87     /**
88      * Desired hit ratio.
89      */

90     protected double desiredHitRatio;
91     
92     
93     /**
94      * Maximum cache size.
95      */

96     protected int maxSize;
97     
98     
99     // Cache statistics
100

101     /**
102      * Number of cache requests.
103      */

104     protected double cacheRequests;
105     
106     
107     /**
108      * Number of cache hits.
109      */

110     protected double cacheHits;
111     
112     
113     /**
114      * Current cache hit ratio. Equals to cacheHits / cacheRequests, if, and
115      * only if cacheRequests >= cache.size(). Otherwise, it's value is not
116      * used. If the cache hit ratio, is inferior to the desired hit ratio,
117      * the cache grows (unless its maximum size is already reached).
118      */

119     protected double currentHitRatio;
120     
121     
122     // ---------------------------------------------------- ObjectCache methods
123

124     
125     /**
126      * Get the object associated with the key.
127      *
128      * @param key Object's key
129      * @return Object null if there is no object associated with that key in
130      * the cache, or the object value otherwise
131      */

132     public abstract Object JavaDoc get(Object JavaDoc key);
133     
134     
135     /**
136      * Add an object to the cache, or overwrite its value.
137      *
138      * @param key Object's key
139      * @param value Object's value
140      */

141     public abstract void put(Object JavaDoc key, Object JavaDoc value);
142     
143     
144     /**
145      * Remove object associated with the given key. Doesn't do anything if the
146      * key wasn't associated with any object.
147      *
148      * @param key Object's key
149      */

150     public abstract void remove(Object JavaDoc key);
151     
152     
153     /**
154      * Clear object cache.
155      */

156     public abstract void clear();
157     
158     
159     // ------------------------------------------------------ Protected Methods
160

161     
162     /**
163      * Removes some elements from the cache. The selection of the objects to
164      * remove, along with the number are left to the implementation.
165      */

166     protected abstract void removeSomeObjects();
167     
168     
169     /**
170      * Get cache size.
171      *
172      * @return int Current cache size
173      */

174     protected abstract int getSize();
175     
176     
177     /**
178      * Resize cache.
179      */

180     protected abstract void resize();
181     
182     
183     /**
184      * Is the cache size too small ?
185      */

186     protected void shouldResize() {
187         if (cacheRequests > getSize()) {
188             currentHitRatio = cacheHits / cacheRequests;
189             if (currentHitRatio > desiredHitRatio) {
190                 resize();
191             }
192         }
193     }
194     
195     
196 }
197
Popular Tags