package net.sf.jasperreports.engine.util;

import java.util.LinkedList;

public class ThreadLocalStack {
  private final ThreadLocal threadStack = new ThreadLocal();
  
  public void push(Object o) {
    LinkedList stack = this.threadStack.get();
    if (stack == null) {
      stack = new LinkedList();
      this.threadStack.set(stack);
    } 
    stack.addFirst(o);
  }
  
  public Object top() {
    Object o = null;
    LinkedList stack = this.threadStack.get();
    if (stack != null && !stack.isEmpty())
      o = stack.getFirst(); 
    return o;
  }
  
  public Object pop() {
    Object o = null;
    LinkedList stack = this.threadStack.get();
    if (stack != null)
      o = stack.removeFirst(); 
    return o;
  }
  
  public boolean empty() {
    LinkedList stack = this.threadStack.get();
    return (stack == null || stack.isEmpty());
  }
}
