jdbm.helper
Class MRU

java.lang.Object
  extended by jdbm.helper.MRU
All Implemented Interfaces:
CachePolicy

public class MRU
extends java.lang.Object
implements CachePolicy

MRU - Most Recently Used cache policy. Methods are *not* synchronized, so no concurrent access is allowed.

Version:
$Id: MRU.java,v 1.8 2005/06/25 23:12:31 doomdark Exp $
Author:
Alex Boisvert

Field Summary
(package private)  CacheEntry _first
          Beginning of linked-list of cache elements.
(package private)  java.util.Hashtable _hash
          Cached object hashtable
(package private)  CacheEntry _last
          End of linked-list of cache elements.
(package private)  int _max
          Maximum number of objects in the cache.
(package private)  java.util.Vector listeners
          Cache eviction listeners
 
Constructor Summary
MRU(int max)
          Construct an MRU with a given maximum number of objects.
 
Method Summary
protected  void addEntry(CacheEntry entry)
          Add a CacheEntry.
 void addListener(CachePolicyListener listener)
          Add a listener to this cache policy
 java.util.Enumeration elements()
          Enumerate elements' values in the cache
 java.lang.Object get(java.lang.Object key)
          Obtain an object in the cache
protected  CacheEntry purgeEntry()
          Purge least recently used object from the cache
 void put(java.lang.Object key, java.lang.Object value)
          Place an object in the cache.
 void remove(java.lang.Object key)
          Remove an object from the cache
 void removeAll()
          Remove all objects from the cache
protected  void removeEntry(CacheEntry entry)
          Remove a CacheEntry from linked list
 void removeListener(CachePolicyListener listener)
          Remove a listener from this cache policy
protected  void touchEntry(CacheEntry entry)
          Place entry at the end of linked list -- Most Recently Used
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_hash

java.util.Hashtable _hash
Cached object hashtable


_max

int _max
Maximum number of objects in the cache.


_first

CacheEntry _first
Beginning of linked-list of cache elements. First entry is element which has been used least recently.


_last

CacheEntry _last
End of linked-list of cache elements. Last entry is element which has been used most recently.


listeners

java.util.Vector listeners
Cache eviction listeners

Constructor Detail

MRU

public MRU(int max)
Construct an MRU with a given maximum number of objects.

Method Detail

put

public void put(java.lang.Object key,
                java.lang.Object value)
         throws CacheEvictionException
Place an object in the cache.

Specified by:
put in interface CachePolicy
Parameters:
key - key for the cached object
value - the cached object
Throws:
CacheEvictionException - propagated if, while evicting objects to make room for new object, an eviction listener encountered this problem.

get

public java.lang.Object get(java.lang.Object key)
Obtain an object in the cache

Specified by:
get in interface CachePolicy
Parameters:
key - key the object was cached under
Returns:
the object if it is still in the cache, null otherwise.

remove

public void remove(java.lang.Object key)
Remove an object from the cache

Specified by:
remove in interface CachePolicy
Parameters:
key - key the object was stored in the cache under.

removeAll

public void removeAll()
Remove all objects from the cache

Specified by:
removeAll in interface CachePolicy

elements

public java.util.Enumeration elements()
Enumerate elements' values in the cache

Specified by:
elements in interface CachePolicy

addListener

public void addListener(CachePolicyListener listener)
Add a listener to this cache policy

Specified by:
addListener in interface CachePolicy
Parameters:
listener - Listener to add to this policy

removeListener

public void removeListener(CachePolicyListener listener)
Remove a listener from this cache policy

Specified by:
removeListener in interface CachePolicy
Parameters:
listener - Listener to remove from this policy

addEntry

protected void addEntry(CacheEntry entry)
Add a CacheEntry. Entry goes at the end of the list.


removeEntry

protected void removeEntry(CacheEntry entry)
Remove a CacheEntry from linked list


touchEntry

protected void touchEntry(CacheEntry entry)
Place entry at the end of linked list -- Most Recently Used


purgeEntry

protected CacheEntry purgeEntry()
                         throws CacheEvictionException
Purge least recently used object from the cache

Returns:
recyclable CacheEntry
Throws:
CacheEvictionException


Cees de Groot (C) 2000-2001. All rights reserved http://jdbm.sourceforge.net