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.expr;
17  
18  import org.codehaus.groovy.ast.ClassHelper;
19  import org.codehaus.groovy.ast.ClassNode;
20  import org.codehaus.groovy.ast.GroovyCodeVisitor;
21  
22  /**
23   * Represents a ternary expression (booleanExpression) ? expression : expression
24   * 
25   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
26   * @version $Revision: 9212 $
27   */
28  public class TernaryExpression extends Expression {
29  
30      private BooleanExpression booleanExpression;
31      private Expression trueExpression;
32      private Expression falseExpression;
33  
34      public TernaryExpression(
35          BooleanExpression booleanExpression,
36          Expression trueExpression,
37          Expression falseExpression) {
38          this.booleanExpression = booleanExpression;
39          this.trueExpression = trueExpression;
40          this.falseExpression = falseExpression;
41      }
42      public void visit(GroovyCodeVisitor visitor) {
43          visitor.visitTernaryExpression(this);
44      }
45  
46      public Expression transformExpression(ExpressionTransformer transformer) {
47          Expression ret = new TernaryExpression(
48                  (BooleanExpression) transformer.transform(booleanExpression),
49                  transformer.transform(trueExpression),
50                  transformer.transform(falseExpression)); 
51          ret.setSourcePosition(this);
52          return ret; 
53      }
54  
55      public String toString() {
56          return super.toString() +"[" + booleanExpression + " ? " + trueExpression + " : " + falseExpression + "]";
57      }
58      
59      public BooleanExpression getBooleanExpression() {
60          return booleanExpression;
61      }
62  
63      public Expression getFalseExpression() {
64          return falseExpression;
65      }
66  
67      public Expression getTrueExpression() {
68          return trueExpression;
69      }
70  
71      public String getText() {
72          return "("
73              + booleanExpression.getText()
74              + ") ? "
75              + trueExpression.getText()
76              + " : "
77              + falseExpression.getText();
78      }
79  
80      public ClassNode getType() {
81          return ClassHelper.OBJECT_TYPE;
82      }
83  }