KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > expressions > AndOrExpression


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.expressions;
16
17 import org.josql.Query;
18 import org.josql.QueryExecutionException;
19
20 /**
21  * Represents either an <code>AND</code> expression or a <code>OR</code> expression.
22  * Lazy evaluation is employed here such if the expression is: <code>LHS OR RHS</code>
23  * and LHS = true then the RHS is NOT evaluated, if the expression is: <code>LHS AND RHS</code>
24  * and LHS = false then the RHS is NOT evaluated (see {@link #isTrue(Object,Query)}). This is important to note if you expect
25  * side-effects to occur in the RHS (bad practice anyway so don't do it!).
26  * <p>
27  * Last Modified By: $Author: barrygently $<br />
28  * Last Modified On: $Date: 2004/12/20 16:22:43 $<br />
29  * Current Revision: $Revision: 1.2 $<br />
30  */

31 public class AndOrExpression extends BinaryExpression
32 {
33
34     private boolean and = false;
35
36     public boolean isAnd ()
37     {
38
39     return this.and;
40
41     }
42
43     public void setAnd (boolean v)
44     {
45
46     this.and = v;
47
48     }
49
50     /**
51      * Evaulates the expression and returns true if the expression evaulates to <code>true</code>.
52      * <p>
53      * <table border="1" cellpadding="3" cellspacing="0">
54      * <tr>
55      * <th>Type</th>
56      * <th>LHS</th>
57      * <th>RHS</th>
58      * <th>Result</th>
59      * <th>Notes</th>
60      * </tr>
61      * <tr>
62      * <td>AND</td>
63      * <td>true</td>
64      * <td>true</td>
65      * <td>true</td>
66      * <td>Both LHS and RHS are evaulated.</td>
67      * </tr>
68      * <tr>
69      * <td>AND</td>
70      * <td>true</td>
71      * <td>false</td>
72      * <td>false</td>
73      * <td>Both LHS and RHS are evaulated.</td>
74      * </tr>
75      * <tr>
76      * <td>AND</td>
77      * <td>false</td>
78      * <td>unknown or false</td>
79      * <td>false</td>
80      * <td>Only the LHS is evaulated.</td>
81      * </tr>
82      * <tr>
83      * <td>OR</td>
84      * <td>true</td>
85      * <td>unknown</td>
86      * <td>true</td>
87      * <td>Only the LHS is evaulated.</td>
88      * </tr>
89      * <tr>
90      * <td>OR</td>
91      * <td>false</td>
92      * <td>true</td>
93      * <td>true</td>
94      * <td>Both the LHS and RHS are evaulated.</td>
95      * </tr>
96      * <tr>
97      * <td>OR</td>
98      * <td>false</td>
99      * <td>false</td>
100      * <td>false</td>
101      * <td>Both the LHS and RHS are evaulated.</td>
102      * </tr>
103      * </table>
104      * <p>
105      * In general what this means is that you should "left-weight" your expressions so that
106      * the expression that returns <code>true</code> most often (or more likely to return
107      * <code>true</code>) should be on the LHS.
108      *
109      * @param o The current object to perform the expression on.
110      * @param q The query object.
111      * @return <code>true</code> if the expression evaulates to <code>true</code>, <code>false</code>
112      * otherwise.
113      * @throws QueryExecutionException If the expression cannot be evaulated.
114      */

115     public boolean isTrue (Object JavaDoc o,
116                Query q)
117                        throws QueryExecutionException
118     {
119
120     // Execute left first.
121
boolean l = this.left.isTrue (o,
122                       q);
123
124     // See what our predicate is...
125
if (this.and)
126     {
127
128             if (!l)
129             {
130
131                return false;
132
133             }
134
135         boolean r = this.right.isTrue (o,
136                        q);
137
138         return l && r;
139
140     }
141
142     if (l)
143     {
144
145         return true;
146
147     }
148
149     boolean r = this.right.isTrue (o,
150                        q);
151     
152     return r;
153
154     }
155
156     /**
157      * Return a string version of this expression.
158      * Note: any formatting of the statement (such as line breaks) will be removed.
159      *
160      * @return A string version of the expression.
161      */

162     public String JavaDoc toString ()
163     {
164
165     String JavaDoc pred = " OR ";
166     
167     if (this.and)
168     {
169
170         pred = " AND ";
171
172     }
173
174     if (this.isBracketed ())
175     {
176
177         return "(" + this.left.toString () + pred + this.right.toString () + ")";
178
179     }
180
181     return this.left.toString () + pred + this.right.toString ();
182
183     }
184
185 }
186
Popular Tags