1 /**
2 * Copyright 2011 meltmedia
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.xchain.framework.util;
17
18 import java.util.ArrayList;
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.NoSuchElementException;
22
23 /**
24 * A stack that has a different state for each thread that uses it.
25 *
26 * @author Christian Trimble
27 * @author Devon Tackett
28 */
29 public class ThreadLocalStack<E>
30 {
31 protected ThreadLocal<LinkedList<E>> stackThreadLocal = new ThreadLocal<LinkedList<E>>();
32
33 /**
34 * Pushes an item onto the top of the stack.
35 *
36 * @param item the item to push onto the stack.
37 */
38 public void push(E item)
39 {
40 LinkedList<E> stack = stackThreadLocal.get();
41
42 if( stack == null ) {
43 stack = new LinkedList<E>();
44 stackThreadLocal.set(stack);
45 }
46
47 stack.addFirst(item);
48 }
49
50 /**
51 * Pops the top element off of the stack and returns it.
52 *
53 * @return the top element from the stack.
54 * @throws NoSuchElementException if the stack is empty.
55 */
56 public E pop()
57 {
58 LinkedList<E> stack = stackThreadLocal.get();
59
60 if( stack == null ) {
61 throw new NoSuchElementException("pop() called on an empty stack.");
62 }
63
64 // if the stack is empty, remove the thread local.
65 if( stack.size() == 1 ) {
66 stackThreadLocal.set(null);
67 }
68
69 return stack.removeFirst();
70 }
71
72 /**
73 * Peeks at the element on top of the stack. This call is the same as peek(0).
74 *
75 * @return the element on the top of the stack.
76 * @throws NoSuchElementException if the stack is empty.
77 */
78 public E peek()
79 {
80 return peek(0);
81 }
82
83 /**
84 * Peeks at an element in the stack.
85 *
86 * @param depth the depth of the element to peek at.
87 * @return the element at depth in the stack.
88 * @throws NoSuchElementException if there is not an element at depth in the stack.
89 */
90 public E peek( int depth )
91 {
92 LinkedList<E> stack = stackThreadLocal.get();
93
94 if( stack == null || stack.size() <= depth ) {
95 throw new NoSuchElementException("peek() called on element that is not in the stack.");
96 }
97
98 return stack.get(depth);
99 }
100
101 /**
102 * Returns the size of the stack.
103 *
104 * @return the size of the stack.
105 */
106 public int size()
107 {
108 LinkedList<E> stack = stackThreadLocal.get();
109
110 if( stack == null ) {
111 return 0;
112 }
113
114 return stack.size();
115 }
116
117 /**
118 * Returns true if the stack for this thread is empty, false otherwise.
119 */
120 public boolean isEmpty()
121 {
122 return stackThreadLocal.get() == null;
123 }
124
125 /**
126 * Clears the stack.
127 */
128 public void clear()
129 {
130 stackThreadLocal.set(null);
131 }
132
133 /**
134 * Return a copy of the current threads stack as a list, with the top of the stack at index 0.
135 */
136 public List<E> toList()
137 {
138 if( stackThreadLocal.get() == null ) {
139 return new ArrayList<E>();
140 }
141 else {
142 return new ArrayList<E>(stackThreadLocal.get());
143 }
144 }
145 }