KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19
20 import org.josql.Query;
21 import org.josql.QueryExecutionException;
22 import org.josql.QueryParseException;
23
24 import org.josql.internal.Utilities;
25
26 /**
27  * Represents a "BETWEEN x AND y" expression.
28  * <p>
29  * Last Modified By: $Author: barrygently $<br />
30  * Last Modified On: $Date: 2004/12/20 16:22:43 $<br />
31  * Current Revision: $Revision: 1.2 $<br />
32  */

33 public class BetweenExpression extends BinaryExpression
34 {
35
36     private ValueExpression start = null;
37     private ValueExpression end = null;
38     private boolean not = false;
39     private boolean leftFR = false;
40     private boolean startFR = false;
41     private boolean endFR = false;
42     private Object JavaDoc leftFRVal = null;
43     private Object JavaDoc startFRVal = null;
44     private Object JavaDoc endFRVal = null;
45
46     /**
47      * Inits the expression.
48      *
49      * @param q The Query object.
50      * @throws QueryParseException If the LHS, start or end cannot be inited.
51      */

52     public void init (Query q)
53                   throws QueryParseException
54     {
55
56     this.left.init (q);
57
58     this.start.init (q);
59     this.end.init (q);
60
61     this.leftFR = this.left.hasFixedResult (q);
62     this.startFR = this.start.hasFixedResult (q);
63     this.endFR = this.end.hasFixedResult (q);
64
65     }
66
67     /**
68      * Get the start expression.
69      *
70      * @return The start expression, is an instance of {@link ValueExpression}.
71      */

72     public Expression getStart ()
73     {
74
75     return this.start;
76
77     }
78
79     /**
80      * Get the end expression.
81      *
82      * @return The end expression, is an instance of {@link ValueExpression}.
83      */

84     public Expression getEnd ()
85     {
86
87     return this.end;
88
89     }
90
91     public void setEnd (ValueExpression e)
92     {
93
94     this.end = e;
95
96     }
97
98     public void setStart (ValueExpression s)
99     {
100
101     this.start = s;
102
103     }
104
105     public boolean isNot ()
106     {
107
108     return this.not;
109
110     }
111
112     public void setNot (boolean v)
113     {
114
115     this.not = v;
116
117     }
118
119     /**
120      * Return whether this expression evaluates to <code>true</code>.
121      *
122      * Is equivalent to: LHS >= START AND LHS <= END. And of course if the
123      * expression is NOTed then it returns !(LHS >= START AND LHS <= END).
124      *
125      * @param o The object to perform the expression on.
126      * @param q The Query object.
127      * @return <code>true</code> if the LHS is between the start and end values.
128      * @throws QueryExecutionException If the expression cannot be executed.
129      */

130     public boolean isTrue (Object JavaDoc o,
131                Query q)
132                        throws QueryExecutionException
133     {
134
135     Object JavaDoc l = null;
136
137     if (this.leftFR)
138     {
139
140         if (this.leftFRVal == null)
141         {
142
143         l = this.left.getValue (o,
144                     q);
145
146         this.leftFRVal = l;
147
148         } else {
149
150         l = this.leftFRVal;
151
152         }
153
154     } else {
155
156         l = this.left.getValue (o,
157                     q);
158
159         if (this.leftFR)
160         {
161
162         this.leftFRVal = l;
163         
164         }
165
166     }
167
168     Object JavaDoc s = null;
169
170     if (this.startFR)
171     {
172
173         if (this.startFRVal == null)
174         {
175
176         s = this.start.getValue (o,
177                      q);
178
179         this.startFRVal = s;
180
181         } else {
182
183         s = this.startFRVal;
184
185         }
186
187     } else {
188
189         s = this.start.getValue (o,
190                      q);
191
192         if (this.startFR)
193         {
194
195         this.startFRVal = s;
196         
197         }
198
199     }
200
201     Object JavaDoc e = null;
202
203     if (this.endFR)
204     {
205
206         if (this.endFRVal == null)
207         {
208
209         e = this.end.getValue (o,
210                        q);
211
212         this.endFRVal = e;
213
214         } else {
215
216         e = this.endFRVal;
217
218         }
219
220     } else {
221
222         e = this.end.getValue (o,
223                    q);
224
225         if (this.endFR)
226         {
227
228         this.endFRVal = e;
229
230         }
231
232     }
233
234     // See if the LHS is >= s.
235
boolean sb = Utilities.isGTEquals (l,
236                        s);
237
238     boolean eb = Utilities.isLTEquals (l,
239                        e);
240
241     if (!this.not
242         &&
243         sb
244         &&
245         eb
246        )
247     {
248
249         return true;
250
251     }
252
253     if (this.not
254         &&
255         (!sb
256          ||
257          !eb
258         )
259        )
260     {
261
262         return true;
263
264     }
265
266     return false;
267
268     }
269
270     /**
271      * Returns a string version of this expression.
272      * Will return the form:
273      * <p>
274      * <pre>
275      * {@link Expression#toString() Expression} [ NOT ] BETWEEN {@link Expression#toString() Expression} AND {@link Expression#toString() Expression}
276      * </pre>
277      *
278      * @return A string version of the expression.
279      */

280     public String JavaDoc toString ()
281     {
282
283     StringBuffer JavaDoc buf = new StringBuffer JavaDoc (this.left.toString ());
284
285     if (this.not)
286     {
287
288         buf.append (" NOT");
289
290     }
291
292     buf.append (" BETWEEN ");
293
294     buf.append (this.start);
295     buf.append (" AND ");
296     buf.append (this.end);
297
298     if (this.isBracketed ())
299     {
300
301         buf.insert (0,
302             "(");
303
304         buf.append (")");
305
306     }
307
308     return buf.toString ();
309
310     }
311
312 }
313
Popular Tags