1 /*
2 * JasperReports - Free Java Reporting Library.
3 * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4 * http://www.jaspersoft.com
5 *
6 * Unless you have purchased a commercial license agreement from Jaspersoft,
7 * the following license terms apply:
8 *
9 * This program is part of JasperReports.
10 *
11 * JasperReports is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * JasperReports is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23 */
24 package net.sf.jasperreports.engine.util;
25
26 import java.util.LinkedList;
27
28
29 /**
30 * Thread local stack utility class.
31 *
32 * @author Lucian Chirita (lucianc@users.sourceforge.net)
33 */
34 public class ThreadLocalStack
35 {
36 private final ThreadLocal<LinkedList<Object>> threadStack;
37
38 public ThreadLocalStack()
39 {
40 threadStack = new ThreadLocal<LinkedList<Object>>();
41 }
42
43 public void push(Object o)
44 {
45 LinkedList<Object> stack = threadStack.get();
46 if (stack == null)
47 {
48 stack = new LinkedList<Object>();
49 threadStack.set(stack);
50 }
51 stack.addFirst(o);
52 }
53
54 public Object top()
55 {
56 Object o = null;
57 LinkedList<Object> stack = threadStack.get();
58 if (stack != null && !stack.isEmpty())
59 {
60 o = stack.getFirst();
61 }
62 return o;
63 }
64
65 public Object pop()
66 {
67 Object o = null;
68 LinkedList<Object> stack = threadStack.get();
69 if (stack != null)
70 {
71 o = stack.removeFirst();
72 }
73 return o;
74 }
75
76 public boolean empty()
77 {
78 LinkedList<Object> stack = threadStack.get();
79 return stack == null || stack.isEmpty();
80 }
81 }
82