1 /**
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
5 * Portions Copyright 2013-2016 Philip Helger + contributors
6 *
7 * The contents of this file are subject to the terms of either the GNU
8 * General Public License Version 2 only ("GPL") or the Common Development
9 * and Distribution License("CDDL") (collectively, the "License"). You
10 * may not use this file except in compliance with the License. You can
11 * obtain a copy of the License at
12 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
13 * or packager/legal/LICENSE.txt. See the License for the specific
14 * language governing permissions and limitations under the License.
15 *
16 * When distributing the software, include this License Header Notice in each
17 * file and include the License file at packager/legal/LICENSE.txt.
18 *
19 * GPL Classpath Exception:
20 * Oracle designates this particular file as subject to the "Classpath"
21 * exception as provided by Oracle in the GPL Version 2 section of the License
22 * file that accompanied this code.
23 *
24 * Modifications:
25 * If applicable, add the following below the License Header, with the fields
26 * enclosed by brackets [] replaced by your own identifying information:
27 * "Portions Copyright [year] [name of copyright owner]"
28 *
29 * Contributor(s):
30 * If you wish your version of this file to be governed by only the CDDL or
31 * only the GPL Version 2, indicate your decision by adding "[Contributor]
32 * elects to include this software in this distribution under the [CDDL or GPL
33 * Version 2] license." If you don't indicate a single choice of license, a
34 * recipient has the option to distribute your version of this file under
35 * either the CDDL, the GPL Version 2 or to extend the choice of license to
36 * its licensees as provided above. However, if you add GPL Version 2 code
37 * and therefore, elected the GPL Version 2 license, then the option applies
38 * only if the new code is made subject to such option by the copyright
39 * holder.
40 */
41 package com.helger.jcodemodel;
42
43 import javax.annotation.Nonnull;
44
45 /**
46 * ForEach Statement This will generate the code for statement based on the new
47 * j2se 1.5 j.l.s.
48 *
49 * @author Bhakti
50 */
51 public class JForEach implements IJStatement
52 {
53 private final AbstractJType m_aType;
54 private final String m_sVarName;
55 private JBlock m_aBody; // lazily created
56 private final IJExpression m_aCollection;
57 private final JVar m_aLopVar;
58
59 protected JForEach (@Nonnull final AbstractJType aVarType,
60 @Nonnull final String sVarName,
61 @Nonnull final IJExpression aCollection)
62 {
63 if (aVarType == null)
64 throw new NullPointerException ("Variable Type");
65 if (sVarName == null)
66 throw new NullPointerException ("Variable name");
67 if (aCollection == null)
68 throw new NullPointerException ("Collection expression");
69
70 m_aType = aVarType;
71 m_sVarName = sVarName;
72 m_aCollection = aCollection;
73 m_aLopVar = new JVar (JMods.forVar (JMod.FINAL), m_aType, m_sVarName, aCollection);
74 }
75
76 @Nonnull
77 public AbstractJType type ()
78 {
79 return m_aType;
80 }
81
82 /**
83 * @return a reference to the loop variable.
84 */
85 @Nonnull
86 public JVar var ()
87 {
88 return m_aLopVar;
89 }
90
91 @Nonnull
92 public IJExpression collection ()
93 {
94 return m_aCollection;
95 }
96
97 @Nonnull
98 public JBlock body ()
99 {
100 if (m_aBody == null)
101 m_aBody = new JBlock ();
102 return m_aBody;
103 }
104
105 public void state (@Nonnull final JFormatter f)
106 {
107 f.print ("for (");
108 f.generable (m_aType).id (m_sVarName).print (": ").generable (m_aCollection);
109 f.print (')');
110 if (m_aBody != null)
111 f.generable (m_aBody);
112 else
113 f.print (';');
114 f.newline ();
115 }
116 }