|
JADE v6.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.dautelle.realtime.ObjectFactory
This class represents an object factory. How and when a factory allocates
its objects depends upon its real-time context. The default context
(HeapContext
) allocates objects on-demand (from the heap)
and recycling is done through garbage collection.
Object factories should be used preferably to class constructors
(ref. "new" keyword) to allow for "stack" allocation when the current
thread executes in a PoolContext
:
// UnliketoString()
the following avoids heap allocation. public CharSequence toChars() { StringBuffer sb = (StringBuffer) STRING_BUFFER_FACTORY.object(); ... // Formats this object into sb ... // (e.g. usingTypeFormat
) return sb; // On the "stack" when executing in aPoolContext
. } static final ObjectFactory STRING_BUFFER_FACTORY = new ObjectFactory() { public Object create() { return new StringBuffer(26); } };
It is also possible to cleanup
factories' objects for
future reuse (e.g. to release system resources or to clear external
references immediately after usage):
static final ObjectFactory FACTORY = new ObjectFactory() { public Object create() { return new ArrayList(256); } public void cleanup(Object obj) { // Ensures that list objects can be garbage collected. ((ArrayList)obj).clear(); } };
Finally, this class serves to the implementation of Realtime
base classes such as RealtimeObject
or
RealtimeNumber
(see implementation code for details).
The number of instances of this class is voluntarely limited (see
JADE's
Configuration for details). Instances of this class should be either
static
or member of persistent objects.
Field Summary | |
static int |
MAX
Holds the maximum number of ObjectFactory (system property
"jade.factories" , default 1024 ). |
Constructor Summary | |
protected |
ObjectFactory()
Default constructor. |
Method Summary | |
void |
cleanup(java.lang.Object obj)
Cleans-up this factory's objects for future reuse. |
abstract java.lang.Object |
create()
Allocates a new object from this factory on the heap (using the new keyword). |
ObjectPool |
currentPool()
Returns the pool for the current thread. |
ObjectPool |
heapPool()
Returns a pool from this factory which represents the heap. |
protected ObjectPool |
newPool()
Creates a new pool for this object factory. |
java.lang.Object |
object()
Returns a new or recycled object from this factory. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int MAX
ObjectFactory
(system property
"jade.factories"
, default 1024
).
Constructor Detail |
protected ObjectFactory()
java.lang.UnsupportedOperationException
- if already MAX
factories exist.Method Detail |
public abstract java.lang.Object create()
new
keyword).
public java.lang.Object object()
this.currentPool().next()
public void cleanup(java.lang.Object obj)
Note: Whether or not this method should be overriden depends mostly on the association types that the factory's objects have with other objects. For composition associations (containment per value), there is usually no need to cleanup. For other types of relationship cleaning up is recommended.
obj
- the object product of this factory being recycled.
java.lang.UnsupportedOperationException
- if this factory does not
support object clean-up (default).public final ObjectPool currentPool()
PoolContext
, then heapPool()
is returned.
heapPool()
when the
current thread executes in a HeapContext
.public final ObjectPool heapPool()
create()
.
protected ObjectPool newPool()
ObjectPool
.
|
JADE v6.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |