KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > generator > ast > ForLoop


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.generator.ast;
17
18 import java.util.List JavaDoc;
19
20 /**
21  * A kind of {@link Statements} that represents a <code>for</code> loop.
22  */

23 public class ForLoop implements Statements {
24
25   private final StatementsList body;
26
27   private final String JavaDoc initializer;
28
29   private String JavaDoc label;
30
31   private final String JavaDoc step;
32
33   private final String JavaDoc test;
34
35   /**
36    * Creates a {@link ForLoop#ForLoop(String,String,String,Statements)} with a
37    * null body.
38    */

39   public ForLoop(String JavaDoc initializer, String JavaDoc test, String JavaDoc step) {
40     this(initializer, test, step, null);
41   }
42
43   /**
44    * Constructs a new <code>ForLoop</code> {@link Node}.
45    *
46    * @param initializer The textual initializer {@link Expression}.
47    * @param test The textual test {@link Expression}.
48    * @param step The textual step {@link Expression}. May be <code>null</code>.
49    * @param statements The {@link Statements} for the body of the loop. May be
50    * <code>null</code>.
51    */

52   public ForLoop(String JavaDoc initializer, String JavaDoc test, String JavaDoc step,
53       Statements statements) {
54     this.initializer = initializer;
55     this.test = test;
56     this.step = step;
57     this.body = new StatementsList();
58
59     if (statements != null) {
60       body.getStatements().add(statements);
61     }
62   }
63
64   public List JavaDoc getStatements() {
65     return body.getStatements();
66   }
67
68   public void setLabel(String JavaDoc label) {
69     this.label = label;
70   }
71
72   public String JavaDoc toCode() {
73     String JavaDoc loop = "for ( " + initializer + "; " + test + "; " + step + " ) {\n"
74         + body.toCode() + "\n" + "}\n";
75
76     return label != null ? label + ": " + loop : loop;
77   }
78 }
79
Popular Tags