View Javadoc
Minimize
Table

1   /*
2    * Copyright 2003-2007 the original author or authors.
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.codehaus.groovy.ast.stmt;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.codehaus.groovy.ast.GroovyCodeVisitor;
22  import org.codehaus.groovy.ast.expr.Expression;
23  
24  /**
25   * Represents a switch (object) { case value: ... case [1, 2, 3]: ...  default: ... } statement in Groovy.
26   * 
27   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
28   * @version $Revision: 15807 $
29   */
30  public class SwitchStatement extends Statement {
31  
32      private Expression expression;
33      private List<CaseStatement> caseStatements = new ArrayList<CaseStatement>();
34      private Statement defaultStatement;
35      
36  
37      public SwitchStatement(Expression expression) {
38          this(expression, EmptyStatement.INSTANCE);
39      }
40  
41      public SwitchStatement(Expression expression, Statement defaultStatement) {
42          this.expression = expression;
43          this.defaultStatement = defaultStatement;
44      }
45  
46      public SwitchStatement(Expression expression, List<CaseStatement> caseStatements, Statement defaultStatement) {
47          this.expression = expression;
48          this.caseStatements = caseStatements;
49          this.defaultStatement = defaultStatement;
50      }
51  
52      public void visit(GroovyCodeVisitor visitor) {
53          visitor.visitSwitch(this);
54      }
55      
56      public List<CaseStatement> getCaseStatements() {
57          return caseStatements;
58      }
59  
60      public Expression getExpression() {
61          return expression;
62      }
63  
64      public void setExpression(Expression e) {
65          expression=e;
66      }
67      
68      public Statement getDefaultStatement() {
69          return defaultStatement;
70      }
71  
72      public void setDefaultStatement(Statement defaultStatement) {
73          this.defaultStatement = defaultStatement;
74      }
75  
76      public void addCase(CaseStatement caseStatement) {
77          caseStatements.add(caseStatement);
78      }
79  
80      /**
81       * @return the case statement of the given index or null
82       */
83      public CaseStatement getCaseStatement(int idx) {
84          if (idx >= 0 && idx < caseStatements.size()) {
85              return caseStatements.get(idx);
86          }
87          return null;
88      }
89  }