KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.core.internal.expressions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18
19 import org.eclipse.core.expressions.EvaluationResult;
20 import org.eclipse.core.expressions.Expression;
21 import org.eclipse.core.expressions.ExpressionInfo;
22 import org.eclipse.core.expressions.IEvaluationContext;
23
24 public abstract class CompositeExpression extends Expression {
25
26     private static final Expression[] EMPTY_ARRAY = new Expression[0];
27
28     /**
29      * The seed for the hash code for all composite expressions.
30      */

31     private static final int HASH_INITIAL= CompositeExpression.class.getName().hashCode();
32     
33     protected List JavaDoc fExpressions;
34     
35     public void add(Expression expression) {
36         if (fExpressions == null)
37             fExpressions= new ArrayList JavaDoc(2);
38         fExpressions.add(expression);
39     }
40     
41     public Expression[] getChildren() {
42         if (fExpressions == null)
43             return EMPTY_ARRAY;
44         return (Expression[])fExpressions.toArray(new Expression[fExpressions.size()]);
45     }
46     
47     protected EvaluationResult evaluateAnd(IEvaluationContext scope) throws CoreException {
48         if (fExpressions == null)
49             return EvaluationResult.TRUE;
50         EvaluationResult result= EvaluationResult.TRUE;
51         for (Iterator JavaDoc iter= fExpressions.iterator(); iter.hasNext();) {
52             Expression expression= (Expression)iter.next();
53             result= result.and(expression.evaluate(scope));
54             // keep iterating even if we have a not loaded found. It can be
55
// that we find a false which will result in a better result.
56
if (result == EvaluationResult.FALSE)
57                 return result;
58         }
59         return result;
60     }
61     
62     protected EvaluationResult evaluateOr(IEvaluationContext scope) throws CoreException {
63         if (fExpressions == null)
64             return EvaluationResult.TRUE;
65         EvaluationResult result= EvaluationResult.FALSE;
66         for (Iterator JavaDoc iter= fExpressions.iterator(); iter.hasNext();) {
67             Expression expression= (Expression)iter.next();
68             result= result.or(expression.evaluate(scope));
69             if (result == EvaluationResult.TRUE)
70                 return result;
71         }
72         return result;
73     }
74     
75     public void collectExpressionInfo(ExpressionInfo info) {
76         if (fExpressions == null)
77             return;
78         for (Iterator JavaDoc iter= fExpressions.iterator(); iter.hasNext();) {
79             Expression expression= (Expression)iter.next();
80             expression.collectExpressionInfo(info);
81         }
82     }
83
84     protected int computeHashCode() {
85         return HASH_INITIAL * HASH_FACTOR + hashCode(fExpressions);
86     }
87 }
88
Popular Tags