31 * |
31 * |
32 * @author Bhakti |
32 * @author Bhakti |
33 */ |
33 */ |
34 public final class JForEach implements JStatement { |
34 public final class JForEach implements JStatement { |
35 |
35 |
36 private final JType type; |
36 private final JType type; |
37 private final String var; |
37 private final String var; |
38 private JBlock body = null; // lazily created |
38 private JBlock body = null; // lazily created |
39 private final JExpression collection; |
39 private final JExpression collection; |
40 private final JVar loopVar; |
40 private final JVar loopVar; |
41 |
41 |
42 public JForEach(JType vartype, String variable, JExpression collection) { |
42 public JForEach(JType vartype, String variable, JExpression collection) { |
43 |
43 |
44 this.type = vartype; |
44 this.type = vartype; |
45 this.var = variable; |
45 this.var = variable; |
46 this.collection = collection; |
46 this.collection = collection; |
47 loopVar = new JVar(JMods.forVar(JMod.NONE), type, var, collection); |
47 loopVar = new JVar(JMods.forVar(JMod.NONE), type, var, collection); |
48 } |
48 } |
49 |
49 |
50 |
50 |
51 /** |
51 /** |
52 * Returns a reference to the loop variable. |
52 * Returns a reference to the loop variable. |
53 */ |
53 */ |
54 public JVar var() { |
54 public JVar var() { |
55 return loopVar; |
55 return loopVar; |
56 } |
56 } |
57 |
57 |
58 public JBlock body() { |
58 public JBlock body() { |
59 if (body == null) |
59 if (body == null) |
60 body = new JBlock(); |
60 body = new JBlock(); |
61 return body; |
61 return body; |
62 } |
62 } |
63 |
63 |
64 public void state(JFormatter f) { |
64 public void state(JFormatter f) { |
65 f.p("for ("); |
65 f.p("for ("); |
66 f.g(type).id(var).p(": ").g(collection); |
66 f.g(type).id(var).p(": ").g(collection); |
67 f.p(')'); |
67 f.p(')'); |
68 if (body != null) |
68 if (body != null) |
69 f.g(body).nl(); |
69 f.g(body).nl(); |
70 else |
70 else |
71 f.p(';').nl(); |
71 f.p(';').nl(); |
72 } |
72 } |
73 |
73 |
74 } |
74 } |