1 | package de.uka.ipd.sdq.simucomframework.variables.stackframe; |
2 | |
3 | import java.io.Serializable; |
4 | import java.util.Stack; |
5 | |
6 | /** |
7 | * A simulated stack used by simulation threads to store their local variables |
8 | * during their execution |
9 | * @author Steffen Becker |
10 | * |
11 | * @param <T> Content-type of the stacks contents |
12 | */ |
13 | public class SimulatedStack<T> implements Serializable { |
14 | |
15 | /** |
16 | * |
17 | */ |
18 | private static final long serialVersionUID = 4131291044209793459L; |
19 | |
20 | /** |
21 | * Use a Java Stack internally |
22 | */ |
23 | Stack<SimulatedStackframe<T>> stack = new Stack<SimulatedStackframe<T>>(); |
24 | |
25 | public SimulatedStack() |
26 | { |
27 | } |
28 | |
29 | /** Add a stackframe to this stack. The frame has no parent frame. |
30 | * @return The frame added by this method |
31 | */ |
32 | public SimulatedStackframe<T> createAndPushNewStackFrame() |
33 | { |
34 | SimulatedStackframe<T> frame = new SimulatedStackframe<T>(); |
35 | stack.push(frame); |
36 | return frame; |
37 | } |
38 | |
39 | /** Add a stackframe to this stack using the given frame as parent frame |
40 | * @param parent The parent frame of the frame to create |
41 | * @return The newly created frame |
42 | */ |
43 | public SimulatedStackframe<T> createAndPushNewStackFrame(SimulatedStackframe<T> parent) |
44 | { |
45 | SimulatedStackframe<T> frame = new SimulatedStackframe<T>(parent); |
46 | stack.push(frame); |
47 | return frame; |
48 | } |
49 | |
50 | /** |
51 | * @return Topmost stackframe on this stack |
52 | */ |
53 | public SimulatedStackframe<T> currentStackFrame() |
54 | { |
55 | return stack.peek(); |
56 | } |
57 | |
58 | /** |
59 | * Pop the topmost stackframe. Called when exiting a scope |
60 | */ |
61 | public void removeStackFrame() |
62 | { |
63 | stack.pop(); |
64 | } |
65 | |
66 | /** |
67 | * @return Size of the stack |
68 | */ |
69 | public int size() { |
70 | return stack.size(); |
71 | } |
72 | |
73 | /** Add a stackframe on top of this stack. The frame already exists. |
74 | * @param copyFrame The frame to push on the stack |
75 | */ |
76 | public void pushStackFrame(SimulatedStackframe<T> copyFrame) { |
77 | stack.push(copyFrame); |
78 | } |
79 | } |