KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > services > EvaluationResultCache


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.ui.internal.services;
13
14 import org.eclipse.core.expressions.EvaluationResult;
15 import org.eclipse.core.expressions.Expression;
16 import org.eclipse.core.expressions.IEvaluationContext;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.ui.ISources;
19
20 /**
21  * <p>
22  * A token representing the activation or contribution of some expression-based
23  * element. This caches the evaluation result so that it is only re-computed as
24  * necessary.
25  * </p>
26  *
27  * @since 3.2
28  */

29 public abstract class EvaluationResultCache implements IEvaluationResultCache {
30
31     /**
32      * The previous computed evaluation result. If no evaluation result is
33      * available, then this value is <code>null</code>.
34      */

35     private EvaluationResult evaluationResult = null;
36
37     /**
38      * The expression to evaluate. This value may be <code>null</code>, in
39      * which case the evaluation result is always <code>true</code>.
40      */

41     private final Expression expression;
42
43     /**
44      * The priority that has been given to this expression.
45      */

46     private final int sourcePriority;
47
48     /**
49      * Constructs a new instance of <code>EvaluationResultCache</code>.
50      *
51      * @param expression
52      * The expression that must evaluate to <code>true</code>
53      * before this handler is active. This value may be
54      * <code>null</code> if it is always active.
55      * @see ISources
56      */

57     protected EvaluationResultCache(final Expression expression) {
58         this.expression = expression;
59         this.sourcePriority = SourcePriorityNameMapping
60                 .computeSourcePriority(expression);
61     }
62
63     public final void clearResult() {
64         evaluationResult = null;
65     }
66
67     public final boolean evaluate(final IEvaluationContext context) {
68         if (expression == null) {
69             return true;
70         }
71
72         if (evaluationResult == null) {
73             try {
74                 evaluationResult = expression.evaluate(context);
75             } catch (final CoreException e) {
76                 /*
77                  * Swallow the exception. It simply means the variable is not
78                  * valid it some (most frequently, that the value is null). This
79                  * kind of information is not really useful to us, so we can
80                  * just treat it as null.
81                  */

82                 evaluationResult = EvaluationResult.FALSE;
83                 return false;
84             }
85         }
86
87         return evaluationResult == EvaluationResult.TRUE;
88     }
89
90     public final Expression getExpression() {
91         return expression;
92     }
93
94     public final int getSourcePriority() {
95         return sourcePriority;
96     }
97
98     public final void setResult(final boolean result) {
99         if (result) {
100             evaluationResult = EvaluationResult.TRUE;
101         } else {
102             evaluationResult = EvaluationResult.FALSE;
103         }
104     }
105 }
106
Popular Tags