1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.onemind.commons.java.datastructure;
22
23 /***
24 * A stack associated with current thread
25 * @author TiongHiang Lee (thlee@onemindsoft.org)
26 * @version $Id: ThreadLocalStack.java,v 1.2 2004/08/26 12:33:16 thlee Exp $ $Name: $
27 */
28 public class ThreadLocalStack
29 {
30
31 /*** the thread local * */
32 private ThreadLocal _local = new ThreadLocal();
33
34 /***
35 * Push a local object the the thread local stack
36 * @param localObject the local object
37 * @return the size after the push
38 */
39 public int pushLocal(Object localObject)
40 {
41 return getLocalStack().pushReturnSize(localObject);
42 }
43
44 /***
45 * get the local stack
46 * @return the stack
47 */
48 public Stack getLocalStack()
49 {
50 Stack s = (Stack) _local.get();
51 if (s == null)
52 {
53 s = new Stack();
54 _local.set(s);
55 }
56 return s;
57 }
58
59 /***
60 * Get the top-most local object in local stack
61 * @return the top-most local object in local stack
62 */
63 public Object getLocal()
64 {
65 return getLocalStack().peek();
66 }
67
68 /***
69 * Pop uptil certain size in local stack
70 * @param i the size
71 */
72 public void popLocalUtil(int i)
73 {
74 getLocalStack().popUntil(i);
75 }
76
77 /***
78 * Pop the top-most local object in threadlocal stack
79 * @return the top-most local object
80 */
81 public Object popLocal()
82 {
83 return getLocalStack().pop();
84 }
85 }