View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
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  }