zinger.util.recycling
Class ObjectRecycler

java.lang.Object
  extended by zinger.util.recycling.ObjectRecycler
All Implemented Interfaces:
ObjectPool
Direct Known Subclasses:
CappedObjectRecycler

public class ObjectRecycler
extends java.lang.Object
implements ObjectPool

Basic implementation of ObjectPool interface. This implementation uses an internal collection to manage pooled instances. It is not thread-safe and does not guaranteed how large it will grow.


Field Summary
protected  ObjectGenerator generator
           
protected  java.util.List instances
          Instance cache.
 
Constructor Summary
ObjectRecycler(ObjectGenerator generator)
           
ObjectRecycler(ObjectGenerator generator, java.util.List instances)
           
 
Method Summary
 void clearCache()
          Clears existing cache.
 java.lang.Object getObject()
          Calls getObject(null).
 java.lang.Object getObject(java.lang.Object arg)
          Gets an instance either out of the cache, or a newly generated one and asks it to be prepared for reuse.
 void instantiate(int nInstances)
           
 boolean recycleObject(java.lang.Object obj)
          Puts obj into the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instances

protected final java.util.List instances
Instance cache.


generator

protected final ObjectGenerator generator
Constructor Detail

ObjectRecycler

public ObjectRecycler(ObjectGenerator generator,
                      java.util.List instances)

ObjectRecycler

public ObjectRecycler(ObjectGenerator generator)
Method Detail

instantiate

public void instantiate(int nInstances)

getObject

public final java.lang.Object getObject()
                                 throws java.lang.IllegalArgumentException
Calls getObject(null).

Specified by:
getObject in interface ObjectPool
Throws:
java.lang.IllegalArgumentException
See Also:
getObject(java.lang.Object)

getObject

public java.lang.Object getObject(java.lang.Object arg)
                           throws java.lang.IllegalArgumentException
Gets an instance either out of the cache, or a newly generated one and asks it to be prepared for reuse.

Specified by:
getObject in interface ObjectPool
Parameters:
arg - Initialization parameter. If null, default initialziation is used.
Throws:
java.lang.IllegalArgumentException
See Also:
ObjectGenerator.prepareObject(java.lang.Object, java.lang.Object)

recycleObject

public boolean recycleObject(java.lang.Object obj)
Puts obj into the cache. It is the user's responsibility to make sure only the objects that were generated this instance are returned. If getObject method encounters a bad instance from the cache, it will discard it and go on to the next one.

Specified by:
recycleObject in interface ObjectPool
Returns:
whether the object was recycled
See Also:
getObject(java.lang.Object)

clearCache

public void clearCache()
Description copied from interface: ObjectPool
Clears existing cache. This could be useful if a memory monitor was connected to this instance and ran in the background. It could know to discard cache when activity is down and memory usage is high.

Specified by:
clearCache in interface ObjectPool