org.jcreme.caches
Class CompositeCache

java.lang.Object
  extended byorg.jcreme.caches.BaseCache
      extended byorg.jcreme.caches.CompositeCache
All Implemented Interfaces:
Cache

public class CompositeCache
extends BaseCache

This class models a Cache that is based on subcaches. All the Caches are used. There is an order of priority between the sub caches so that the results coming from one have preeminence over the next ones. The highest priority cache is used when objects are registered in this cache.

Version:
$Revision: 1.3 $
Author:
$Author: dbregeon $

Constructor Summary
CompositeCache(Cache mainSubCache)
          Creates a new instance of CompositeCache
CompositeCache(Cache[] subCaches)
          Creates a new instance of CompositeCache.
 
Method Summary
 void addSubCache(Cache cache)
          Adds a cache at the end of the list of Cache.
 void clear()
          Removes all the objects from the Cache, leaving it empty.
 java.lang.Object[] getAllObjects()
          This method gives access to the full content of the cache.
 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.
 java.util.Map getMap()
          This method enables to access to the cache contents.
 java.lang.Object getObject(java.lang.Object key)
          Gives access to an object registered in the cache.
 int getSize()
          This methods gives the current size of the cache.
 void insertSubCache(Cache cache, int position)
          Adds a cache at the given position of the list of Cache.
 void registerAllObjects(java.util.Map m)
          This is a convenience method.
 void registerObject(java.lang.Object key, java.lang.Object value)
          Registers an object in the cache using the given key to refer to it.
 void removeSubCache(Cache cache)
          Removes a cache from the list.
 void removeSubCache(int position)
          Removes a cache from the list.
 void unregisterObject(java.lang.Object key)
          Removes the object associated with the given key from the Cache.
 
Methods inherited from class org.jcreme.caches.BaseCache
addCacheListener, buildEvent, fireElementAdded, fireElementRemoved, getCacheListeners, getMaxSize, getMinSize, getReplacementPolicy, removeCacheListener, setMaxSize, setReplacementPolicy
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CompositeCache

public CompositeCache(Cache mainSubCache)
               throws java.lang.IllegalArgumentException
Creates a new instance of CompositeCache

Parameters:
mainSubCache - the main cache in the composite. It is the cache that is primarily used to register objects.
Throws:
java.lang.IllegalArgumentException - if the mainSubCache is null.

CompositeCache

public CompositeCache(Cache[] subCaches)
               throws java.lang.IllegalArgumentException
Creates a new instance of CompositeCache.

Parameters:
subCaches - the subcaches to use. The first cache in the array will be considered the main cache.
Throws:
java.lang.IllegalArgumentException - if the subCaches is null or empty.
Method Detail

clear

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

Specified by:
clear in interface Cache
Specified by:
clear in class BaseCache

getAllObjects

public java.lang.Object[] getAllObjects()
This method gives access to the full content of the cache.

Specified by:
getAllObjects in interface Cache
Specified by:
getAllObjects in class BaseCache
Returns:
an array containing all the objects stored in the cache.

getAllObjects

public 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
Specified by:
getAllObjects in class BaseCache
Parameters:
type - the type to give to the object in the result array.
Returns:
an array of objects of the type.

getMap

public java.util.Map getMap()
This method enables to access to the cache contents.

Returns:
a map of the (key,value) contents of the cache.

getObject

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

Specified by:
getObject in interface Cache
Specified by:
getObject in class BaseCache
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.

getSize

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

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

registerAllObjects

public void registerAllObjects(java.util.Map m)
                        throws CacheFullException
This is a convenience method. It enables to add to the cache all the objects present in the map.

Parameters:
m - the map that contains the objects (and keys) to use.
Throws:
CacheFullException - when the Cache would exceed its maximum size and no room can be made.

registerObject

public void registerObject(java.lang.Object key,
                           java.lang.Object value)
                    throws CacheFullException
Registers an object in the cache using the given key to refer to it.

Specified by:
registerObject in interface Cache
Specified by:
registerObject in class BaseCache
Parameters:
key - the key to use to retrieve the object.
value - the object to store in the Cache.
Throws:
CacheFullException - when the Cache would exceed its maximum size and no room can be made.

unregisterObject

public void unregisterObject(java.lang.Object key)
Removes the object associated with the given key from the Cache.

Specified by:
unregisterObject in interface Cache
Specified by:
unregisterObject in class BaseCache
Parameters:
key - the key associated to the object to be removed.

addSubCache

public void addSubCache(Cache cache)
Adds a cache at the end of the list of Cache.

Parameters:
cache - the cache to be added. Nothing happens it it is null or if it already is in the CompositeCache.

insertSubCache

public void insertSubCache(Cache cache,
                           int position)
Adds a cache at the given position of the list of Cache.

Parameters:
cache - the cache to be added. Nothing happens it it is null or if it already is in the CompositeCache.
position - in the CompositeCache. If the position is lesser than -1 or greater than the number of caches in the composite, the cache is added at the last position.

removeSubCache

public void removeSubCache(Cache cache)
Removes a cache from the list.

Parameters:
cache - the cache to be removed. Nothing happens it it is null or if it already is in the CompositeCache.

removeSubCache

public void removeSubCache(int position)
Removes a cache from the list.

Parameters:
position - the position of the cache to remove.


Copyright 2006 null. All Rights Reserved.