KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > expressions > ExpressionInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.core.expressions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * A status object describing information about an expression tree.
19  * This information can for example be used to decide whether a
20  * expression tree as to be reevaluated if the value of some
21  * variables changes.
22  * <p>
23  * This class is not intended to be extended by clients.
24  * </p>
25  *
26  * @since 3.2
27  */

28 public class ExpressionInfo {
29
30     private boolean fHasDefaultVariableAccess;
31     private boolean fHasSystemPropertyAccess;
32     
33     // Although we are using this as sets we use lists since
34
// they are faster for smaller numbers of elements
35
private List JavaDoc fAccessedVariableNames;
36     private List JavaDoc fMisbehavingExpressionTypes;
37     
38     /**
39      * Returns <code>true</code> if the default variable is accessed
40      * by the expression tree.
41      *
42      * @return whether the default variable is accessed or not
43      */

44     public boolean hasDefaultVariableAccess() {
45         return fHasDefaultVariableAccess;
46     }
47     
48     /**
49      * Marks the default variable as accessed.
50      */

51     public void markDefaultVariableAccessed() {
52         fHasDefaultVariableAccess= true;
53     }
54
55     /**
56      * Returns <code>true</code> if the system property is accessed
57      * by the expression tree.
58      *
59      * @return whether the system property is accessed or not
60      */

61     public boolean hasSystemPropertyAccess() {
62         return fHasSystemPropertyAccess;
63     }
64     
65     /**
66      * Marks the system property as accessed.
67      */

68     public void markSystemPropertyAccessed() {
69         fHasSystemPropertyAccess= true;
70     }
71
72     /**
73      * Returns the set off accessed variables.
74      *
75      * @return the set off accessed variables
76      */

77     public String JavaDoc[] getAccessedVariableNames() {
78         if (fAccessedVariableNames == null)
79             return new String JavaDoc[0];
80         return (String JavaDoc[])fAccessedVariableNames.toArray(new String JavaDoc[fAccessedVariableNames.size()]);
81     }
82     
83     /**
84      * Marks the given variable as accessed.
85      *
86      * @param name the accessed variable
87      */

88     public void addVariableNameAccess(String JavaDoc name) {
89         if (fAccessedVariableNames == null) {
90             fAccessedVariableNames= new ArrayList JavaDoc(5);
91             fAccessedVariableNames.add(name);
92         } else {
93             if (!fAccessedVariableNames.contains(name))
94                 fAccessedVariableNames.add(name);
95         }
96     }
97
98     /**
99      * Returns the set of expression types which don't implement the
100      * new (@link Expression#computeReevaluationInfo(IEvaluationContext)}
101      * method. If one expression didn't implement the method the expression
102      * tree no optimizations can be done. Returns <code>null</code> if
103      * all expressions implement the method.
104      *
105      * @return the set of expression types which don't implement the
106      * <code>computeReevaluationInfo</code> method.
107      */

108     public Class JavaDoc[] getMisbehavingExpressionTypes() {
109         if (fMisbehavingExpressionTypes == null)
110             return null;
111         return (Class JavaDoc[])fMisbehavingExpressionTypes.toArray(new Class JavaDoc[fMisbehavingExpressionTypes.size()]);
112     }
113     
114     /**
115      * Adds the given class to the list of misbehaving classes.
116      *
117      * @param clazz the class to add.
118      */

119     public void addMisBehavingExpressionType(Class JavaDoc clazz) {
120         if (fMisbehavingExpressionTypes == null) {
121             fMisbehavingExpressionTypes= new ArrayList JavaDoc();
122             fMisbehavingExpressionTypes.add(clazz);
123         } else {
124             if (!fMisbehavingExpressionTypes.contains(clazz))
125                 fMisbehavingExpressionTypes.add(clazz);
126         }
127     }
128     
129     /**
130      * Merges this reevaluation information with the given info.
131      *
132      * @param other the information to merge with
133      */

134     public void merge(ExpressionInfo other) {
135         mergeDefaultVariableAccess(other);
136         mergeSystemPropertyAccess(other);
137         
138         mergeAccessedVariableNames(other);
139         mergeMisbehavingExpressionTypes(other);
140     }
141
142     /**
143      * Merges this reevaluation information with the given info
144      * ignoring the default variable access.
145      *
146      * @param other the information to merge with
147      */

148     public void mergeExceptDefaultVariable(ExpressionInfo other) {
149         mergeSystemPropertyAccess(other);
150         
151         mergeAccessedVariableNames(other);
152         mergeMisbehavingExpressionTypes(other);
153     }
154     
155     /**
156      * Merges only the default variable access.
157      *
158      * @param other the information to merge with
159      */

160     private void mergeDefaultVariableAccess(ExpressionInfo other) {
161         fHasDefaultVariableAccess= fHasDefaultVariableAccess || other.fHasDefaultVariableAccess;
162     }
163     
164     /**
165      * Merges only the system property access.
166      *
167      * @param other the information to merge with
168      */

169     private void mergeSystemPropertyAccess(ExpressionInfo other) {
170         fHasSystemPropertyAccess= fHasSystemPropertyAccess || other.fHasSystemPropertyAccess;
171     }
172
173     /**
174      * Merges only the accessed variable names.
175      *
176      * @param other the information to merge with
177      */

178     private void mergeAccessedVariableNames(ExpressionInfo other) {
179         if (fAccessedVariableNames == null) {
180             fAccessedVariableNames= other.fAccessedVariableNames;
181         } else {
182             if (other.fAccessedVariableNames != null) {
183                 for (Iterator JavaDoc iter= other.fAccessedVariableNames.iterator(); iter.hasNext();) {
184                     Object JavaDoc variableName= iter.next();
185                     if (!fAccessedVariableNames.contains(variableName))
186                         fAccessedVariableNames.add(variableName);
187                 }
188             }
189         }
190     }
191
192     /**
193      * Merges only the misbehaving expression types.
194      *
195      * @param other the information to merge with
196      */

197     private void mergeMisbehavingExpressionTypes(ExpressionInfo other) {
198         if (fMisbehavingExpressionTypes == null) {
199             fMisbehavingExpressionTypes= other.fMisbehavingExpressionTypes;
200         } else {
201             if (other.fMisbehavingExpressionTypes != null) {
202                 for (Iterator JavaDoc iter= other.fMisbehavingExpressionTypes.iterator(); iter.hasNext();) {
203                     Object JavaDoc clazz= iter.next();
204                     if (!fMisbehavingExpressionTypes.contains(clazz))
205                         fMisbehavingExpressionTypes.add(clazz);
206                 }
207             }
208         }
209     }
210 }
Popular Tags