01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.expressions;
11:
12: import java.util.ArrayList;
13: import java.util.Iterator;
14: import java.util.List;
15:
16: import org.eclipse.core.expressions.EvaluationResult;
17: import org.eclipse.core.expressions.Expression;
18: import org.eclipse.core.expressions.ExpressionInfo;
19: import org.eclipse.core.expressions.IEvaluationContext;
20: import org.eclipse.core.runtime.CoreException;
21:
22: /**
23: * Copied from org.eclipse.core.internal.expressions.
24: */
25: public abstract class CompositeExpression extends Expression {
26:
27: private static final Expression[] EMPTY_ARRAY = new Expression[0];
28:
29: protected List fExpressions;
30:
31: public void add(Expression expression) {
32: if (fExpressions == null) {
33: fExpressions = new ArrayList(2);
34: }
35: fExpressions.add(expression);
36: }
37:
38: public Expression[] getChildren() {
39: if (fExpressions == null) {
40: return EMPTY_ARRAY;
41: }
42: return (Expression[]) fExpressions
43: .toArray(new Expression[fExpressions.size()]);
44: }
45:
46: protected EvaluationResult evaluateAnd(IEvaluationContext scope)
47: throws CoreException {
48: if (fExpressions == null) {
49: return EvaluationResult.TRUE;
50: }
51: EvaluationResult result = EvaluationResult.TRUE;
52: for (Iterator iter = fExpressions.iterator(); iter.hasNext();) {
53: Expression expression = (Expression) iter.next();
54: result = result.and(expression.evaluate(scope));
55: // keep iterating even if we have a not loaded found. It can be
56: // that we find a false which will result in a better result.
57: if (result == EvaluationResult.FALSE) {
58: return result;
59: }
60: }
61: return result;
62: }
63:
64: protected EvaluationResult evaluateOr(IEvaluationContext scope)
65: throws CoreException {
66: if (fExpressions == null) {
67: return EvaluationResult.TRUE;
68: }
69: EvaluationResult result = EvaluationResult.FALSE;
70: for (Iterator iter = fExpressions.iterator(); iter.hasNext();) {
71: Expression expression = (Expression) iter.next();
72: result = result.or(expression.evaluate(scope));
73: if (result == EvaluationResult.TRUE) {
74: return result;
75: }
76: }
77: return result;
78: }
79:
80: public void collectExpressionInfo(ExpressionInfo info) {
81: if (fExpressions == null) {
82: return;
83: }
84: for (Iterator iter = fExpressions.iterator(); iter.hasNext();) {
85: Expression expression = (Expression) iter.next();
86: expression.collectExpressionInfo(info);
87: }
88: }
89: }
|