1   package com.stateofflow.eclipse.metrics.calculators.localsinscope;
2   
3   import java.util.Stack;
4   
5   import com.stateofflow.eclipse.metrics.util.Counter;
6   
7   final class NumberOfLocalsInScope {
8       private final Stack<Counter> scopes = new Stack<Counter>();
9       private int max;
10  
11      public NumberOfLocalsInScope() {
12      }
13  
14      public NumberOfLocalsInScope(final NumberOfLocalsInScope toCopy) {
15          scopes.addAll(toCopy.scopes);
16          this.max = toCopy.max;
17      }
18  
19      public void startMethod() {
20          scopes.clear();
21          scopes.push(new Counter());
22          max = 0;
23      }
24  
25      public void addVariableDeclaration() {
26          peek().increment();
27      }
28  
29      private Counter peek() {
30          return scopes.peek();
31      }
32  
33      public void startNewScope() {
34          scopes.push(new Counter(peek().getValue()));
35      }
36  
37      public void endCurrentScope() {
38          max = Math.max(max, scopes.pop().getValue());
39      }
40  
41      public int getMax() {
42          return max;
43      }
44  }