KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.ui.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.expressions.EvaluationResult;
18 import org.eclipse.core.expressions.Expression;
19 import org.eclipse.core.expressions.ExpressionInfo;
20 import org.eclipse.core.expressions.IEvaluationContext;
21 import org.eclipse.core.runtime.CoreException;
22
23 /**
24  * Copied from org.eclipse.core.internal.expressions.
25  */

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