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