01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.codemodel.internal;
27:
28: import java.util.ArrayList;
29: import java.util.List;
30:
31: /**
32: * For statement
33: */
34:
35: public class JForLoop implements JStatement {
36:
37: private List<Object> inits = new ArrayList<Object>();
38: private JExpression test = null;
39: private List<JExpression> updates = new ArrayList<JExpression>();
40: private JBlock body = null;
41:
42: public JVar init(int mods, JType type, String var, JExpression e) {
43: JVar v = new JVar(JMods.forVar(mods), type, var, e);
44: inits.add(v);
45: return v;
46: }
47:
48: public JVar init(JType type, String var, JExpression e) {
49: return init(JMod.NONE, type, var, e);
50: }
51:
52: public void init(JVar v, JExpression e) {
53: inits.add(JExpr.assign(v, e));
54: }
55:
56: public void test(JExpression e) {
57: this .test = e;
58: }
59:
60: public void update(JExpression e) {
61: updates.add(e);
62: }
63:
64: public JBlock body() {
65: if (body == null)
66: body = new JBlock();
67: return body;
68: }
69:
70: public void state(JFormatter f) {
71: f.p("for (");
72: boolean first = true;
73: for (Object o : inits) {
74: if (!first)
75: f.p(',');
76: if (o instanceof JVar)
77: f.b((JVar) o);
78: else
79: f.g((JExpression) o);
80: first = false;
81: }
82: f.p(';').g(test).p(';').g(updates).p(')');
83: if (body != null)
84: f.g(body).nl();
85: else
86: f.p(';').nl();
87: }
88:
89: }
|