net.spy.factory
Class GenFactory<T>

java.lang.Object
  extended by net.spy.SpyObject
      extended by net.spy.factory.GenFactory<T>

public abstract class GenFactory<T>
extends SpyObject

Generic object instance cache for objects collections suitable of being stored completely in memory.


Constructor Summary
protected GenFactory(String key, long time)
          Get an instance of GenFactory.
 
Method Summary
protected  void cacheInstance(T i)
          Store or update an individual object in the cache.
protected  Storage<T> getCache()
          Get the cache for this factory.
protected abstract  Collection<T> getInstances()
          Get the collection of all Instance objects to be cached.
 long getLastRefresh()
          Get the timestamp of the last time this was refreshed.
 T getObject(int id)
          Convenience method for getObject(String,Object) assuming an integer field uniquely cached as ``id.''
 T getObject(String cacheName, Object key)
          Get an object from an alternate cache by cache name and key.
 Collection<T> getObjects()
          Get all objects cached by this factory.
 Collection<T> getObjects(String cacheName, Object key)
          Get all of the objects mapped with the given key under the given cache name.
protected  T handleNullLookup(String cacheName, Object key)
          This method is called whenever getObject(String,Object) would return null.
 void recache()
          Reset the cache for this factory.
 
Methods inherited from class net.spy.SpyObject
getLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenFactory

protected GenFactory(String key,
                     long time)
Get an instance of GenFactory.

Parameters:
key - the cache key to use
time - the refresh duration of the cache
Method Detail

getCache

protected Storage<T> getCache()
Get the cache for this factory. If the cache does not exist, getNewCacheEntry() will be called to get an uninitialized CacheEntry instance, and getInstances() will be called to get a collection of instances to populate the cache.

Returns:
a CacheEntry

cacheInstance

protected void cacheInstance(T i)
                      throws Exception
Store or update an individual object in the cache.

Parameters:
i - the object to store
Throws:
Exception - if the cache cannot be updated with this instance

getInstances

protected abstract Collection<T> getInstances()
Get the collection of all Instance objects to be cached.


handleNullLookup

protected T handleNullLookup(String cacheName,
                             Object key)
This method is called whenever getObject(String,Object) would return null. The result of this object will be used instead. Alternatively, one may throw a RuntimeException indicating a failure.

Parameters:
cacheName - the name of the cache that was accessed
key - the key under that cache that was accessed
Returns:
null

getObject

public T getObject(String cacheName,
                   Object key)
Get an object from an alternate cache by cache name and key.

Parameters:
cacheName - the name of the alt cache
key - the key under which to look
Returns:
the object instance, or null if there's no such object

getObject

public T getObject(int id)
Convenience method for getObject(String,Object) assuming an integer field uniquely cached as ``id.''

Parameters:
id - the id value
Returns:
the object stored with this id

getObjects

public Collection<T> getObjects(String cacheName,
                                Object key)
Get all of the objects mapped with the given key under the given cache name.

Parameters:
cacheName - the name of the cache
key - the key with that name
Returns:
the objects mapped to that key, or an empty string if none

getObjects

public Collection<T> getObjects()
Get all objects cached by this factory.


recache

public void recache()
Reset the cache for this factory.


getLastRefresh

public long getLastRefresh()
Get the timestamp of the last time this was refreshed.



Copyright © 1995-2007 SPY Internetworking. All Rights Reserved.