com.jaxfront.core.util
Class MRUHashtable

java.lang.Object
  extended by com.jaxfront.core.util.MRUHashtable
All Implemented Interfaces:
java.lang.Runnable

public class MRUHashtable
extends java.lang.Object
implements java.lang.Runnable

Instances of this class can be used as object cache. Each MCRCache has a certain capacity, the maximum number of objects the cache will hold. When the cache is full and another object is put into the cache, the cache will discard the least recently used object to get place for the new object. The cache will always hold the most recently used objects by updating its internal structure whenever an object is get from the cache or put into the cache. The cache also provides methods for getting the current cache hit rate and fill rate. Like in a hashtable, an MCRCache uses a unique key for each object.

See Also:
Hashtable

Nested Class Summary
 class MRUHashtable.CacheEntry
          For each object in the cache, there is one MCRCacheEntry object encapsulating it.
 
Field Summary
static java.util.Comparator CACHE_ENTRY_COMPARATOR
           
 
Constructor Summary
MRUHashtable(int capacity)
          Creates a new cache with a given capacity.
 
Method Summary
 void clear()
          Clears the cache by removing all entries from the cache
 java.lang.Object get(java.lang.Object key)
          Returns an object from the cache for the given key, or null if there currently is no object in the cache with this key.
 int getCapacity()
          Returns the capacity of this cache.
 int getCurrentSize()
          Returns the number of objects currently cached.
 double getFillRate()
          Returns the fill rate of this cache.
 double getHitRate()
          Returns the hit rate of this cache.
 java.util.Enumeration getKeyEntries()
           
 java.lang.Object[] getKeyObjects()
           
 int getSizeInByte()
           
 int getSlearingInterval()
           
 int getSurviveTime()
           
 boolean isEmpty()
          Returns true if this cache is empty.
 boolean isFull()
          Returns true if this cache is full.
 boolean isRunning()
           
 java.util.Set keySet()
           
static void main(java.lang.String[] args)
          A small sample program for testing this class.
 void put(java.lang.Object key, java.lang.Object obj)
          Puts an object into the cache, storing it under the given key.
 void put(java.lang.Object key, java.lang.Object obj, java.lang.Object context)
           
 void put(java.lang.Object key, java.lang.Object obj, java.lang.Object context, int surviveTime)
           
 void remove(java.lang.Object key)
          Removes an object from the cache for the given key.
 java.lang.Object returnIfUpToDat(java.lang.Object key, long time)
          Returns an object from the cache for the given key, but only if the cache entry is not older than the given timestamp.
 void run()
           
 void setCapacity(int capacity)
          Changes the capacity of this cache.
 void setPeriodicClear(boolean removePeriodic, int interval, int surviveTime)
           
 void stopPeriodicClear()
           
 java.lang.String toString()
          Returns a String containing information about cache capacity, size, current fill rate and hit rate.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CACHE_ENTRY_COMPARATOR

public static final java.util.Comparator CACHE_ENTRY_COMPARATOR
Constructor Detail

MRUHashtable

public MRUHashtable(int capacity)
Creates a new cache with a given capacity.

Parameters:
capacity - the maximum number of objects this cache will hold
Method Detail

clear

public void clear()
Clears the cache by removing all entries from the cache


get

public java.lang.Object get(java.lang.Object key)
Returns an object from the cache for the given key, or null if there currently is no object in the cache with this key.

Parameters:
key - the key for the object you want to get from this cache
Returns:
the cached object, or null

getCapacity

public int getCapacity()
Returns the capacity of this cache. This is the maximum number of objects this cache will hold at a time.

Returns:
the capacity of this cache

getCurrentSize

public int getCurrentSize()
Returns the number of objects currently cached.

Returns:
the number of objects currently cached

getFillRate

public double getFillRate()
Returns the fill rate of this cache. This is the current number of objects in the cache diveded by its capacity.

Returns:
the fill rate of this cache as double value

getHitRate

public double getHitRate()
Returns the hit rate of this cache. This is the number of successful hits divided by the total number of get requests so far. Using this ratio can help finding the appropriate cache capacity.

Returns:
the hit rate of this cache as double value

getKeyEntries

public java.util.Enumeration getKeyEntries()

getKeyObjects

public java.lang.Object[] getKeyObjects()

getSizeInByte

public int getSizeInByte()

getSlearingInterval

public int getSlearingInterval()

getSurviveTime

public int getSurviveTime()

isEmpty

public boolean isEmpty()
Returns true if this cache is empty.

Returns:
true if this cache is empty

isFull

public boolean isFull()
Returns true if this cache is full.

Returns:
true if this cache is full

isRunning

public boolean isRunning()

keySet

public java.util.Set keySet()

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
A small sample program for testing this class.

Throws:
java.lang.Exception

put

public void put(java.lang.Object key,
                java.lang.Object obj)
Puts an object into the cache, storing it under the given key. If the cache is already full, the least recently used object will be removed from the cache first. If the cache already contains an entry under the key provided, this entry is replaced.

Parameters:
key - the non-null key to store the object under
obj - the non-null object to be put into the cache

put

public void put(java.lang.Object key,
                java.lang.Object obj,
                java.lang.Object context)

put

public void put(java.lang.Object key,
                java.lang.Object obj,
                java.lang.Object context,
                int surviveTime)

remove

public void remove(java.lang.Object key)
Removes an object from the cache for the given key.

Parameters:
key - the key for the object you want to remove from this cache

returnIfUpToDat

public java.lang.Object returnIfUpToDat(java.lang.Object key,
                                        long time)
Returns an object from the cache for the given key, but only if the cache entry is not older than the given timestamp. If there currently is no object in the cache with this key, null is returned. If the cache entry is older than the timestamp, the entry is removed from the cache and null is returned.

Parameters:
key - the key for the object you want to get from this cache
time - the timestamp to check that the cache entry is up to date
Returns:
the cached object, or null

run

public void run()
Specified by:
run in interface java.lang.Runnable

setCapacity

public void setCapacity(int capacity)
Changes the capacity of this cache. This is the maximum number of objects that will be cached at a time. If the new capacity is smaller than the current number of objects in the cache, the least recently used objects will be removed from the cache.

Parameters:
capacity - the maximum number of objects this cache will hold

setPeriodicClear

public void setPeriodicClear(boolean removePeriodic,
                             int interval,
                             int surviveTime)

stopPeriodicClear

public void stopPeriodicClear()

toString

public java.lang.String toString()
Returns a String containing information about cache capacity, size, current fill rate and hit rate. Useful for testing and debugging.

Overrides:
toString in class java.lang.Object