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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.core.dom;
14 import java.util.ArrayList;
15 import java.util.List;
18 * Conditional expression AST node type.
21 * ConditionalExpression:
22 * Expression <b>?</b> Expression <b>:</b> Expression
26 * @noinstantiate This class is not intended to be instantiated by clients.
28 public class ConditionalExpression extends Expression {
31 * The "expression" structural property of this node type.
34 public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
35 new ChildPropertyDescriptor(ConditionalExpression.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
38 * The "thenExpression" structural property of this node type.
41 public static final ChildPropertyDescriptor THEN_EXPRESSION_PROPERTY =
42 new ChildPropertyDescriptor(ConditionalExpression.class, "thenExpression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
45 * The "elseExpression" structural property of this node type.
48 public static final ChildPropertyDescriptor ELSE_EXPRESSION_PROPERTY =
49 new ChildPropertyDescriptor(ConditionalExpression.class, "elseExpression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
52 * A list of property descriptors (element type:
53 * {@link StructuralPropertyDescriptor}),
54 * or null if uninitialized.
56 private static final List PROPERTY_DESCRIPTORS;
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);
68 * Returns a list of structural property descriptors for this node type.
69 * Clients must not modify the result.
71 * @param apiLevel the API level; one of the
72 * <code>AST.JLS*</code> constants
74 * @return a list of property descriptors (element type:
75 * {@link StructuralPropertyDescriptor})
78 public static List propertyDescriptors(int apiLevel) {
79 return PROPERTY_DESCRIPTORS;
83 * The condition expression; lazily initialized; defaults to an unspecified,
84 * but legal, expression.
86 private Expression conditionExpression = null;
89 * The "then" expression; lazily initialized; defaults to an unspecified,
90 * but legal, expression.
92 private Expression thenExpression = null;
95 * The "else" expression; lazily initialized; defaults to an unspecified,
96 * but legal, expression.
98 private Expression elseExpression = null;
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.
105 * N.B. This constructor is package-private.
108 * @param ast the AST that is to own this node
110 ConditionalExpression(AST ast) {
114 /* (omit javadoc for this method)
115 * Method declared on ASTNode.
117 final List internalStructuralPropertiesForType(int apiLevel) {
118 return propertyDescriptors(apiLevel);
121 /* (omit javadoc for this method)
122 * Method declared on ASTNode.
124 final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
125 if (property == EXPRESSION_PROPERTY) {
127 return getExpression();
129 setExpression((Expression) child);
133 if (property == THEN_EXPRESSION_PROPERTY) {
135 return getThenExpression();
137 setThenExpression((Expression) child);
141 if (property == ELSE_EXPRESSION_PROPERTY) {
143 return getElseExpression();
145 setElseExpression((Expression) child);
149 // allow default implementation to flag the error
150 return super.internalGetSetChildProperty(property, get, child);
153 /* (omit javadoc for this method)
154 * Method declared on ASTNode.
156 final int getNodeType0() {
157 return CONDITIONAL_EXPRESSION;
160 /* (omit javadoc for this method)
161 * Method declared on ASTNode.
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));
174 /* (omit javadoc for this method)
175 * Method declared on ASTNode.
177 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
178 // dispatch to correct overloaded match method
179 return matcher.match(this, other);
182 /* (omit javadoc for this method)
183 * Method declared on ASTNode.
185 void accept0(ASTVisitor visitor) {
186 boolean visitChildren = visitor.visit(this);
188 // visit children in normal left to right reading order
189 acceptChild(visitor, getExpression());
190 acceptChild(visitor, getThenExpression());
191 acceptChild(visitor, getElseExpression());
193 visitor.endVisit(this);
197 * Returns the condition of this conditional expression.
199 * @return the condition node
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) {
207 this.conditionExpression = new SimpleName(this.ast);
208 postLazyInit(this.conditionExpression, EXPRESSION_PROPERTY);
212 return this.conditionExpression;
216 * Sets the condition of this conditional expression.
218 * @param expression the condition node
219 * @exception IllegalArgumentException if:
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>
226 public void setExpression(Expression expression) {
227 if (expression == null) {
228 throw new IllegalArgumentException();
230 ASTNode oldChild = this.conditionExpression;
231 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
232 this.conditionExpression = expression;
233 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
237 * Returns the "then" part of this conditional expression.
239 * @return the "then" expression node
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) {
247 this.thenExpression = new SimpleName(this.ast);
248 postLazyInit(this.thenExpression, THEN_EXPRESSION_PROPERTY);
252 return this.thenExpression;
256 * Sets the "then" part of this conditional expression.
258 * @param expression the "then" expression node
259 * @exception IllegalArgumentException if:
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>
266 public void setThenExpression(Expression expression) {
267 if (expression == null) {
268 throw new IllegalArgumentException();
270 ASTNode oldChild = this.thenExpression;
271 preReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY);
272 this.thenExpression = expression;
273 postReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY);
277 * Returns the "else" part of this conditional expression.
279 * @return the "else" expression node
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) {
287 this.elseExpression = new SimpleName(this.ast);
288 postLazyInit(this.elseExpression, ELSE_EXPRESSION_PROPERTY);
292 return this.elseExpression;
296 * Sets the "else" part of this conditional expression.
298 * @param expression the "else" expression node
299 * @exception IllegalArgumentException if:
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>
306 public void setElseExpression(Expression expression) {
307 if (expression == null) {
308 throw new IllegalArgumentException();
310 ASTNode oldChild = this.elseExpression;
311 preReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY);
312 this.elseExpression = expression;
313 postReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY);
316 /* (omit javadoc for this method)
317 * Method declared on ASTNode.
320 // treat Code as free
321 return BASE_NODE_SIZE + 3 * 4;
324 /* (omit javadoc for this method)
325 * Method declared on ASTNode.
330 + (this.conditionExpression == null ? 0 : getExpression().treeSize())
331 + (this.thenExpression == null ? 0 : getThenExpression().treeSize())
332 + (this.elseExpression == null ? 0 : getElseExpression().treeSize());