KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > ConditionalExpression


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 org.eclipse.jdt.core.dom;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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  */

27 public class ConditionalExpression extends Expression {
28     
29     /**
30      * The "expression" structural property of this node type.
31      * @since 3.0
32      */

33     public static final ChildPropertyDescriptor EXPRESSION_PROPERTY =
34         new ChildPropertyDescriptor(ConditionalExpression.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
35

36     /**
37      * The "thenExpression" structural property of this node type.
38      * @since 3.0
39      */

40     public static final ChildPropertyDescriptor THEN_EXPRESSION_PROPERTY =
41         new ChildPropertyDescriptor(ConditionalExpression.class, "thenExpression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
42

43     /**
44      * The "elseExpression" structural property of this node type.
45      * @since 3.0
46      */

47     public static final ChildPropertyDescriptor ELSE_EXPRESSION_PROPERTY =
48         new ChildPropertyDescriptor(ConditionalExpression.class, "elseExpression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$
49

50     /**
51      * A list of property descriptors (element type:
52      * {@link StructuralPropertyDescriptor}),
53      * or null if uninitialized.
54      */

55     private static final List JavaDoc PROPERTY_DESCRIPTORS;
56     
57     static {
58         List JavaDoc properyList = new ArrayList JavaDoc(4);
59         createPropertyList(ConditionalExpression.class, properyList);
60         addProperty(EXPRESSION_PROPERTY, properyList);
61         addProperty(THEN_EXPRESSION_PROPERTY, properyList);
62         addProperty(ELSE_EXPRESSION_PROPERTY, properyList);
63         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
64     }
65
66     /**
67      * Returns a list of structural property descriptors for this node type.
68      * Clients must not modify the result.
69      *
70      * @param apiLevel the API level; one of the
71      * <code>AST.JLS*</code> constants
72
73      * @return a list of property descriptors (element type:
74      * {@link StructuralPropertyDescriptor})
75      * @since 3.0
76      */

77     public static List JavaDoc propertyDescriptors(int apiLevel) {
78         return PROPERTY_DESCRIPTORS;
79     }
80             
81     /**
82      * The condition expression; lazily initialized; defaults to an unspecified,
83      * but legal, expression.
84      */

85     private Expression conditionExpression = null;
86
87     /**
88      * The "then" expression; lazily initialized; defaults to an unspecified,
89      * but legal, expression.
90      */

91     private Expression thenExpression = null;
92
93     /**
94      * The "else" expression; lazily initialized; defaults to an unspecified,
95      * but legal, expression.
96      */

97     private Expression elseExpression = null;
98
99     /**
100      * Creates a new unparented conditional expression node owned by the given
101      * AST. By default, the condition, "then", and "else" expresssions are
102      * unspecified, but legal.
103      * <p>
104      * N.B. This constructor is package-private.
105      * </p>
106      *
107      * @param ast the AST that is to own this node
108      */

109     ConditionalExpression(AST ast) {
110         super(ast);
111     }
112
113     /* (omit javadoc for this method)
114      * Method declared on ASTNode.
115      */

116     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
117         return propertyDescriptors(apiLevel);
118     }
119     
120     /* (omit javadoc for this method)
121      * Method declared on ASTNode.
122      */

123     final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
124         if (property == EXPRESSION_PROPERTY) {
125             if (get) {
126                 return getExpression();
127             } else {
128                 setExpression((Expression) child);
129                 return null;
130             }
131         }
132         if (property == THEN_EXPRESSION_PROPERTY) {
133             if (get) {
134                 return getThenExpression();
135             } else {
136                 setThenExpression((Expression) child);
137                 return null;
138             }
139         }
140         if (property == ELSE_EXPRESSION_PROPERTY) {
141             if (get) {
142                 return getElseExpression();
143             } else {
144                 setElseExpression((Expression) child);
145                 return null;
146             }
147         }
148         // allow default implementation to flag the error
149
return super.internalGetSetChildProperty(property, get, child);
150     }
151     
152     /* (omit javadoc for this method)
153      * Method declared on ASTNode.
154      */

155     final int getNodeType0() {
156         return CONDITIONAL_EXPRESSION;
157     }
158
159     /* (omit javadoc for this method)
160      * Method declared on ASTNode.
161      */

162     ASTNode clone0(AST target) {
163         ConditionalExpression result = new ConditionalExpression(target);
164         result.setSourceRange(this.getStartPosition(), this.getLength());
165         result.setExpression((Expression) getExpression().clone(target));
166         result.setThenExpression(
167             (Expression) getThenExpression().clone(target));
168         result.setElseExpression(
169             (Expression) getElseExpression().clone(target));
170         return result;
171     }
172
173     /* (omit javadoc for this method)
174      * Method declared on ASTNode.
175      */

176     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
177         // dispatch to correct overloaded match method
178
return matcher.match(this, other);
179     }
180
181     /* (omit javadoc for this method)
182      * Method declared on ASTNode.
183      */

184     void accept0(ASTVisitor visitor) {
185         boolean visitChildren = visitor.visit(this);
186         if (visitChildren) {
187             // visit children in normal left to right reading order
188
acceptChild(visitor, getExpression());
189             acceptChild(visitor, getThenExpression());
190             acceptChild(visitor, getElseExpression());
191         }
192         visitor.endVisit(this);
193     }
194     
195     /**
196      * Returns the condition of this conditional expression.
197      *
198      * @return the condition node
199      */

200     public Expression getExpression() {
201         if (this.conditionExpression == null) {
202             // lazy init must be thread-safe for readers
203
synchronized (this) {
204                 if (this.conditionExpression == null) {
205                     preLazyInit();
206                     this.conditionExpression = new SimpleName(this.ast);
207                     postLazyInit(this.conditionExpression, EXPRESSION_PROPERTY);
208                 }
209             }
210         }
211         return this.conditionExpression;
212     }
213     
214     /**
215      * Sets the condition of this conditional expression.
216      *
217      * @param expression the condition node
218      * @exception IllegalArgumentException if:
219      * <ul>
220      * <li>the node belongs to a different AST</li>
221      * <li>the node already has a parent</li>
222      * <li>a cycle in would be created</li>
223      * </ul>
224      */

225     public void setExpression(Expression expression) {
226         if (expression == null) {
227             throw new IllegalArgumentException JavaDoc();
228         }
229         ASTNode oldChild = this.conditionExpression;
230         preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
231         this.conditionExpression = expression;
232         postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
233     }
234     
235     /**
236      * Returns the "then" part of this conditional expression.
237      *
238      * @return the "then" expression node
239      */

240     public Expression getThenExpression() {
241         if (this.thenExpression == null) {
242             // lazy init must be thread-safe for readers
243
synchronized (this) {
244                 if (this.thenExpression == null) {
245                     preLazyInit();
246                     this.thenExpression = new SimpleName(this.ast);
247                     postLazyInit(this.thenExpression, THEN_EXPRESSION_PROPERTY);
248                 }
249             }
250         }
251         return this.thenExpression;
252     }
253     
254     /**
255      * Sets the "then" part of this conditional expression.
256      *
257      * @param expression the "then" expression node
258      * @exception IllegalArgumentException if:
259      * <ul>
260      * <li>the node belongs to a different AST</li>
261      * <li>the node already has a parent</li>
262      * <li>a cycle in would be created</li>
263      * </ul>
264      */

265     public void setThenExpression(Expression expression) {
266         if (expression == null) {
267             throw new IllegalArgumentException JavaDoc();
268         }
269         ASTNode oldChild = this.thenExpression;
270         preReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY);
271         this.thenExpression = expression;
272         postReplaceChild(oldChild, expression, THEN_EXPRESSION_PROPERTY);
273     }
274
275     /**
276      * Returns the "else" part of this conditional expression.
277      *
278      * @return the "else" expression node
279      */

280     public Expression getElseExpression() {
281         if (this.elseExpression == null) {
282             // lazy init must be thread-safe for readers
283
synchronized (this) {
284                 if (this.elseExpression == null) {
285                     preLazyInit();
286                     this.elseExpression = new SimpleName(this.ast);
287                     postLazyInit(this.elseExpression, ELSE_EXPRESSION_PROPERTY);
288                 }
289             }
290         }
291         return this.elseExpression;
292     }
293     
294     /**
295      * Sets the "else" part of this conditional expression.
296      *
297      * @param expression the "else" expression node
298      * @exception IllegalArgumentException if:
299      * <ul>
300      * <li>the node belongs to a different AST</li>
301      * <li>the node already has a parent</li>
302      * <li>a cycle in would be created</li>
303      * </ul>
304      */

305     public void setElseExpression(Expression expression) {
306         if (expression == null) {
307             throw new IllegalArgumentException JavaDoc();
308         }
309         ASTNode oldChild = this.elseExpression;
310         preReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY);
311         this.elseExpression = expression;
312         postReplaceChild(oldChild, expression, ELSE_EXPRESSION_PROPERTY);
313     }
314
315     /* (omit javadoc for this method)
316      * Method declared on ASTNode.
317      */

318     int memSize() {
319         // treat Code as free
320
return BASE_NODE_SIZE + 3 * 4;
321     }
322     
323     /* (omit javadoc for this method)
324      * Method declared on ASTNode.
325      */

326     int treeSize() {
327         return
328             memSize()
329             + (this.conditionExpression == null ? 0 : getExpression().treeSize())
330             + (this.thenExpression == null ? 0 : getThenExpression().treeSize())
331             + (this.elseExpression == null ? 0 : getElseExpression().treeSize());
332     }
333 }
334
Popular Tags