package net.puppygames.applet; import java.util.ArrayList; /** * A pool of objects for reuse * @param */ public class SimplePool implements Pool { private final Factory factory; private final int maxSize; private final ArrayList pool = new ArrayList(4); /** * C'tor * @param factory Constructs new instances when necessary * @param maxSize Maximum size this pool should grow to, or 0, for unlimited */ public SimplePool(Factory factory, int maxSize) { this.factory = factory; this.maxSize = maxSize; } @Override public T obtain() { if (pool.size() == 0) { return factory.createNew(); } else { return pool.remove(pool.size() - 1); } } @Override public void release(T obj) { if (Game.DEBUG) { if (obj == null) { throw new IllegalArgumentException("Can't release null to "+this); } if (pool.contains(obj)) { throw new IllegalStateException(obj+" already present in pool "+this); } } if (maxSize == 0 || pool.size() < maxSize) { pool.add(obj); } } }