Debian Codegraph @

jdbm1/src/main/java/jdbm/helper/MRU.java

raw
1 /**
2 * JDBM LICENSE v1.00
3 *
4 * Redistribution and use of this software and associated documentation
5 * ("Software"), with or without modification, are permitted provided
6 * that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain copyright
9 * statements and notices. Redistributions must also contain a
10 * copy of this document.
11 *
12 * 2. Redistributions in binary form must reproduce the
13 * above copyright notice, this list of conditions and the
14 * following disclaimer in the documentation and/or other
15 * materials provided with the distribution.
16 *
17 * 3. The name "JDBM" must not be used to endorse or promote
18 * products derived from this Software without prior written
19 * permission of Cees de Groot. For written permission,
20 * please contact [email protected].
21 *
22 * 4. Products derived from this Software may not be called "JDBM"
23 * nor may "JDBM" appear in their names without prior written
24 * permission of Cees de Groot.
25 *
26 * 5. Due credit should be given to the JDBM Project
27 * (http://jdbm.sourceforge.net/).
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
31 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
43 * Contributions are Copyright (C) 2000 by their associated contributors.
44 *
45 * $Id: MRU.java,v 1.8 2005/06/25 23:12:31 doomdark Exp $
46 */
47
48 package jdbm.helper;
49
50
51 import java.util.ArrayList;
52 import java.util.Enumeration;
53 import java.util.HashMap;
54 import java.util.Iterator;
55 import java.util.List;
56 import java.util.Map;
57
58 import jdbm.I18n;
59
60
61 /**
62 * MRU - Most Recently Used cache policy.
63 *
64 * Methods are *not* synchronized, so no concurrent access is allowed.
65 *
66 * @author <a href="mailto:[email protected]">Alex Boisvert</a>
67 */
68 public class MRU<K, V> implements CachePolicy<K, V>
69 {
70 /** Cached object Map */
71 Map<Object, CacheEntry> map = new HashMap<Object, CacheEntry>();
72
73 /**
74 * Maximum number of objects in the cache.
75 */
76 int max;
77
78 /**
79 * Beginning of linked-list of cache elements. First entry is element
80 * which has been used least recently.
81 */
82 CacheEntry first;
83
84 /**
85 * End of linked-list of cache elements. Last entry is element
86 * which has been used most recently.
87 */
88 CacheEntry last;
89
90 /**
91 * Cache eviction listeners
92 */
93 List<CachePolicyListener> listeners = new ArrayList<CachePolicyListener>();
94
95
96 /**
97 * Construct an MRU with a given maximum number of objects.
98 */
99 public MRU( int max )
100 {
101 if ( max <= 0 )
102 {
103 throw new IllegalArgumentException( I18n.err( I18n.ERR_528 ) );
104 }
105
106 this.max = max;
107 }
108
109
110 /**
111 * Place an object in the cache.
112 */
113 public void put( K key, V value ) throws CacheEvictionException
114 {
115 CacheEntry entry = map.get( key );
116
117 if ( entry != null )
118 {
119 entry.setValue( value );
120 touchEntry( entry );
121 }
122 else
123 {
124
125 if ( map.size() == max )
126 {
127 // purge and recycle entry
128 entry = purgeEntry();
129 entry.setKey( key );
130 entry.setValue( value );
131 }
132 else
133 {
134 entry = new CacheEntry( key, value );
135 }
136
137 addEntry( entry );
138 map.put( entry.getKey(), entry );
139 }
140 }
141
142
143 /**
144 * Obtain an object in the cache
145 */
146 public V get( K key )
147 {
148 CacheEntry entry = map.get( key );
149
150 if ( entry != null )
151 {
152 touchEntry( entry );
153 return ( V ) entry.getValue();
154 }
155 else
156 {
157 return null;
158 }
159 }
160
161
162 /**
163 * Remove an object from the cache
164 */
165 public void remove( K key )
166 {
167 CacheEntry entry = map.get( key );
168
169 if ( entry != null )
170 {
171 removeEntry( entry );
172 map.remove( entry.getKey() );
173 }
174 }
175
176
177 /**
178 * Remove all objects from the cache
179 */
180 public void removeAll()
181 {
182 map = new HashMap<Object, CacheEntry>();
183 first = null;
184 last = null;
185 }
186
187
188 /**
189 * Enumerate elements' values in the cache
190 */
191 public Enumeration<V> elements()
192 {
193 return new MRUEnumeration<V>( map.values().iterator() );
194 }
195
196
197 /**
198 * Add a listener to this cache policy
199 *
200 * @param listener Listener to add to this policy
201 */
202 public void addListener( CachePolicyListener listener )
203 {
204 if ( listener == null )
205 {
206 throw new IllegalArgumentException( I18n.err( I18n.ERR_539_BAD_BLOCK_ID ) );
207 }
208
209 if ( !listeners.contains( listener ) )
210 {
211 listeners.add( listener );
212 }
213 }
214
215
216 /**
217 * Remove a listener from this cache policy
218 *
219 * @param listener Listener to remove from this policy
220 */
221 public void removeListener( CachePolicyListener listener )
222 {
223 listeners.remove( listener );
224 }
225
226
227 /**
228 * Add a CacheEntry. Entry goes at the end of the list.
229 */
230 protected void addEntry( CacheEntry entry )
231 {
232 if ( first == null )
233 {
234 first = entry;
235 last = entry;
236 }
237 else
238 {
239 last.setNext( entry );
240 entry.setPrevious( last );
241 last = entry;
242 }
243 }
244
245
246 /**
247 * Remove a CacheEntry from linked list, and relink the
248 * remaining element sin the list.
249 */
250 protected void removeEntry( CacheEntry entry )
251 {
252 if ( entry == first )
253 {
254 first = entry.getNext();
255
256 if ( first != null )
257 {
258 first.setPrevious( null );
259 }
260 }
261 else if ( last == entry )
262 {
263 last = entry.getPrevious();
264
265 if ( last != null )
266 {
267 last.setNext( null );
268 }
269 }
270 else
271 {
272 entry.getPrevious().setNext( entry.getNext() );
273 entry.getNext().setPrevious( entry.getPrevious() );
274 }
275 }
276
277
278 /**
279 * Place entry at the end of linked list -- Most Recently Used
280 */
281 protected void touchEntry( CacheEntry entry )
282 {
283 if ( last == entry )
284 {
285 return;
286 }
287
288 removeEntry( entry );
289 addEntry( entry );
290 }
291
292
293 /**
294 * Purge least recently used object from the cache
295 *
296 * @return recyclable CacheEntry
297 */
298 protected CacheEntry purgeEntry() throws CacheEvictionException
299 {
300 CacheEntry entry = first;
301
302 // Notify policy listeners first. if any of them throw an
303 // eviction exception, then the internal data structure
304 // remains untouched.
305 CachePolicyListener listener;
306
307 for ( int i = 0; i < listeners.size(); i++ )
308 {
309 listener = listeners.get( i );
310 listener.cacheObjectEvicted( entry.getValue() );
311 }
312
313 removeEntry( entry );
314 map.remove( entry.getKey() );
315 entry.setValue( null );
316
317 return entry;
318 }
319
320 }
321
322 /**
323 * State information for cache entries.
324 */
325 class CacheEntry
326 {
327 private Object key;
328 private Object value;
329
330 private CacheEntry previous;
331 private CacheEntry next;
332
333
334 CacheEntry( Object key, Object value )
335 {
336 this.key = key;
337 this.value = value;
338 }
339
340
341 Object getKey()
342 {
343 return key;
344 }
345
346
347 void setKey( Object obj )
348 {
349 key = obj;
350 }
351
352
353 Object getValue()
354 {
355 return value;
356 }
357
358
359 void setValue( Object obj )
360 {
361 value = obj;
362 }
363
364
365 CacheEntry getPrevious()
366 {
367 return previous;
368 }
369
370
371 void setPrevious( CacheEntry entry )
372 {
373 previous = entry;
374 }
375
376
377 CacheEntry getNext()
378 {
379 return next;
380 }
381
382
383 void setNext( CacheEntry entry )
384 {
385 next = entry;
386 }
387 }
388
389 /**
390 * Enumeration wrapper to return actual user objects instead of
391 * CacheEntries.
392 */
393 class MRUEnumeration<V> implements Enumeration<V>
394 {
395 Iterator<CacheEntry> elements;
396
397
398 MRUEnumeration( Iterator<CacheEntry> elements )
399 {
400 this.elements = elements;
401 }
402
403
404 public boolean hasMoreElements()
405 {
406 return elements.hasNext();
407 }
408
409
410 public V nextElement()
411 {
412 CacheEntry entry = elements.next();
413
414 return ( V ) entry.getValue();
415 }
416 }