package commons.application.core;
import java.util.Stack;
import commons.application.ctx.Context;
import basics.application.Binder;
/**
 * A stack serving as cache for name bound beans that can be reused. The cache can only hold one
 * type of objects!
 * @author kst
 */
public class BoundBeanCache<T>
{

Stack<T>       stack = new Stack<T>();
private String bindingName;

/**
 * The binding name is the name to which the bean type in question is bound to.
 */
public BoundBeanCache(String bindingName)
{
   this.bindingName = bindingName;
}

@SuppressWarnings("unchecked")
public T get (final Context ctx)
{
   T matcher = null;
   synchronized (stack)
   {
      if (stack.empty())
         matcher = (T) Binder.createNew(bindingName);
      else
         matcher = stack.pop();
   }
   return matcher;
}

public void put (T matcher)
{
   synchronized (stack)
   {
      matcher = stack.push(matcher);
   }
}
public void clear()
{
   stack.clear();
}
}
