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

Package class diagram package ObjectManager
java.lang.Object
  extended by cz.cuni.amis.utils.objectmanager.ObjectManager<MANAGED_OBJECT>
Type Parameters:
MANAGED_OBJECT -

public class ObjectManager<MANAGED_OBJECT>
extends 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.

Author:
Jimmy

Constructor Summary
ObjectManager(IObjectFactory<MANAGED_OBJECT> objectFactory)
           
ObjectManager(IObjectFactory<MANAGED_OBJECT> objectFactory, int initialObjectCount)
           
ObjectManager(IObjectFactory<MANAGED_OBJECT> objectFactory, int initialObjectCount, int moreNewObjectsCount)
           
 
Method Summary
 MANAGED_OBJECT get()
          Returns you an instance of object.
 void giveBack(MANAGED_OBJECT 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<MANAGED_OBJECT> objectFactory)

ObjectManager

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

ObjectManager

public ObjectManager(IObjectFactory<MANAGED_OBJECT> 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 MANAGED_OBJECT get()
Returns you an instance of object. Note that it might be used one.

Returns:

giveBack

public void giveBack(MANAGED_OBJECT 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 -


Copyright © 2012 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.