package com.jxml.quick.recycle;
import java.util.*;
import java.lang.ref.*;

/**
 * Manages a pool of reusable objects.
 */
public final class QPool
{
	protected ArrayList stack=new ArrayList();

	/**
	 * The object is now available for reuse.
	 */
	public void recycle(Object obj)
	{
		stack.add(new SoftReference(obj));
	}

	/**
	 * Allocate a previously used object. If none exists, create a new one.
	 */
	public Object allocate()
	{
		while (stack.size()>0)
		{
			SoftReference sr=(SoftReference)stack.remove(stack.size()-1);
			Object obj=sr.get();
			if (obj!=null)
				return obj;
		}
		return null;
	}
}
