zinger.util.recycling
Class ObjectRecycler

java.lang.Object
  |
  +--zinger.util.recycling.ObjectRecycler
Direct Known Subclasses:
CappedObjectRecycler

public class ObjectRecycler
extends java.lang.Object

Allows for reuse of instances without having to constantly instantiate new ones and discard old ones. Whenever there is a high turn-over rate of certain type of objects, relying on instantiation and garbage collection can significantly slow down the system. Instantiation is a fairly expensive process and garbage collection usually doesn't happen until it has to, which means memory gets eaten away at until the last moment when everything slows down to let GC to kick in. ObjectRecycler works by simply caching instances. Instantiation and preparation of instances is delegated to an implementation of ObjectGenerator.

See Also:
instances, generator

Field Summary
protected  ObjectGenerator generator
           
protected  java.util.Stack instances
          Instance cache.
 
Constructor Summary
ObjectRecycler(ObjectGenerator generator)
           
 
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, registerNatives, toString, wait, wait, wait
 

Field Detail

instances

protected final java.util.Stack instances
Instance cache.

generator

protected final ObjectGenerator generator
Constructor Detail

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).
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.
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.
Returns:
whether the object was recycled
See Also:
getObject(java.lang.Object)

clearCache

public void clearCache()
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.