|
J avolution v5.2 (J2SE 1.5+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavolution.context.ObjectFactory<T>
public abstract class ObjectFactory<T>
This class represents an object factory; it allows for object recycling, pre-allocation and stack allocations.
Object factories are recommended over class constructors (ref. "new"
keyword) to allows for custom allocation policy (see
AllocatorContext
). For example:
static ObjectFactory<int[][]> BOARD_FACTORY = new ObjectFactory<int[][]>() {
protected int[][] create() {
return new int[8][8];
}
};
...
int[][] board = BOARD_FACTORY.object();
// The board object might have been preallocated at start-up,
// it might also be on the thread "stack/pool" for threads
// executing in a StackContext.
...
BOARD_FACTORY.recycle(board); // Immediate recycling of the board object (optional).
For arrays of variable length ArrayFactory
is recommended.
For convenience, this class provides a static getInstance(java.lang.Class
method
to retrieve a factory implementation for any given class.
For example:
ObjectFactory<ArrayList> listFactory = ObjectFactory.getInstance(ArrayList.class);
ArrayList list = listFactory.object();
... // Do something.
listFactory.recycle(list); // Optional.
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). |
|
Allocator<T> |
currentAllocator()
Returns the factory allocator for the current thread (equivalent to AllocatorContext.current().getAllocator(this) ). |
|
protected boolean |
doCleanup()
Indicates if this factory requires cleanup. |
|
static
|
getInstance(java.lang.Class<T> forClass)
Returns a factory implementation producing instances of the specified class. |
|
T |
object()
Returns a factory object possibly recycled or preallocated. |
|
void |
recycle(T obj)
Recycles the specified object. |
|
static
|
setInstance(ObjectFactory<T> factory,
java.lang.Class<T> forClass)
Sets explicitely the factory to be used for the specified class (see getInstance(java.lang.Class ). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected ObjectFactory()
Method Detail |
---|
public static <T> ObjectFactory<T> getInstance(java.lang.Class<T> forClass)
set explicitly
:
class LogContext {
public static final Class<LogContext> NULL = Null.class;
...
private static class Null extends LogContext ... // Private.
static {
// Allows Null instances to be factory produced (even so the class is not accessible).
ObjectFactory.setInstance(new ObjectFactory<Null> {
protected Null create() { return new Null() }},
Null.class);
}
}
forClass
- the class for which an object factory is returned.
public static <T> void setInstance(ObjectFactory<T> factory, java.lang.Class<T> forClass)
getInstance(java.lang.Class)
).
factory
- the factory to use.forClass
- the associated class.getInstance(Class)
public final T object()
currentAllocator().nextInQueue()
.
public final void recycle(T obj)
getAllocator().recycle(obj)
.
obj
- the object to be recycled.public final Allocator<T> currentAllocator()
AllocatorContext.current().getAllocator(this)
).
protected abstract T create()
new
keyword).
protected void cleanup(T obj)
static ObjectFactory<ArrayList> ARRAY_LIST_FACTORY = new ObjectFactory<ArrayList>() {
protected ArrayList create() {
return new ArrayList();
}
protected void cleanup(ArrayList obj) {
obj.clear(); // Clears external references.
}
};
obj
- the factory object being recycled.protected final boolean doCleanup()
true
if cleanup(T)
is overriden and
cleanup(T)
has been called at least once;
false
otherwise.
|
J avolution v5.2 (J2SE 1.5+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |