J avolution v3.7 (J2SE 1.5+)

javolution.realtime
Class ObjectFactory<T>

java.lang.Object
  extended by javolution.realtime.ObjectFactory<T>
Direct Known Subclasses:
RealtimeObject.Factory

public abstract class ObjectFactory<T>
extends java.lang.Object

This class represents an object factory; it allows for object recycling and pre-allocation.

Object factories are recommended over class constructors (ref. "new" keyword) in order to benefit from context-based allocation policies. For example:

     static ObjectFactory<int[][]> BOARD_FACTORY = new ObjectFactory<int[][]>() { 
         protected int[][] create() {
             return new int[8][8];
         }
     };
     ...
     int[][] board = BOARD_FACTORY.object(); // On the stack if current thread executes 
     ...                                     // in a PoolContext; heap otherwise.
     

ObjectFactory instances are uniquely identified by their class (one instance per sub-class). The number of instances is voluntarely limited (see Javolution Configuration for details).

Version:
3.7, January 1, 2006
Author:
Jean-Marie Dautelle

Constructor Summary
protected ObjectFactory()
          Default constructor.
 
Method Summary
protected  void cleanup(T obj)
          Cleans-up this factory's objects for future reuse.
protected abstract  T create()
          Constructs a new object for this factory (using the new keyword).
 ObjectPool<T> currentPool()
          Returns the local pool for the current thread or the heapPool() when the current thread executes in a HeapContext.
 ObjectPool<T> heapPool()
          Returns the pool representing the heap; this pool always returns objects allocated on the heap.
protected  ObjectPool<T> newPool()
          Returns a new local pool for this object factory.
 T object()
          Returns a factory object possibly recycled or preallocated.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectFactory

protected ObjectFactory()
Default constructor.

Throws:
java.lang.UnsupportedOperationException - if more than one instance per factory sub-class or if the MAX number of factories has been reached.
Method Detail

create

protected abstract T create()
Constructs a new object for this factory (using the new keyword).

Returns:
a new factory object.

object

public T object()
Returns a factory object possibly recycled or preallocated. This method is equivalent to object(PoolContext.current()).

Returns:
a recycled, pre-allocated or new factory object.

currentPool

public final ObjectPool<T> currentPool()
Returns the local pool for the current thread or the heapPool() when the current thread executes in a HeapContext.

Returns:
the local pool or a pool representing the heap.

heapPool

public final ObjectPool<T> heapPool()
Returns the pool representing the heap; this pool always returns objects allocated on the heap.

Returns:
the heap pool for this factory.

cleanup

protected void cleanup(T obj)
Cleans-up this factory's objects for future reuse. When overriden, this method is called on objects being recycled to dispose of system resources or to clear references to external objects potentially on the heap (it allows these external objects to be garbage collected immediately and therefore reduces the memory footprint).

Parameters:
obj - the object product of this factory being recycled.
Throws:
java.lang.UnsupportedOperationException - if this factory does not support object clean-up (default).

newPool

protected ObjectPool<T> newPool()
Returns a new local pool for this object factory. Sub-classes may override this method in order to use specialized pools.

Returns:
a new pool for stack allocation.

J avolution v3.7 (J2SE 1.5+)

Copyright © 2006 Javolution.