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 java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.codehaus.groovy.ast.GroovyCodeVisitor;
23  
24  /**
25   * Represents a tuple expression {1, 2, 3} which creates an immutable List
26   * 
27   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
28   * @version $Revision: 15807 $
29   */
30  public class TupleExpression extends Expression {
31      private List<Expression> expressions;
32  
33      public TupleExpression() {
34          this(0);
35      }
36  
37      public TupleExpression(Expression expr) {
38          this(1);
39          addExpression(expr);
40      }
41  
42      public TupleExpression(Expression expr1, Expression expr2) {
43          this(2);
44          addExpression(expr1);
45          addExpression(expr2);
46      }
47  
48      public TupleExpression(Expression expr1, Expression expr2, Expression expr3) {
49          this(3);
50          addExpression(expr1);
51          addExpression(expr2);
52          addExpression(expr3);
53      }
54      
55      public TupleExpression(int length) {
56          this.expressions = new ArrayList<Expression>(length);
57      }
58      
59      public TupleExpression(List<Expression> expressions) {
60          this.expressions = expressions;
61      }
62      
63      public TupleExpression(Expression[] expressionArray) {
64          this();
65          expressions.addAll(Arrays.asList(expressionArray));
66      }
67  
68      public TupleExpression addExpression(Expression expression) {
69          expressions.add(expression);
70          return this;
71      }
72      
73      public List<Expression> getExpressions() {
74          return expressions;
75      }
76  
77      public void visit(GroovyCodeVisitor visitor) {
78          visitor.visitTupleExpression(this);
79      }
80  
81      public Expression transformExpression(ExpressionTransformer transformer) {
82          Expression ret = new TupleExpression(transformExpressions(getExpressions(), transformer)); 
83          ret.setSourcePosition(this);
84          return ret;
85      }
86  
87      public Expression getExpression(int i) {
88          return expressions.get(i);
89      }
90  
91      public String getText() {
92          StringBuffer buffer = new StringBuffer("(");
93          boolean first = true;
94          for (Expression expression : expressions) {
95              if (first) {
96                  first = false;
97              }
98              else {
99                  buffer.append(", ");
100             }
101             
102             buffer.append(expression.getText());
103         }
104         buffer.append(")");
105         return buffer.toString();
106     }
107 
108     public String toString() {
109         return super.toString() + expressions;
110     }
111 }