001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.dom;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: /**
016: * Conditional expression AST node type.
017: *
018: * <pre>
019: * ConditionalExpression:
020: * Expression <b>?</b> Expression <b>:</b> Expression
021: * </pre>
022: *
023: * @since 2.0
024: */
025: public class ConditionalExpression extends Expression {
026:
027: /**
028: * The "expression" structural property of this node type.
029: * @since 3.0
030: */
031: public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = new ChildPropertyDescriptor(
032: ConditionalExpression.class,
033: "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
034:
035: /**
036: * The "thenExpression" structural property of this node type.
037: * @since 3.0
038: */
039: public static final ChildPropertyDescriptor THEN_EXPRESSION_PROPERTY = new ChildPropertyDescriptor(
040: ConditionalExpression.class,
041: "thenExpression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
042:
043: /**
044: * The "elseExpression" structural property of this node type.
045: * @since 3.0
046: */
047: public static final ChildPropertyDescriptor ELSE_EXPRESSION_PROPERTY = new ChildPropertyDescriptor(
048: ConditionalExpression.class,
049: "elseExpression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
050:
051: /**
052: * A list of property descriptors (element type:
053: * {@link StructuralPropertyDescriptor}),
054: * or null if uninitialized.
055: */
056: private static final List PROPERTY_DESCRIPTORS;
057:
058: static {
059: List properyList = new ArrayList(4);
060: createPropertyList(ConditionalExpression.class, properyList);
061: addProperty(EXPRESSION_PROPERTY, properyList);
062: addProperty(THEN_EXPRESSION_PROPERTY, properyList);
063: addProperty(ELSE_EXPRESSION_PROPERTY, properyList);
064: PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
065: }
066:
067: /**
068: * Returns a list of structural property descriptors for this node type.
069: * Clients must not modify the result.
070: *
071: * @param apiLevel the API level; one of the
072: * <code>AST.JLS*</code> constants
073:
074: * @return a list of property descriptors (element type:
075: * {@link StructuralPropertyDescriptor})
076: * @since 3.0
077: */
078: public static List propertyDescriptors(int apiLevel) {
079: return PROPERTY_DESCRIPTORS;
080: }
081:
082: /**
083: * The condition expression; lazily initialized; defaults to an unspecified,
084: * but legal, expression.
085: */
086: private Expression conditionExpression = null;
087:
088: /**
089: * The "then" expression; lazily initialized; defaults to an unspecified,
090: * but legal, expression.
091: */
092: private Expression thenExpression = null;
093:
094: /**
095: * The "else" expression; lazily initialized; defaults to an unspecified,
096: * but legal, expression.
097: */
098: private Expression elseExpression = null;
099:
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(
125: ChildPropertyDescriptor property, boolean get, ASTNode child) {
126: if (property == EXPRESSION_PROPERTY) {
127: if (get) {
128: return getExpression();
129: } else {
130: setExpression((Expression) child);
131: return null;
132: }
133: }
134: if (property == THEN_EXPRESSION_PROPERTY) {
135: if (get) {
136: return getThenExpression();
137: } else {
138: setThenExpression((Expression) child);
139: return null;
140: }
141: }
142: if (property == ELSE_EXPRESSION_PROPERTY) {
143: if (get) {
144: return getElseExpression();
145: } else {
146: setElseExpression((Expression) child);
147: return null;
148: }
149: }
150: // allow default implementation to flag the error
151: return super .internalGetSetChildProperty(property, get, child);
152: }
153:
154: /* (omit javadoc for this method)
155: * Method declared on ASTNode.
156: */
157: final int getNodeType0() {
158: return CONDITIONAL_EXPRESSION;
159: }
160:
161: /* (omit javadoc for this method)
162: * Method declared on ASTNode.
163: */
164: ASTNode clone0(AST target) {
165: ConditionalExpression result = new ConditionalExpression(target);
166: result
167: .setSourceRange(this .getStartPosition(), this
168: .getLength());
169: result
170: .setExpression((Expression) getExpression().clone(
171: target));
172: result.setThenExpression((Expression) getThenExpression()
173: .clone(target));
174: result.setElseExpression((Expression) getElseExpression()
175: .clone(target));
176: return result;
177: }
178:
179: /* (omit javadoc for this method)
180: * Method declared on ASTNode.
181: */
182: final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
183: // dispatch to correct overloaded match method
184: return matcher.match(this , other);
185: }
186:
187: /* (omit javadoc for this method)
188: * Method declared on ASTNode.
189: */
190: void accept0(ASTVisitor visitor) {
191: boolean visitChildren = visitor.visit(this );
192: if (visitChildren) {
193: // visit children in normal left to right reading order
194: acceptChild(visitor, getExpression());
195: acceptChild(visitor, getThenExpression());
196: acceptChild(visitor, getElseExpression());
197: }
198: visitor.endVisit(this );
199: }
200:
201: /**
202: * Returns the condition of this conditional expression.
203: *
204: * @return the condition node
205: */
206: public Expression getExpression() {
207: if (this .conditionExpression == null) {
208: // lazy init must be thread-safe for readers
209: synchronized (this ) {
210: if (this .conditionExpression == null) {
211: preLazyInit();
212: this .conditionExpression = new SimpleName(this .ast);
213: postLazyInit(this .conditionExpression,
214: EXPRESSION_PROPERTY);
215: }
216: }
217: }
218: return this .conditionExpression;
219: }
220:
221: /**
222: * Sets the condition of this conditional expression.
223: *
224: * @param expression the condition node
225: * @exception IllegalArgumentException if:
226: * <ul>
227: * <li>the node belongs to a different AST</li>
228: * <li>the node already has a parent</li>
229: * <li>a cycle in would be created</li>
230: * </ul>
231: */
232: public void setExpression(Expression expression) {
233: if (expression == null) {
234: throw new IllegalArgumentException();
235: }
236: ASTNode oldChild = this .conditionExpression;
237: preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
238: this .conditionExpression = expression;
239: postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
240: }
241:
242: /**
243: * Returns the "then" part of this conditional expression.
244: *
245: * @return the "then" expression node
246: */
247: public Expression getThenExpression() {
248: if (this .thenExpression == null) {
249: // lazy init must be thread-safe for readers
250: synchronized (this ) {
251: if (this .thenExpression == null) {
252: preLazyInit();
253: this .thenExpression = new SimpleName(this .ast);
254: postLazyInit(this .thenExpression,
255: THEN_EXPRESSION_PROPERTY);
256: }
257: }
258: }
259: return this .thenExpression;
260: }
261:
262: /**
263: * Sets the "then" part of this conditional expression.
264: *
265: * @param expression the "then" expression node
266: * @exception IllegalArgumentException if:
267: * <ul>
268: * <li>the node belongs to a different AST</li>
269: * <li>the node already has a parent</li>
270: * <li>a cycle in would be created</li>
271: * </ul>
272: */
273: public void setThenExpression(Expression expression) {
274: if (expression == null) {
275: throw new IllegalArgumentException();
276: }
277: ASTNode oldChild = this .thenExpression;
278: preReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY);
279: this .thenExpression = expression;
280: postReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY);
281: }
282:
283: /**
284: * Returns the "else" part of this conditional expression.
285: *
286: * @return the "else" expression node
287: */
288: public Expression getElseExpression() {
289: if (this .elseExpression == null) {
290: // lazy init must be thread-safe for readers
291: synchronized (this ) {
292: if (this .elseExpression == null) {
293: preLazyInit();
294: this .elseExpression = new SimpleName(this .ast);
295: postLazyInit(this .elseExpression,
296: ELSE_EXPRESSION_PROPERTY);
297: }
298: }
299: }
300: return this .elseExpression;
301: }
302:
303: /**
304: * Sets the "else" part of this conditional expression.
305: *
306: * @param expression the "else" expression node
307: * @exception IllegalArgumentException if:
308: * <ul>
309: * <li>the node belongs to a different AST</li>
310: * <li>the node already has a parent</li>
311: * <li>a cycle in would be created</li>
312: * </ul>
313: */
314: public void setElseExpression(Expression expression) {
315: if (expression == null) {
316: throw new IllegalArgumentException();
317: }
318: ASTNode oldChild = this .elseExpression;
319: preReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY);
320: this .elseExpression = expression;
321: postReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY);
322: }
323:
324: /* (omit javadoc for this method)
325: * Method declared on ASTNode.
326: */
327: int memSize() {
328: // treat Code as free
329: return BASE_NODE_SIZE + 3 * 4;
330: }
331:
332: /* (omit javadoc for this method)
333: * Method declared on ASTNode.
334: */
335: int treeSize() {
336: return memSize()
337: + (this .conditionExpression == null ? 0
338: : getExpression().treeSize())
339: + (this .thenExpression == null ? 0
340: : getThenExpression().treeSize())
341: + (this .elseExpression == null ? 0
342: : getElseExpression().treeSize());
343: }
344: }
|