View Javadoc
Minimize
Table

Bug Overview

linebug prioritybugbug descriptionexisting sinceauthor
39mediumTODODer Code ist an dieser Stelle eventuell unfertig oder fehlerhaft. Bitte überprüfen!18.06.2012-10:31Unbekannt

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.List;
20  
21  import org.codehaus.groovy.ast.ClassHelper;
22  import org.codehaus.groovy.ast.GroovyCodeVisitor;
23  
24  /**
25   * Represents a map expression [1 : 2, "a" : "b", x : y] which creates a mutable Map
26   *
27   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
28   * @version $Revision: 22367 $
29   */
30  public class MapExpression extends Expression {
31      private final List<MapEntryExpression> mapEntryExpressions;
32  
33      public MapExpression() {
34          this(new ArrayList<MapEntryExpression>());
35      }
36  
37      public MapExpression(List<MapEntryExpression> mapEntryExpressions) {
38          this.mapEntryExpressions = mapEntryExpressions;
39 bug overview            //TODO: get the type's of the expressions to specify the 
40          // map type to Map<X> if possible.
41          setType(ClassHelper.MAP_TYPE);
42      }
43  
44      public void addMapEntryExpression(MapEntryExpression expression) {
45          mapEntryExpressions.add(expression);
46      }
47  
48      public List<MapEntryExpression> getMapEntryExpressions() {
49          return mapEntryExpressions;
50      }
51  
52      public void visit(GroovyCodeVisitor visitor) {
53          visitor.visitMapExpression(this);
54      }
55  
56      public boolean isDynamic() {
57          return false;
58      }
59  
60      public Expression transformExpression(ExpressionTransformer transformer) {
61          Expression ret = new MapExpression(transformExpressions(getMapEntryExpressions(), transformer, MapEntryExpression.class));
62          ret.setSourcePosition(this);
63          return ret;
64      }
65  
66      public String toString() {
67          return super.toString() + mapEntryExpressions;
68      }
69  
70      public String getText() {
71          StringBuffer sb = new StringBuffer(32);
72          sb.append("[");
73          int size = mapEntryExpressions.size();
74          MapEntryExpression mapEntryExpression = null;
75          if (size > 0) {
76              mapEntryExpression = mapEntryExpressions.get(0);
77              sb.append(mapEntryExpression.getKeyExpression().getText() + ":" + mapEntryExpression.getValueExpression().getText());
78              for (int i = 1; i < size; i++) {
79                  mapEntryExpression = mapEntryExpressions.get(i);
80                  sb.append(", " + mapEntryExpression.getKeyExpression().getText() + ":" + mapEntryExpression.getValueExpression().getText());
81                  if (sb.length() > 120 && i < size - 1) {
82                      sb.append(", ... ");
83                      break;
84                  }
85              }
86          } else {
87              sb.append(":");
88          }
89          sb.append("]");
90          return sb.toString();
91      }
92  
93      public void addMapEntryExpression(Expression keyExpression, Expression valueExpression) {
94          addMapEntryExpression(new MapEntryExpression(keyExpression, valueExpression));
95      }
96  
97  }