zinger.util.recycling
Class ObjectRecycler
java.lang.Object
|
+--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.
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 |
instances
protected final java.util.List instances
- Instance cache.
generator
protected final ObjectGenerator generator
ObjectRecycler
public ObjectRecycler(ObjectGenerator generator,
java.util.List instances)
ObjectRecycler
public ObjectRecycler(ObjectGenerator generator)
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
- 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
- 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