01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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 initializer;
28:
29: private String label;
30:
31: private final String step;
32:
33: private final String test;
34:
35: /**
36: * Creates a {@link ForLoop#ForLoop(String,String,String,Statements)} with a
37: * null body.
38: */
39: public ForLoop(String initializer, String test, String 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 initializer, String test, String 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<Statements> getStatements() {
65: return body.getStatements();
66: }
67:
68: public void setLabel(String label) {
69: this .label = label;
70: }
71:
72: public String toCode() {
73: String loop = "for ( " + initializer + "; " + test + "; "
74: + step + " ) {\n" + body.toCode() + "\n" + "}\n";
75:
76: return label != null ? label + ": " + loop : loop;
77: }
78: }
|