01: /*
02: * The contents of this file are subject to the terms
03: * of the Common Development and Distribution License
04: * (the "License"). You may not use this file except
05: * in compliance with the License.
06: *
07: * You can obtain a copy of the license at
08: * https://jwsdp.dev.java.net/CDDLv1.0.html
09: * See the License for the specific language governing
10: * permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL
13: * HEADER in each file and include the License file at
14: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
15: * add the following below this CDDL HEADER, with the
16: * fields enclosed by brackets "[]" replaced with your
17: * own identifying information: Portions Copyright [yyyy]
18: * [name of copyright owner]
19: */
20: package com.sun.codemodel;
21:
22: /**
23: * ForEach Statement
24: * This will generate the code for statement based on the new
25: * j2se 1.5 j.l.s.
26: *
27: * @author Bhakti
28: */
29: public final class JForEach implements JStatement {
30:
31: private final JType type;
32: private final String var;
33: private JBlock body = null; // lazily created
34: private final JExpression collection;
35: private final JVar loopVar;
36:
37: public JForEach(JType vartype, String variable,
38: JExpression collection) {
39:
40: this .type = vartype;
41: this .var = variable;
42: this .collection = collection;
43: loopVar = new JVar(JMods.forVar(JMod.NONE), type, var,
44: collection);
45: }
46:
47: /**
48: * Returns a reference to the loop variable.
49: */
50: public JVar var() {
51: return loopVar;
52: }
53:
54: public JBlock body() {
55: if (body == null)
56: body = new JBlock();
57: return body;
58: }
59:
60: public void state(JFormatter f) {
61: f.p("for (");
62: f.g(type).id(var).p(": ").g(collection);
63: f.p(')');
64: if (body != null)
65: f.g(body).nl();
66: else
67: f.p(';').nl();
68: }
69:
70: }
|