qflib 0.98.0

de.qfs.lib.util
Class MRUCache

java.lang.Object
  |
  +--de.qfs.lib.util.MRUCache

public class MRUCache
extends java.lang.Object

This is a cache that stores only a certain number of resources. If more resources are added, only the most recently used ones are kept.

The MRUCache is based on a MultiMap, so it can store different kinds of resources distinguished by a key. The size limit of the cache applies to the total number of resorces stored independent of their type.

When a resource is retrieved with get, it is automatically removed from the MRUCache.

If you just want to store resources of one kind, there is a simplified interface of get() and put(Object) where you don't need to provide a key.

You cannot store the same resource more than once, not even using different keys.

Since:
0.98.0
Version:
$Revision: 1.1 $
Author:
Gregor Schmid

Inner Class Summary
static class MRUCache.UnitTest
          Test cases for the MRUCache class.
 
Constructor Summary
MRUCache(int size)
          Create a new MRUCache.
 
Method Summary
 void addMRUCacheListener(MRUCacheListener listener)
          Add a listener for dropped resources to the MRUCache.
protected  void fireResourceDropped(java.lang.Object key, java.lang.Object resource)
          Notify all listeners about a resource dropped from the cache.
 java.lang.Object get()
          Get a resource from the MRUCache.
 java.lang.Object get(java.lang.Object key)
          Get a resource from the MRUCache.
 int getCurrentSize()
          Query the current size of the MRUCache.
 int getMaximumSize()
          Query the maximum size of the MRUCache.
 void put(java.lang.Object resource)
          Store a resource in the MRUCache.
 void put(java.lang.Object key, java.lang.Object resource)
          Store a resource in the MRUCache.
 void removeMRUCacheListener(MRUCacheListener listener)
          Remove a listener for dropped resources from the MRUCache.
 void setMaximumSize(int size)
          Set the maximum size of the MRUCache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MRUCache

public MRUCache(int size)
Create a new MRUCache.
Parameters:
size - The maximum number of resources to keep.
Method Detail

get

public java.lang.Object get()
Get a resource from the MRUCache.
Returns:
An arbitrary resource or null if none is available.

get

public java.lang.Object get(java.lang.Object key)
Get a resource from the MRUCache.
Parameters:
key - The key for the resource.
Returns:
An arbitrary resource stored for that key or null if none is available.

put

public void put(java.lang.Object resource)
Store a resource in the MRUCache.
Parameters:
resource - The resource to store.

put

public void put(java.lang.Object key,
                java.lang.Object resource)
Store a resource in the MRUCache.
Parameters:
key - The key to store the resource under.
resource - The resource to store.

getMaximumSize

public int getMaximumSize()
Query the maximum size of the MRUCache.
Returns:
The maximum number of resources stored by this cache.

setMaximumSize

public void setMaximumSize(int size)
Set the maximum size of the MRUCache.
Parameters:
size - The maximum number of resources allowed for this cache.

getCurrentSize

public int getCurrentSize()
Query the current size of the MRUCache.
Returns:
The current number of resources stored by this cache.

addMRUCacheListener

public void addMRUCacheListener(MRUCacheListener listener)
Add a listener for dropped resources to the MRUCache.
Parameters:
listener - The listener to add.

removeMRUCacheListener

public void removeMRUCacheListener(MRUCacheListener listener)
Remove a listener for dropped resources from the MRUCache.
Parameters:
listener - The listener to remove.

fireResourceDropped

protected void fireResourceDropped(java.lang.Object key,
                                   java.lang.Object resource)
Notify all listeners about a resource dropped from the cache.
Parameters:
key - The key the resource was stored under.
resource - The dropped resource.

qflib 0.98.0