cz.cuni.amis.utils.objectmanager
Class ObjectManager<ManagedObject>

java.lang.Object
  extended by cz.cuni.amis.utils.objectmanager.ObjectManager<ManagedObject>
Type Parameters:
ManagedObject -

public class ObjectManager<ManagedObject>
extends java.lang.Object

ObjectManager is a class that helps you to keep unused objects in the memory. Rather then to create the objects again and again you can retrieve their old instances. Of course those objects must be reusable and have support for resetting it's crucial variables to your liking.

Implementation is using soft references therefore you don't have to worry about memory leakage - if JVM will need more memory it will purge those soft references.

Note that the manager is thread-safe + get() will always return you an old (or possibly) new instance of the object.

We discourage you to use ObjectManager for objects that have multiple references throughout your code as it is hard to know whether the object is not referenced from other parts of your code so you can give it back to the manager.


Constructor Summary
ObjectManager(IObjectFactory<ManagedObject> objectFactory)
           
ObjectManager(IObjectFactory<ManagedObject> objectFactory, int initialObjectCount)
           
ObjectManager(IObjectFactory<ManagedObject> objectFactory, int initialObjectCount, int moreNewObjectsCount)
           
 
Method Summary
 ManagedObject get()
          Returns you an instance of object.
 void giveBack(ManagedObject obj)
          Returns an instance of object to the manager, it will be stored in the manager via soft reference.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectManager

public ObjectManager(IObjectFactory<ManagedObject> objectFactory)

ObjectManager

public ObjectManager(IObjectFactory<ManagedObject> objectFactory,
                     int initialObjectCount)
Parameters:
objectFactory -
initialObjectCount - how many objects to create in advance

ObjectManager

public ObjectManager(IObjectFactory<ManagedObject> objectFactory,
                     int initialObjectCount,
                     int moreNewObjectsCount)
Parameters:
objectFactory -
initialObjectCount - how many objects to create in advance
moreNewObjectsCount - how many new objects to create in advance in case of shortage of free objects
Method Detail

get

public ManagedObject get()
Returns you an instance of object. Note that it might be used one.

Returns:

giveBack

public void giveBack(ManagedObject obj)
Returns an instance of object to the manager, it will be stored in the manager via soft reference. Note that you have to ensure that returned objects are still usable! Also - you must be sure that this object is not used in any part of your code!

Parameters:
obj -