KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > expressions > EvaluationResult


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.expressions;
12
13 import org.eclipse.core.runtime.Assert;
14
15 /**
16  * An evaluation result represents the result of an expression
17  * evaluation. There are exact three instances of evaluation
18  * result. They are: <code>FALSE</code>, <code>TRUE</code> and
19  * <code>NOT_LOADED</code>. <code>NOT_LOADED</code> represents
20  * the fact that an expression couldn't be evaluated since a
21  * plug-in providing certain test expressions isn't loaded yet.
22  * <p>
23  * In addition the class implements the three operation <code>and
24  * </code>, <code>or</code> and <code>not</code>. The operation are
25  * defined as follows:
26  * </p>
27  * <p>
28  * The and operation:
29  * </p>
30  * <table border="1" cellpadding="5">
31  * <colgroup>
32  * <col width="120">
33  * <col width="120">
34  * <col width="120">
35  * <col width="120">
36  * </colgroup>
37  * <tbody>
38  * <tr>
39  * <td><em>AND</em></td>
40  * <td>FALSE</td>
41  * <td>TRUE</td>
42  * <td>NOT_LOADED</td>
43  * </tr>
44  * <tr>
45  * <td>FALSE</td>
46  * <td>FALSE</td>
47  * <td>FALSE</td>
48  * <td>FALSE</td>
49  * </tr>
50  * <tr>
51  * <td>TRUE</td>
52  * <td>FALSE</td>
53  * <td>TRUE</td>
54  * <td>NOT_LOADED</td>
55  * </tr>
56  * <tr>
57  * <td>NOT_LOADED</td>
58  * <td>FALSE</td>
59  * <td>NOT_LOADED</td>
60  * <td>NOT_LOADED</td>
61  * </tr>
62  * </tbody>
63  * </table>
64  * <p>
65  * The or operation:
66  * </p>
67  * <table border="1" cellpadding="5">
68  * <colgroup>
69  * <col width="120">
70  * <col width="120">
71  * <col width="120">
72  * <col width="120">
73  * </colgroup>
74  * <tbody>
75  * <tr>
76  * <td><em>OR</em></td>
77  * <td>FALSE</td>
78  * <td>TRUE</td>
79  * <td>NOT_LOADED</td>
80  * </tr>
81  * <tr>
82  * <td>FALSE</td>
83  * <td>FALSE</td>
84  * <td>TRUE</td>
85  * <td>NOT_LOADED</td>
86  * </tr>
87  * <tr>
88  * <td>TRUE</td>
89  * <td>TRUE</td>
90  * <td>TRUE</td>
91  * <td>TRUE</td>
92  * </tr>
93  * <tr>
94  * <td>NOT_LOADED</td>
95  * <td>NOT_LOADED</td>
96  * <td>TRUE</td>
97  * <td>NOT_LOADED</td>
98  * </tr>
99  * </tbody>
100  * </table>
101  * <p>
102  * The not operation:
103  * </p>
104  * <table border="1" cellpadding="5">
105  * <colgroup>
106  * <col width="120">
107  * <col width="120">
108  * <col width="120">
109  * <col width="120">
110  * </colgroup>
111  * <tbody>
112  * <tr>
113  * <td><em>NOT<em></td>
114  * <td>FALSE</td>
115  * <td>TRUE</td>
116  * <td>NOT_LOADED</td>
117  * </tr>
118  * <tr>
119  * <td></td>
120  * <td>TRUE</td>
121  * <td>FALSE</td>
122  * <td>NOT_LOADED</td>
123  * </tr>
124  * </tbody>
125  * </table>
126  *
127  * <p>
128  * The class is not intended to be subclassed by clients.
129  * </p>
130  * @since 3.0
131  */

132 public class EvaluationResult {
133     
134     private int fValue;
135     
136     private static final int FALSE_VALUE= 0;
137     private static final int TRUE_VALUE= 1;
138     private static final int NOT_LOADED_VALUE= 2;
139     
140     /** The evaluation result representing the value FALSE */
141     public static final EvaluationResult FALSE= new EvaluationResult(FALSE_VALUE);
142     /** The evaluation result representing the value TRUE */
143     public static final EvaluationResult TRUE= new EvaluationResult(TRUE_VALUE);
144     /** The evaluation result representing the value NOT_LOADED */
145     public static final EvaluationResult NOT_LOADED= new EvaluationResult(NOT_LOADED_VALUE);
146
147     private static final EvaluationResult[][] AND= new EvaluationResult[][] {
148                         // FALSE //TRUE //NOT_LOADED
149
/* FALSE */ { FALSE, FALSE, FALSE },
150         /* TRUE */ { FALSE, TRUE, NOT_LOADED },
151         /* PNL */ { FALSE, NOT_LOADED, NOT_LOADED },
152     };
153
154     private static final EvaluationResult[][] OR= new EvaluationResult[][] {
155                         // FALSE //TRUE //NOT_LOADED
156
/* FALSE */ { FALSE, TRUE, NOT_LOADED },
157         /* TRUE */ { TRUE, TRUE, TRUE },
158         /* PNL */ { NOT_LOADED, TRUE, NOT_LOADED },
159     };
160
161     private static final EvaluationResult[] NOT= new EvaluationResult[] {
162         //FALSE //TRUE //NOT_LOADED
163
TRUE, FALSE, NOT_LOADED
164     };
165
166     /*
167      * No instances outside of <code>EvaluationResult</code>
168      */

169     private EvaluationResult(int value) {
170         fValue= value;
171     }
172     
173     /**
174      * Returns an <code>EvaluationResult</code> whose value is <code>this &amp;&amp; other)</code>.
175      *
176      * @param other the right hand side of the and operation.
177      *
178      * @return <code>this &amp;&amp; other</code> as defined by the evaluation result
179      */

180     public EvaluationResult and(EvaluationResult other) {
181         return AND[fValue][other.fValue];
182     }
183     
184     /**
185      * Returns an <code>EvaluationResult</code> whose value is <code>this || other)</code>.
186      *
187      * @param other the right hand side of the or operation.
188      *
189      * @return <code>this || other</code> as defined by the evaluation result
190      */

191     public EvaluationResult or(EvaluationResult other) {
192         return OR[fValue][other.fValue];
193     }
194     
195     /**
196      * Returns the inverted value of this evaluation result
197      *
198      * @return the inverted value of this evaluation result
199      */

200     public EvaluationResult not() {
201         return NOT[fValue];
202     }
203     
204     /**
205      * Returns an evaluation result instance representing the
206      * given boolean value. If the given boolean value is
207      * <code>true</code> then <code>ExpressionResult.TRUE</code>
208      * is returned. If the value is <code>false</code> then <code>
209      * ExpressionResult.FALSE</code> is returned.
210      *
211      * @param b a boolean value
212      *
213      * @return the expression result representing the boolean
214      * value
215      */

216     public static EvaluationResult valueOf(boolean b) {
217         return b ? TRUE : FALSE;
218     }
219     
220     /**
221      * Returns a evaluation result instance representing the
222      * given <code>Boolean</code> value. If the given <code>Boolean
223      * </code> value is <code>true</code> then <code>ExpressionResult.TRUE</code>
224      * is returned. If the value is <code>false</code> then <code>
225      * ExpressionResult.FALSE</code> is returned.
226      *
227      * @param b a <code>Boolean</code> value
228      *
229      * @return the expression result representing the <code>Boolean</code>
230      * value
231      */

232     public static EvaluationResult valueOf(Boolean JavaDoc b) {
233         return b.booleanValue() ? TRUE : FALSE;
234     }
235     
236     /**
237      * For debugging purpose only
238      *
239      * @return a string representing this object. The result is not
240      * human readable
241      */

242     public String JavaDoc toString() {
243         switch (fValue) {
244             case 0:
245                 return "false"; //$NON-NLS-1$
246
case 1:
247                 return "true"; //$NON-NLS-1$
248
case 2:
249                 return "not_loaded"; //$NON-NLS-1$
250
}
251         Assert.isTrue(false);
252         return null;
253     }
254 }
255
Popular Tags