org.jboss.resteasy.util.ThreadLocalStack Maven / Gradle / Ivy
package org.jboss.resteasy.util;
import java.util.ArrayList;
/**
* Comment
*
* @author Bill Burke
* @version $Revision$
*/
public class ThreadLocalStack
{
private ThreadLocal> local = new ThreadLocal>();
public void push(T obj)
{
getStack(true).add(obj);
}
private ArrayList getStack(boolean create)
{
ArrayList stack = local.get();
if (stack == null && create)
{
stack = new ArrayList();
local.set(stack);
}
return stack;
}
public T get()
{
ArrayList stack = local.get();
if (stack == null || stack.isEmpty()) return null;
return stack.get(stack.size() - 1);
}
public T pop()
{
ArrayList stack = local.get();
if (stack == null || stack.isEmpty()) return null;
return stack.remove(stack.size() - 1);
}
public void setLast(T obj)
{
ArrayList stack = getStack(true);
if (stack.isEmpty())
{
stack.add(obj);
}
else
{
stack.set(stack.size() - 1, obj);
}
}
public boolean isEmpty()
{
ArrayList stack = getStack(false);
return stack == null || stack.isEmpty();
}
public int size()
{
ArrayList stack = getStack(false);
return stack == null ? 0 : stack.size();
}
public void clear()
{
local.set(null);
}
}