KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > expressions > IterateExpression


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.internal.expressions;
12
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.w3c.dom.Element JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IConfigurationElement;
22
23 import org.eclipse.core.expressions.EvaluationResult;
24 import org.eclipse.core.expressions.ExpressionInfo;
25 import org.eclipse.core.expressions.IEvaluationContext;
26 import org.eclipse.core.expressions.IIterable;
27
28 public class IterateExpression extends CompositeExpression {
29     
30     private static class IteratePool implements IEvaluationContext {
31         
32         private Iterator JavaDoc fIterator;
33         private Object JavaDoc fDefaultVariable;
34         private IEvaluationContext fParent;
35         
36         public IteratePool(IEvaluationContext parent, Iterator JavaDoc iterator) {
37             Assert.isNotNull(parent);
38             Assert.isNotNull(iterator);
39             fParent= parent;
40             fIterator= iterator;
41         }
42         public IEvaluationContext getParent() {
43             return fParent;
44         }
45         public IEvaluationContext getRoot() {
46             return fParent.getRoot();
47         }
48         public Object JavaDoc getDefaultVariable() {
49             return fDefaultVariable;
50         }
51         public boolean getAllowPluginActivation() {
52             return fParent.getAllowPluginActivation();
53         }
54         public void setAllowPluginActivation(boolean value) {
55             fParent.setAllowPluginActivation(value);
56         }
57         public void addVariable(String JavaDoc name, Object JavaDoc value) {
58             fParent.addVariable(name, value);
59         }
60         public Object JavaDoc removeVariable(String JavaDoc name) {
61             return fParent.removeVariable(name);
62         }
63         public Object JavaDoc getVariable(String JavaDoc name) {
64             return fParent.getVariable(name);
65         }
66         public Object JavaDoc resolveVariable(String JavaDoc name, Object JavaDoc[] args) throws CoreException {
67             return fParent.resolveVariable(name, args);
68         }
69         public Object JavaDoc next() {
70             fDefaultVariable= fIterator.next();
71             return fDefaultVariable;
72         }
73         public boolean hasNext() {
74             return fIterator.hasNext();
75         }
76     }
77     
78     private static final String JavaDoc ATT_OPERATOR= "operator"; //$NON-NLS-1$
79
private static final String JavaDoc ATT_IF_EMPTY= "ifEmpty"; //$NON-NLS-1$
80
private static final int OR= 1;
81     private static final int AND= 2;
82
83     /**
84      * The seed for the hash code for all iterate expressions.
85      */

86     private static final int HASH_INITIAL= IterateExpression.class.getName().hashCode();
87     
88     private int fOperator;
89     private Boolean JavaDoc fEmptyResult;
90     
91     public IterateExpression(IConfigurationElement configElement) throws CoreException {
92         String JavaDoc opValue= configElement.getAttribute(ATT_OPERATOR);
93         initializeOperatorValue(opValue);
94         initializeEmptyResultValue(configElement.getAttribute(ATT_IF_EMPTY));
95     }
96
97     public IterateExpression(Element JavaDoc element) throws CoreException {
98         String JavaDoc opValue= element.getAttribute(ATT_OPERATOR);
99         initializeOperatorValue(opValue.length() > 0 ? opValue : null);
100         String JavaDoc ifEmpty= element.getAttribute(ATT_IF_EMPTY);
101         initializeEmptyResultValue(ifEmpty.length() > 0 ? ifEmpty : null);
102     }
103
104     public IterateExpression(String JavaDoc opValue) throws CoreException {
105         initializeOperatorValue(opValue);
106     }
107     
108     public IterateExpression(String JavaDoc opValue, String JavaDoc ifEmpty) throws CoreException {
109         initializeOperatorValue(opValue);
110         initializeEmptyResultValue(ifEmpty);
111     }
112     
113     private void initializeOperatorValue(String JavaDoc opValue) throws CoreException {
114         if (opValue == null) {
115             fOperator= AND;
116         } else {
117             Expressions.checkAttribute(ATT_OPERATOR, opValue, new String JavaDoc[] {"and", "or"}); //$NON-NLS-1$//$NON-NLS-2$
118
if ("and".equals(opValue)) { //$NON-NLS-1$
119
fOperator= AND;
120             } else {
121                 fOperator= OR;
122             }
123         }
124     }
125     
126     private void initializeEmptyResultValue(String JavaDoc value) {
127         if (value == null) {
128             fEmptyResult= null;
129         } else {
130             fEmptyResult= Boolean.valueOf(value);
131         }
132     }
133
134     /* (non-Javadoc)
135      * @see Expression#evaluate(IVariablePool)
136      */

137     public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
138         Object JavaDoc var= context.getDefaultVariable();
139         if (var instanceof Collection JavaDoc) {
140             Collection JavaDoc col= (Collection JavaDoc)var;
141             switch (col.size()) {
142                 case 0:
143                     if (fEmptyResult == null) {
144                         return fOperator == AND ? EvaluationResult.TRUE : EvaluationResult.FALSE;
145                     } else {
146                         return fEmptyResult.booleanValue() ? EvaluationResult.TRUE : EvaluationResult.FALSE;
147                     }
148                 case 1:
149                     if (col instanceof List JavaDoc)
150                         return evaluateAnd(new DefaultVariable(context, ((List JavaDoc)col).get(0)));
151                     // fall through
152
default:
153                     IteratePool iter= new IteratePool(context, col.iterator());
154                     EvaluationResult result= fOperator == AND ? EvaluationResult.TRUE : EvaluationResult.FALSE;
155                     while (iter.hasNext()) {
156                         iter.next();
157                         switch(fOperator) {
158                             case OR:
159                                 result= result.or(evaluateAnd(iter));
160                                 if (result == EvaluationResult.TRUE)
161                                     return result;
162                                 break;
163                             case AND:
164                                 result= result.and(evaluateAnd(iter));
165                                 if (result != EvaluationResult.TRUE)
166                                     return result;
167                                 break;
168                         }
169                     }
170                     return result;
171             }
172         } else {
173             IIterable iterable= Expressions.getAsIIterable(var, this);
174             if (iterable == null)
175                 return EvaluationResult.NOT_LOADED;
176             int count= 0;
177             IteratePool iter= new IteratePool(context, iterable.iterator());
178             EvaluationResult result= fOperator == AND ? EvaluationResult.TRUE : EvaluationResult.FALSE;
179             while (iter.hasNext()) {
180                 iter.next();
181                 count++;
182                 switch(fOperator) {
183                     case OR:
184                         result= result.or(evaluateAnd(iter));
185                         if (result == EvaluationResult.TRUE)
186                             return result;
187                         break;
188                     case AND:
189                         result= result.and(evaluateAnd(iter));
190                         if (result != EvaluationResult.TRUE)
191                             return result;
192                         break;
193                 }
194             }
195             if (count > 0) {
196                 return result;
197             } else {
198                 if (fEmptyResult == null) {
199                     return fOperator == AND ? EvaluationResult.TRUE : EvaluationResult.FALSE;
200                 } else {
201                     return fEmptyResult.booleanValue() ? EvaluationResult.TRUE : EvaluationResult.FALSE;
202                 }
203             }
204         }
205     }
206
207     public void collectExpressionInfo(ExpressionInfo info) {
208         // Although we access every single variable we only mark the default
209
// variable as accessed since we don't have single variables for the
210
// elements.
211
info.markDefaultVariableAccessed();
212         super.collectExpressionInfo(info);
213     }
214
215     public boolean equals(final Object JavaDoc object) {
216         if (!(object instanceof IterateExpression))
217             return false;
218         
219         final IterateExpression that= (IterateExpression)object;
220         return (this.fOperator == that.fOperator) && equals(this.fExpressions, that.fExpressions);
221     }
222
223     protected int computeHashCode() {
224         return HASH_INITIAL * HASH_FACTOR + hashCode(fExpressions)
225             * HASH_FACTOR + fOperator;
226     }
227 }
228
Popular Tags