removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / WhileStatement.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.core.dom;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 /**
18  * While statement AST node type.
19  *
20  * <pre>
21  * WhileStatement:
22  *    <b>while</b> <b>(</b> Expression <b>)</b> Statement
23  * </pre>
24  * 
25  * @since 2.0
26  * @noinstantiate This class is not intended to be instantiated by clients.
27  */
28 public class WhileStatement extends Statement {
29
30         /**
31          * The "expression" structural property of this node type.
32          * @since 3.0
33          */
34         public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = 
35                 new ChildPropertyDescriptor(WhileStatement.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
36
37         /**
38          * The "body" structural property of this node type.
39          * @since 3.0
40          */
41         public static final ChildPropertyDescriptor BODY_PROPERTY = 
42                 new ChildPropertyDescriptor(WhileStatement.class, "body", Statement.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
43
44         /**
45          * A list of property descriptors (element type: 
46          * {@link StructuralPropertyDescriptor}),
47          * or null if uninitialized.
48          */
49         private static final List PROPERTY_DESCRIPTORS;
50         
51         static {
52                 List propertyList = new ArrayList(3);
53                 createPropertyList(WhileStatement.class, propertyList);
54                 addProperty(EXPRESSION_PROPERTY, propertyList);
55                 addProperty(BODY_PROPERTY, propertyList);
56                 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
57         }
58
59         /**
60          * Returns a list of structural property descriptors for this node type.
61          * Clients must not modify the result.
62          * 
63          * @param apiLevel the API level; one of the
64          * <code>AST.JLS*</code> constants
65
66          * @return a list of property descriptors (element type: 
67          * {@link StructuralPropertyDescriptor})
68          * @since 3.0
69          */
70         public static List propertyDescriptors(int apiLevel) {
71                 return PROPERTY_DESCRIPTORS;
72         }
73                         
74         /**
75          * The expression; lazily initialized; defaults to an unspecified, but 
76          * legal, expression.
77          */
78         private Expression expression = null;
79
80         /**
81          * The body statement; lazily initialized; defaults to an empty block 
82          * statement.
83          */
84         private Statement body = null;
85
86         /**
87          * Creates a new unparented while statement node owned by the given 
88          * AST. By default, the expresssion is unspecified, but legal, and
89          * the body statement is an empty block.
90          * <p>
91          * N.B. This constructor is package-private.
92          * </p>
93          * 
94          * @param ast the AST that is to own this node
95          */
96         WhileStatement(AST ast) {
97                 super(ast);
98         }
99
100         /* (omit javadoc for this method)
101          * Method declared on ASTNode.
102          */
103         final List internalStructuralPropertiesForType(int apiLevel) {
104                 return propertyDescriptors(apiLevel);
105         }
106         
107         /* (omit javadoc for this method)
108          * Method declared on ASTNode.
109          */
110         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
111                 if (property == EXPRESSION_PROPERTY) {
112                         if (get) {
113                                 return getExpression();
114                         } else {
115                                 setExpression((Expression) child);
116                                 return null;
117                         }
118                 }
119                 if (property == BODY_PROPERTY) {
120                         if (get) {
121                                 return getBody();
122                         } else {
123                                 setBody((Statement) child);
124                                 return null;
125                         }
126                 }
127                 // allow default implementation to flag the error
128                 return super.internalGetSetChildProperty(property, get, child);
129         }
130         
131         /* (omit javadoc for this method)
132          * Method declared on ASTNode.
133          */
134         final int getNodeType0() {
135                 return WHILE_STATEMENT;
136         }
137
138         /* (omit javadoc for this method)
139          * Method declared on ASTNode.
140          */
141         ASTNode clone0(AST target) {
142                 WhileStatement result = new WhileStatement(target);
143                 result.setSourceRange(this.getStartPosition(), this.getLength());
144                 result.copyLeadingComment(this);
145                 result.setExpression((Expression) getExpression().clone(target));
146                 result.setBody((Statement) getBody().clone(target));
147                 return result;
148         }
149
150         /* (omit javadoc for this method)
151          * Method declared on ASTNode.
152          */
153         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
154                 // dispatch to correct overloaded match method
155                 return matcher.match(this, other);
156         }
157
158         /* (omit javadoc for this method)
159          * Method declared on ASTNode.
160          */
161         void accept0(ASTVisitor visitor) {
162                 boolean visitChildren = visitor.visit(this);
163                 if (visitChildren) {
164                         // visit children in normal left to right reading order
165                         acceptChild(visitor, getExpression());
166                         acceptChild(visitor, getBody());
167                 }
168                 visitor.endVisit(this);
169         }
170         
171         /**
172          * Returns the expression of this while statement.
173          * 
174          * @return the expression node
175          */ 
176         public Expression getExpression() {
177                 if (this.expression == null) {
178                         // lazy init must be thread-safe for readers
179                         synchronized (this) {
180                                 if (this.expression == null) {
181                                         preLazyInit();
182                                         this.expression = new SimpleName(this.ast);
183                                         postLazyInit(this.expression, EXPRESSION_PROPERTY);
184                                 }
185                         }
186                 }
187                 return this.expression;
188         }
189         
190         /**
191          * Sets the expression of this while statement.
192          * 
193          * @param expression the expression node
194          * @exception IllegalArgumentException if:
195          * <ul>
196          * <li>the node belongs to a different AST</li>
197          * <li>the node already has a parent</li>
198          * <li>a cycle in would be created</li>
199          * </ul>
200          */ 
201         public void setExpression(Expression expression) {
202                 if (expression == null) {
203                         throw new IllegalArgumentException();
204                 }
205                 ASTNode oldChild = this.expression;
206                 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
207                 this.expression = expression;
208                 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
209         }
210
211         /**
212          * Returns the body of this while statement.
213          * 
214          * @return the body statement node
215          */ 
216         public Statement getBody() {
217                 if (this.body == null) {
218                         // lazy init must be thread-safe for readers
219                         synchronized (this) {
220                                 if (this.body == null) {
221                                         preLazyInit();
222                                         this.body = new Block(this.ast);
223                                         postLazyInit(this.body, BODY_PROPERTY);
224                                 }
225                         }
226                 }
227                 return this.body;
228         }
229         
230         /**
231          * Sets the body of this while statement.
232          * <p>
233          * Special note: The Java language does not allow a local variable declaration
234          * to appear as the body of a while statement (they may only appear within a
235          * block). However, the AST will allow a <code>VariableDeclarationStatement</code>
236          * as the body of a <code>WhileStatement</code>. To get something that will
237          * compile, be sure to embed the <code>VariableDeclarationStatement</code>
238          * inside a <code>Block</code>.
239          * </p>
240          * 
241          * @param statement the body statement node
242          * @exception IllegalArgumentException if:
243          * <ul>
244          * <li>the node belongs to a different AST</li>
245          * <li>the node already has a parent</li>
246          * <li>a cycle in would be created</li>
247          * </ul>
248          */ 
249         public void setBody(Statement statement) {
250                 if (statement == null) {
251                         throw new IllegalArgumentException();
252                 }
253                 ASTNode oldChild = this.body;
254                 preReplaceChild(oldChild, statement, BODY_PROPERTY);
255                 this.body = statement;
256                 postReplaceChild(oldChild, statement, BODY_PROPERTY);
257         }
258         
259         /* (omit javadoc for this method)
260          * Method declared on ASTNode.
261          */
262         int memSize() {
263                 return super.memSize() + 2 * 4;
264         }
265         
266         /* (omit javadoc for this method)
267          * Method declared on ASTNode.
268          */
269         int treeSize() {
270                 return
271                         memSize()
272                         + (this.expression == null ? 0 : getExpression().treeSize())
273                         + (this.body == null ? 0 : getBody().treeSize());
274         }
275 }
276