A light pool of objects that can be resused to avoid allocation.
import java.util.ArrayList;
/**
* A light pool of objects that can be resused to avoid allocation.
* Based on Nathan Sweet pool implementation
*/
abstractclass Pool<T> {
privatefinal ArrayList<T> objects;
public Pool (int initCapacity) {
objects = new ArrayList<T>(initCapacity);
}
protectedabstract T getNew();
public T get() {
return objects.isEmpty() ? getNew() : objects.remove(objects.size()-1);
}
publicvoid free(T obj) {
if (!objects.contains(obj))
objects.add(obj);
}
publicvoid clear() {
objects.clear();
}
publicint size() {
return objects.size();
}
}