org.jcreme.caches
Class BaseCache

java.lang.Object
  extended byorg.jcreme.caches.BaseCache
All Implemented Interfaces:
Cache
Direct Known Subclasses:
AggregateCache, CompositeCache, HashCache, LUFOCache

public abstract class BaseCache
extends java.lang.Object
implements Cache

This abstract cache is the base for policied caches. The policy element is not enforced in this class. It is up to the subclasses to enforce the policies.

Version:
$Revision: 1.2 $
Author:
$Author: dbregeon $

Constructor Summary
protected BaseCache()
          Creates new Cache
protected BaseCache(CachePolicy policy)
          Creates new Cache
protected BaseCache(int minSize, int maxSize, CachePolicy policy)
          Creates new Cache
 
Method Summary
 void addCacheListener(CacheListener listener)
          To add a CacheListener for this cache.
protected  CacheEvent buildEvent(java.lang.Object element)
          Convenience method to build events when the cache is modified.
abstract  void clear()
          Removes all the objects from the Cache, leaving it empty.
protected  void fireElementAdded(CacheEvent evt)
          Convenience method to fire events when the cache is modified.
protected  void fireElementRemoved(CacheEvent evt)
          Convenience method to fire events when the cache is modified.
abstract  java.lang.Object[] getAllObjects()
          Gives access to all the objects stored in the Cache.
abstract  java.lang.Object[] getAllObjects(java.lang.Class type)
          This method gives the same result as the getAllObjects method but the objects in the result array are types with the parameter type.
 CacheListener[] getCacheListeners()
          Gives access to the list of listeners that listen to this cache.
 int getMaxSize()
          Gives access to the Cache's maximum size.
protected  int getMinSize()
          Gives access to the Cache's minimum size.
abstract  java.lang.Object getObject(java.lang.Object key)
          Gives access to an object registered in the cache.
 CachePolicy getReplacementPolicy()
          Enables access to the policy currently used by the cache.
abstract  int getSize()
          This methods gives the current size of the cache.
abstract  void registerObject(java.lang.Object key, java.lang.Object value)
          This method adds an object in the cache.
 void removeCacheListener(CacheListener listener)
          To remove a CacheListener for this cache.
 void setMaxSize(int maxSize)
          This method modifies the maximum size of the cache.
protected  void setReplacementPolicy(CachePolicy policy)
          Enables to modify the policy of this Cache.
abstract  void unregisterObject(java.lang.Object key)
          This method removes an object from the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jcreme.caches.Cache
getMap, registerAllObjects
 

Constructor Detail

BaseCache

protected BaseCache()
Creates new Cache


BaseCache

protected BaseCache(CachePolicy policy)
Creates new Cache

Parameters:
policy - the policy used by this cache.

BaseCache

protected BaseCache(int minSize,
                    int maxSize,
                    CachePolicy policy)
Creates new Cache

Parameters:
minSize - the initial size of the cache (number of objects).
maxSize - the maximum size of the cache (number of objects).
policy - the policy of this cache.
Method Detail

setMaxSize

public void setMaxSize(int maxSize)
This method modifies the maximum size of the cache.

Specified by:
setMaxSize in interface Cache
Parameters:
maxSize - the maximum number of objects that can be stored in the cache.

getReplacementPolicy

public CachePolicy getReplacementPolicy()
Enables access to the policy currently used by the cache.

Specified by:
getReplacementPolicy in interface Cache
Returns:
the policy of the cache.

setReplacementPolicy

protected void setReplacementPolicy(CachePolicy policy)
Enables to modify the policy of this Cache.

Parameters:
policy - the new policy for the Cache.

registerObject

public abstract void registerObject(java.lang.Object key,
                                    java.lang.Object value)
                             throws CacheFullException
This method adds an object in the cache.

Specified by:
registerObject in interface Cache
Parameters:
key - the key that will be used to retrieve the object.
value - the object to store in the cache.
Throws:
CacheFullException - if the object could not be stored in the cache.

unregisterObject

public abstract void unregisterObject(java.lang.Object key)
This method removes an object from the cache.

Specified by:
unregisterObject in interface Cache
Parameters:
key - the key that was used to store the object in the cache.

getObject

public abstract java.lang.Object getObject(java.lang.Object key)
Gives access to an object registered in the cache.

Specified by:
getObject in interface Cache
Parameters:
key - the key used to register the searched object.
Returns:
the object stored in the cache. Null if no object was stored with this key.

getMaxSize

public int getMaxSize()
Gives access to the Cache's maximum size.

Specified by:
getMaxSize in interface Cache
Returns:
the maximum size for the Cache.

getMinSize

protected int getMinSize()
Gives access to the Cache's minimum size.

Returns:
the minimum size for the Cache.

addCacheListener

public void addCacheListener(CacheListener listener)
To add a CacheListener for this cache.

Specified by:
addCacheListener in interface Cache
Parameters:
listener - a cache listener. It is silently ignored if it is null.

removeCacheListener

public void removeCacheListener(CacheListener listener)
To remove a CacheListener for this cache.

Specified by:
removeCacheListener in interface Cache
Parameters:
listener - a cache listener. It is silently ignored if it is null.

fireElementRemoved

protected void fireElementRemoved(CacheEvent evt)
Convenience method to fire events when the cache is modified.

Parameters:
evt - a cache event.

fireElementAdded

protected void fireElementAdded(CacheEvent evt)
Convenience method to fire events when the cache is modified.

Parameters:
evt - a cache event.

buildEvent

protected CacheEvent buildEvent(java.lang.Object element)
Convenience method to build events when the cache is modified.

Parameters:
element - the element that changed in the cache.
Returns:
the event built from element.

getAllObjects

public abstract java.lang.Object[] getAllObjects()
Gives access to all the objects stored in the Cache.

Specified by:
getAllObjects in interface Cache
Returns:
all the contents of the Cache.

clear

public abstract void clear()
Removes all the objects from the Cache, leaving it empty.

Specified by:
clear in interface Cache

getSize

public abstract int getSize()
This methods gives the current size of the cache.

Specified by:
getSize in interface Cache
Returns:
the number of objects currently stored in the cache.

getAllObjects

public abstract java.lang.Object[] getAllObjects(java.lang.Class type)
This method gives the same result as the getAllObjects method but the objects in the result array are types with the parameter type.

Specified by:
getAllObjects in interface Cache
Parameters:
type - the type to give to the object in the result array.
Returns:
an array of objects of the type.

getCacheListeners

public CacheListener[] getCacheListeners()
Gives access to the list of listeners that listen to this cache.

Specified by:
getCacheListeners in interface Cache
Returns:
the listeners of this Cache.


Copyright 2006 null. All Rights Reserved.