1 22 package org.jboss.ejb3; 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 33 public class ThreadLocalStack<T> 34 { 35 private ThreadLocal <ArrayList <T>> stack = new ThreadLocal <ArrayList <T>>(); 36 37 public void push(T obj) 38 { 39 ArrayList <T> list = stack.get(); 40 if (list == null) 41 { 42 list = new ArrayList <T>(1); 43 stack.set(list); 44 } 45 list.add(obj); 46 } 47 48 public T pop() 49 { 50 ArrayList <T> list = stack.get(); 51 if (list == null) 52 { 53 return null; 54 } 55 T rtn = list.remove(list.size() - 1); 56 if (list.size() == 0) 57 { 58 stack.set(null); 59 list.clear(); 60 } 61 return rtn; 62 } 63 64 public T get() 65 { 66 ArrayList <T> list = (ArrayList <T>)stack.get(); 67 if (list == null) 68 { 69 return null; 70 } 71 return list.get(list.size() - 1); 72 } 73 74 public List <T> getList() 75 { 76 return stack.get(); 77 } 78 } 79 | Popular Tags |