public abstract class ArrayFactory<T>
extends java.lang.Object
This class holds factories to produces arrays of variable length.
It allows for object recycling, pre-allocation and stack
allocations:
// Primitive types.
char[] buffer = ArrayFactory.CHARS_FACTORY.array(1024); // Possibly recycled.
for (int i = reader.read(buffer, 0, buffer.length); i > 0;) {
...
}
ArrayFactory.CHARS_FACTORY.recycle(buffer); //
// Custom types.
static ArrayFactory<Vertex[]> VERTICES_FACTORY = new ArrayFactory<Vertex[]> {
protected Vertex[] create(int size) {
return new Vertex[size];
}
};
...
Vertex[] vertices = VERTICES_FACTORY.array(256);
Modifier and Type | Field and Description |
---|---|
static ArrayFactory<boolean[]> |
BOOLEANS_FACTORY
Holds factory for
boolean arrays. |
static ArrayFactory<byte[]> |
BYTES_FACTORY
Holds factory for
byte arrays. |
static ArrayFactory<char[]> |
CHARS_FACTORY
Holds factory for
char arrays. |
static ArrayFactory<double[]> |
DOUBLES_FACTORY
Holds factory for
double arrays. |
static ArrayFactory<float[]> |
FLOATS_FACTORY
Holds factory for
float arrays. |
static ArrayFactory<int[]> |
INTS_FACTORY
Holds factory for
int arrays. |
static ArrayFactory<long[]> |
LONGS_FACTORY
Holds factory for
long arrays. |
static ArrayFactory<java.lang.Object[]> |
OBJECTS_FACTORY
Holds factory for generic
Object arrays. |
static ArrayFactory<short[]> |
SHORTS_FACTORY
Holds factory for
short arrays. |
Constructor and Description |
---|
ArrayFactory()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
T |
array(int capacity)
Returns an array possibly recycled or preallocated of specified
minimum size.
|
protected abstract T |
create(int size)
Constructs a new array of specified size from this factory
(using the
new keyword). |
void |
recycle(T array)
Recycles the specified arrays.
|
public static final ArrayFactory<boolean[]> BOOLEANS_FACTORY
boolean
arrays.public static final ArrayFactory<byte[]> BYTES_FACTORY
byte
arrays.public static final ArrayFactory<char[]> CHARS_FACTORY
char
arrays.public static final ArrayFactory<short[]> SHORTS_FACTORY
short
arrays.public static final ArrayFactory<int[]> INTS_FACTORY
int
arrays.public static final ArrayFactory<long[]> LONGS_FACTORY
long
arrays.public static final ArrayFactory<float[]> FLOATS_FACTORY
float
arrays.public static final ArrayFactory<double[]> DOUBLES_FACTORY
double
arrays.public static final ArrayFactory<java.lang.Object[]> OBJECTS_FACTORY
Object
arrays.public final T array(int capacity)
capacity
- the minimum size of the array to be returned.public void recycle(T array)
array
- the array to be recycled.protected abstract T create(int size)
new
keyword).size
- the size of the array.Copyright © 2005 - 2007 Javolution.