removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / ConditionalExpression.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  * Conditional expression AST node type.
19  *
20  * <pre>
21  * ConditionalExpression:
22  *    Expression <b>?</b> Expression <b>:</b> Expression
23  * </pre>
24  * 
25  * @since 2.0
26  * @noinstantiate This class is not intended to be instantiated by clients.
27  */
28 public class ConditionalExpression extends Expression {
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(ConditionalExpression.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
36
37         /**
38          * The "thenExpression" structural property of this node type.
39          * @since 3.0
40          */
41         public static final ChildPropertyDescriptor THEN_EXPRESSION_PROPERTY = 
42                 new ChildPropertyDescriptor(ConditionalExpression.class, "thenExpression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
43
44         /**
45          * The "elseExpression" structural property of this node type.
46          * @since 3.0
47          */
48         public static final ChildPropertyDescriptor ELSE_EXPRESSION_PROPERTY = 
49                 new ChildPropertyDescriptor(ConditionalExpression.class, "elseExpression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
50
51         /**
52          * A list of property descriptors (element type: 
53          * {@link StructuralPropertyDescriptor}),
54          * or null if uninitialized.
55          */
56         private static final List PROPERTY_DESCRIPTORS;
57         
58         static {
59                 List properyList = new ArrayList(4);
60                 createPropertyList(ConditionalExpression.class, properyList);
61                 addProperty(EXPRESSION_PROPERTY, properyList);
62                 addProperty(THEN_EXPRESSION_PROPERTY, properyList);
63                 addProperty(ELSE_EXPRESSION_PROPERTY, properyList);
64                 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
65         }
66
67         /**
68          * Returns a list of structural property descriptors for this node type.
69          * Clients must not modify the result.
70          * 
71          * @param apiLevel the API level; one of the
72          * <code>AST.JLS*</code> constants
73
74          * @return a list of property descriptors (element type: 
75          * {@link StructuralPropertyDescriptor})
76          * @since 3.0
77          */
78         public static List propertyDescriptors(int apiLevel) {
79                 return PROPERTY_DESCRIPTORS;
80         }
81                         
82         /**
83          * The condition expression; lazily initialized; defaults to an unspecified,
84          * but legal, expression.
85          */
86         private Expression conditionExpression = null;
87
88         /**
89          * The "then" expression; lazily initialized; defaults to an unspecified,
90          * but legal, expression.
91          */
92         private Expression thenExpression = null;
93
94         /**
95          * The "else" expression; lazily initialized; defaults to an unspecified,
96          * but legal, expression.
97          */
98         private Expression elseExpression = null;
99
100         /**
101          * Creates a new unparented conditional expression node owned by the given 
102          * AST. By default, the condition, "then", and "else" expresssions are
103          * unspecified, but legal.
104          * <p>
105          * N.B. This constructor is package-private.
106          * </p>
107          * 
108          * @param ast the AST that is to own this node
109          */
110         ConditionalExpression(AST ast) {
111                 super(ast);
112         }
113
114         /* (omit javadoc for this method)
115          * Method declared on ASTNode.
116          */
117         final List internalStructuralPropertiesForType(int apiLevel) {
118                 return propertyDescriptors(apiLevel);
119         }
120         
121         /* (omit javadoc for this method)
122          * Method declared on ASTNode.
123          */
124         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
125                 if (property == EXPRESSION_PROPERTY) {
126                         if (get) {
127                                 return getExpression();
128                         } else {
129                                 setExpression((Expression) child);
130                                 return null;
131                         }
132                 }
133                 if (property == THEN_EXPRESSION_PROPERTY) {
134                         if (get) {
135                                 return getThenExpression();
136                         } else {
137                                 setThenExpression((Expression) child);
138                                 return null;
139                         }
140                 }
141                 if (property == ELSE_EXPRESSION_PROPERTY) {
142                         if (get) {
143                                 return getElseExpression();
144                         } else {
145                                 setElseExpression((Expression) child);
146                                 return null;
147                         }
148                 }
149                 // allow default implementation to flag the error
150                 return super.internalGetSetChildProperty(property, get, child);
151         }
152         
153         /* (omit javadoc for this method)
154          * Method declared on ASTNode.
155          */
156         final int getNodeType0() {
157                 return CONDITIONAL_EXPRESSION;
158         }
159
160         /* (omit javadoc for this method)
161          * Method declared on ASTNode.
162          */
163         ASTNode clone0(AST target) {
164                 ConditionalExpression result = new ConditionalExpression(target);
165                 result.setSourceRange(this.getStartPosition(), this.getLength());
166                 result.setExpression((Expression) getExpression().clone(target));
167                 result.setThenExpression(
168                         (Expression) getThenExpression().clone(target));
169                 result.setElseExpression(
170                         (Expression) getElseExpression().clone(target));
171                 return result;
172         }
173
174         /* (omit javadoc for this method)
175          * Method declared on ASTNode.
176          */
177         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
178                 // dispatch to correct overloaded match method
179                 return matcher.match(this, other);
180         }
181
182         /* (omit javadoc for this method)
183          * Method declared on ASTNode.
184          */
185         void accept0(ASTVisitor visitor) {
186                 boolean visitChildren = visitor.visit(this);
187                 if (visitChildren) {
188                         // visit children in normal left to right reading order
189                         acceptChild(visitor, getExpression());
190                         acceptChild(visitor, getThenExpression());
191                         acceptChild(visitor, getElseExpression());
192                 }
193                 visitor.endVisit(this);
194         }
195         
196         /**
197          * Returns the condition of this conditional expression.
198          * 
199          * @return the condition node
200          */ 
201         public Expression getExpression() {
202                 if (this.conditionExpression == null) {
203                         // lazy init must be thread-safe for readers
204                         synchronized (this) {
205                                 if (this.conditionExpression == null) {
206                                         preLazyInit();
207                                         this.conditionExpression = new SimpleName(this.ast);
208                                         postLazyInit(this.conditionExpression, EXPRESSION_PROPERTY);
209                                 }
210                         }
211                 }
212                 return this.conditionExpression;
213         }
214         
215         /**
216          * Sets the condition of this conditional expression.
217          * 
218          * @param expression the condition node
219          * @exception IllegalArgumentException if:
220          * <ul>
221          * <li>the node belongs to a different AST</li>
222          * <li>the node already has a parent</li>
223          * <li>a cycle in would be created</li>
224          * </ul>
225          */ 
226         public void setExpression(Expression expression) {
227                 if (expression == null) {
228                         throw new IllegalArgumentException();
229                 }
230                 ASTNode oldChild = this.conditionExpression;
231                 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
232                 this.conditionExpression = expression;
233                 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
234         }
235         
236         /**
237          * Returns the "then" part of this conditional expression.
238          * 
239          * @return the "then" expression node
240          */ 
241         public Expression getThenExpression() {
242                 if (this.thenExpression == null) {
243                         // lazy init must be thread-safe for readers
244                         synchronized (this) {
245                                 if (this.thenExpression == null) {
246                                         preLazyInit();
247                                         this.thenExpression = new SimpleName(this.ast);
248                                         postLazyInit(this.thenExpression, THEN_EXPRESSION_PROPERTY);
249                                 }
250                         }
251                 }
252                 return this.thenExpression;
253         }
254         
255         /**
256          * Sets the "then" part of this conditional expression.
257          * 
258          * @param expression the "then" expression node
259          * @exception IllegalArgumentException if:
260          * <ul>
261          * <li>the node belongs to a different AST</li>
262          * <li>the node already has a parent</li>
263          * <li>a cycle in would be created</li>
264          * </ul>
265          */ 
266         public void setThenExpression(Expression expression) {
267                 if (expression == null) {
268                         throw new IllegalArgumentException();
269                 }
270                 ASTNode oldChild = this.thenExpression;
271                 preReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY);
272                 this.thenExpression = expression;
273                 postReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY);
274         }
275
276         /**
277          * Returns the "else" part of this conditional expression.
278          * 
279          * @return the "else" expression node
280          */ 
281         public Expression getElseExpression() {
282                 if (this.elseExpression == null) {
283                         // lazy init must be thread-safe for readers
284                         synchronized (this) {
285                                 if (this.elseExpression == null) {
286                                         preLazyInit();
287                                         this.elseExpression = new SimpleName(this.ast);
288                                         postLazyInit(this.elseExpression, ELSE_EXPRESSION_PROPERTY);
289                                 }
290                         }
291                 }
292                 return this.elseExpression;
293         }
294         
295         /**
296          * Sets the "else" part of this conditional expression.
297          * 
298          * @param expression the "else" expression node
299          * @exception IllegalArgumentException if:
300          * <ul>
301          * <li>the node belongs to a different AST</li>
302          * <li>the node already has a parent</li>
303          * <li>a cycle in would be created</li>
304          * </ul>
305          */ 
306         public void setElseExpression(Expression expression) {
307                 if (expression == null) {
308                         throw new IllegalArgumentException();
309                 }
310                 ASTNode oldChild = this.elseExpression;
311                 preReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY);
312                 this.elseExpression = expression;
313                 postReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY);
314         }
315
316         /* (omit javadoc for this method)
317          * Method declared on ASTNode.
318          */
319         int memSize() {
320                 // treat Code as free
321                 return BASE_NODE_SIZE + 3 * 4;
322         }
323         
324         /* (omit javadoc for this method)
325          * Method declared on ASTNode.
326          */
327         int treeSize() {
328                 return 
329                         memSize()
330                         + (this.conditionExpression == null ? 0 : getExpression().treeSize())
331                         + (this.thenExpression == null ? 0 : getThenExpression().treeSize())
332                         + (this.elseExpression == null ? 0 : getElseExpression().treeSize());
333         }
334 }