1 package com.tonbeller.wcf.controller; 2 3 import java.util.Stack ; 4 5 15 class ThreadLocalStack { 16 ThreadLocal tl = new ThreadLocal (); 17 18 public void push(Object obj) { 19 Stack stack = (Stack ) tl.get(); 20 if (stack == null) { 21 stack = new Stack (); 22 tl.set(stack); 23 } 24 stack.push(obj); 25 } 26 27 31 public Object peek(boolean failIfEmpty) throws EmptyThreadLocalStackException { 32 Stack stack = (Stack ) tl.get(); 33 if (stack == null || stack.isEmpty()) { 34 if (failIfEmpty) 35 throw new EmptyThreadLocalStackException(); 36 return null; 37 } 38 return stack.peek(); 39 } 40 41 42 public void pop() { 43 Stack stack = (Stack ) tl.get(); 44 if (stack == null || stack.isEmpty()) 45 throw new EmptyThreadLocalStackException(); 46 stack.pop(); 47 if (stack.isEmpty()) 48 tl.set(null); 49 } 50 51 } 52 | Popular Tags |