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:
21: package com.sun.codemodel;
22:
23: import java.util.ArrayList;
24: import java.util.List;
25:
26: /**
27: * For statement
28: */
29:
30: public class JForLoop implements JStatement {
31:
32: private List<Object> inits = new ArrayList<Object>();
33: private JExpression test = null;
34: private List<JExpression> updates = new ArrayList<JExpression>();
35: private JBlock body = null;
36:
37: public JVar init(int mods, JType type, String var, JExpression e) {
38: JVar v = new JVar(JMods.forVar(mods), type, var, e);
39: inits.add(v);
40: return v;
41: }
42:
43: public JVar init(JType type, String var, JExpression e) {
44: return init(JMod.NONE, type, var, e);
45: }
46:
47: public void init(JVar v, JExpression e) {
48: inits.add(JExpr.assign(v, e));
49: }
50:
51: public void test(JExpression e) {
52: this .test = e;
53: }
54:
55: public void update(JExpression e) {
56: updates.add(e);
57: }
58:
59: public JBlock body() {
60: if (body == null)
61: body = new JBlock();
62: return body;
63: }
64:
65: public void state(JFormatter f) {
66: f.p("for (");
67: boolean first = true;
68: for (Object o : inits) {
69: if (!first)
70: f.p(',');
71: if (o instanceof JVar)
72: f.b((JVar) o);
73: else
74: f.g((JExpression) o);
75: first = false;
76: }
77: f.p(';').g(test).p(';').g(updates).p(')');
78: if (body != null)
79: f.g(body).nl();
80: else
81: f.p(';').nl();
82: }
83:
84: }
|