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