Initial upgrade to Platform/JDT 3.4.1
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / SwitchCase.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  * Switch case AST node type. A switch case is a special kind of node used only
19  * in switch statements. It is a <code>Statement</code> in name only.
20  * <p>
21  * <pre>
22  * SwitchCase:
23  *              <b>case</b> Expression  <b>:</b>
24  *              <b>default</b> <b>:</b>
25  * </pre>
26  * </p>
27  * 
28  * @since 2.0
29  * @noinstantiate This class is not intended to be instantiated by clients.
30  */
31 public class SwitchCase extends Statement {
32         
33         /**
34          * The "expression" structural property of this node type.
35          * @since 3.0
36          */
37         public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = 
38                 new ChildPropertyDescriptor(SwitchCase.class, "expression", Expression.class, OPTIONAL, CYCLE_RISK); //$NON-NLS-1$
39
40         /**
41          * A list of property descriptors (element type: 
42          * {@link StructuralPropertyDescriptor}),
43          * or null if uninitialized.
44          */
45         private static final List PROPERTY_DESCRIPTORS;
46         
47         static {
48                 List propertyList = new ArrayList(2);
49                 createPropertyList(SwitchCase.class, propertyList);
50                 addProperty(EXPRESSION_PROPERTY, propertyList);
51                 PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
52         }
53
54         /**
55          * Returns a list of structural property descriptors for this node type.
56          * Clients must not modify the result.
57          * 
58          * @param apiLevel the API level; one of the
59          * <code>AST.JLS*</code> constants
60          * @return a list of property descriptors (element type: 
61          * {@link StructuralPropertyDescriptor})
62          * @since 3.0
63          */
64         public static List propertyDescriptors(int apiLevel) {
65                 return PROPERTY_DESCRIPTORS;
66         }
67                         
68         /**
69          * The expression; <code>null</code> for none; lazily initialized (but
70          * does <b>not</b> default to none).
71          * @see #expressionInitialized
72          */
73         private Expression optionalExpression = null;
74
75         /**
76          * Indicates whether <code>optionalExpression</code> has been initialized.
77          */
78         private boolean expressionInitialized = false;
79         
80         /**
81          * Creates a new AST node for a switch case pseudo-statement owned by the 
82          * given AST. By default, there is an unspecified, but legal, expression.
83          * 
84          * @param ast the AST that is to own this node
85          */
86         SwitchCase(AST ast) {
87                 super(ast);
88         }
89
90         /* (omit javadoc for this method)
91          * Method declared on ASTNode.
92          */
93         final List internalStructuralPropertiesForType(int apiLevel) {
94                 return propertyDescriptors(apiLevel);
95         }
96         
97         /* (omit javadoc for this method)
98          * Method declared on ASTNode.
99          */
100         final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) {
101                 if (property == EXPRESSION_PROPERTY) {
102                         if (get) {
103                                 return getExpression();
104                         } else {
105                                 setExpression((Expression) child);
106                                 return null;
107                         }
108                 }
109                 // allow default implementation to flag the error
110                 return super.internalGetSetChildProperty(property, get, child);
111         }
112         
113         /* (omit javadoc for this method)
114          * Method declared on ASTNode.
115          */
116         final int getNodeType0() {
117                 return SWITCH_CASE;
118         }
119
120         /* (omit javadoc for this method)
121          * Method declared on ASTNode.
122          */
123         ASTNode clone0(AST target) {
124                 SwitchCase result = new SwitchCase(target);
125                 result.setSourceRange(this.getStartPosition(), this.getLength());
126                 result.copyLeadingComment(this);
127                 result.setExpression(
128                         (Expression) ASTNode.copySubtree(target, getExpression()));
129                 return result;
130         }
131
132         /* (omit javadoc for this method)
133          * Method declared on ASTNode.
134          */
135         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
136                 // dispatch to correct overloaded match method
137                 return matcher.match(this, other);
138         }
139
140         /* (omit javadoc for this method)
141          * Method declared on ASTNode.
142          */
143         void accept0(ASTVisitor visitor) {
144                 boolean visitChildren = visitor.visit(this);
145                 if (visitChildren) {
146                         acceptChild(visitor, getExpression());
147                 }
148                 visitor.endVisit(this);
149         }
150         
151         /**
152          * Returns the expression of this switch case, or 
153          * <code>null</code> if there is none (the "default:" case).
154          * 
155          * @return the expression node, or <code>null</code> if there is none
156          */ 
157         public Expression getExpression() {
158                 if (!this.expressionInitialized) {
159                         // lazy init must be thread-safe for readers
160                         synchronized (this) {
161                                 if (!this.expressionInitialized) {
162                                         preLazyInit();
163                                         this.optionalExpression = new SimpleName(this.ast);
164                                         this.expressionInitialized = true;
165                                         postLazyInit(this.optionalExpression, EXPRESSION_PROPERTY);
166                                 }
167                         }
168                 }
169                 return this.optionalExpression;
170         }
171         
172         /**
173          * Sets the expression of this switch case, or clears it (turns it into
174          * the  "default:" case).
175          * 
176          * @param expression the expression node, or <code>null</code> to 
177          *    turn it into the  "default:" case
178          * @exception IllegalArgumentException if:
179          * <ul>
180          * <li>the node belongs to a different AST</li>
181          * <li>the node already has a parent</li>
182          * <li>a cycle in would be created</li>
183          * </ul>
184          */ 
185         public void setExpression(Expression expression) {
186                 ASTNode oldChild = this.optionalExpression;
187                 preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
188                 this.optionalExpression = expression;
189                 this.expressionInitialized = true;
190                 postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
191         }
192
193         /**
194          * Returns whether this switch case represents the "default:" case.
195          * <p>
196          * This convenience method is equivalent to
197          * <code>getExpression() == null</code>.
198          * </p>
199          * 
200          * @return <code>true</code> if this is the default switch case, and
201          *    <code>false</code> if this is a non-default switch case
202          */ 
203         public boolean isDefault()  {
204                 return getExpression() == null;
205         }
206         
207         /* (omit javadoc for this method)
208          * Method declared on ASTNode.
209          */
210         int memSize() {
211                 return super.memSize() + 2 * 4;
212         }
213         
214         /* (omit javadoc for this method)
215          * Method declared on ASTNode.
216          */
217         int treeSize() {
218                 return
219                         memSize()
220                         + (this.optionalExpression == null ? 0 : optionalExpression.treeSize());
221         }
222 }