View Javadoc

1   package cz.cuni.amis.pogamut.sposh.engine;
2   
3   import java.util.ArrayList;
4   
5   /**
6    * One level in callstack.
7    */
8   class StackElement<T extends ElementExecutor> {
9   
10      public final String name;
11      public final Class clazz;
12      public final T executor;
13  
14      StackElement(Class clazz, String name, T executor) {
15          this.clazz = clazz;
16          this.name = name;
17          this.executor = executor;
18      }
19  
20      @Override
21      public String toString() {
22          return "StackElement[" + clazz.getSimpleName() + ":" + name + ":" + executor + "]";
23      }
24  
25      T getExecutor() {
26          return executor;
27      }
28  }
29  
30  /**
31   * Callstack used for storing info what was call hiearchy of elements.
32   *
33   * @author Honza
34   */
35  final class ElementStackTrace extends ArrayList<StackElement> {
36  
37      public void push(StackElement element) {
38      	add(element);
39      }
40      
41      public StackElement pop() {
42      	if (size() == 0) return null;
43      	return remove(size()-1);
44      }
45      
46      public StackElement peek() {
47      	if (size() == 0) return null;
48      	return get(size()-1);
49      }
50      
51      public void removeAllElements() {
52      	clear();
53      }
54      
55      /**
56       * Pops all elements until 'executor' is encountered. Pops 'executor' out of the stack as well.
57       * 
58       * If 'null' is passed as 'executor', removes whole stack.
59       * 
60       * @param executor
61       */
62      public void cutDownToIncluding(ElementExecutor executor) {
63      	if (executor == null) {
64      		removeAllElements();
65      		return;
66      	}
67  		while (size() != 0 && peek().getExecutor() != executor) {
68  			pop();
69  		}
70  		if (size() > 0) pop(); // pops 'executor' as well
71  	}
72      
73      /**
74       * Pops all elements until 'executor' is encountered. Leaves 'executor' on the stack.
75       * 
76       * If 'null' is passed as 'executor', removes whole stack.
77       * 
78       * @param executor
79       */
80      public void cutDownToExcluding(ElementExecutor executor) {
81      	if (executor == null) {
82      		removeAllElements();
83      		return;
84      	}
85  		while (size() != 0 && peek().getExecutor() != executor) {
86  			pop();
87  		}
88  		// leave executor on the stack
89  	}
90  }