KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.josql.internal.Utilities;
21
22 /**
23  * This class represents an "=" or "!=" expression. This class also provides the
24  * ability for the developer to prefix the "=" or "!=" with "$" to indicate that a
25  * case-insensitive comparison should be made, in which case the LHS and RHS
26  * are converted to strings first.
27  * <p>
28  * Last Modified By: $Author: barrygently $<br />
29  * Last Modified On: $Date: 2004/12/20 16:22:43 $<br />
30  * Current Revision: $Revision: 1.2 $<br />
31  */

32 public class EqualsExpression extends BinaryExpression
33 {
34
35     private boolean not = false;
36
37     public boolean ignoreCase = false;
38
39     public void setIgnoreCase (boolean v)
40     {
41
42     this.ignoreCase = v;
43
44     }
45
46     public boolean isIgnoreCase ()
47     {
48
49     return this.ignoreCase;
50
51     }
52
53     public boolean isNot ()
54     {
55
56     return this.not;
57
58     }
59
60     public void setNot (boolean v)
61     {
62
63     this.not = v;
64
65     }
66
67     /**
68      * Return whether this expression evaluates to <code>true</code>.
69      *
70      * @param o The current object to perform the expression on.
71      * @param q The Query object.
72      * @return <code>true</code> if the expression evaluates to <code>true</code>, <code>false</code>
73      * otherwise.
74      * @throws QueryExecutionException If the expression cannot be evaluated.
75      */

76     public boolean isTrue (Object JavaDoc o,
77                Query q)
78                        throws QueryExecutionException
79     {
80
81     // Get the value for the lhs.
82
Object JavaDoc l = null;
83
84     try
85     {
86
87         l = this.left.getValue (o,
88                     q);
89
90     } catch (Exception JavaDoc e) {
91
92         throw new QueryExecutionException ("Unable to get value for LHS of expression: " +
93                            this,
94                            e);
95
96     }
97
98     Object JavaDoc r = null;
99
100     try
101     {
102
103         r = this.right.getValue (o,
104                      q);
105
106     } catch (Exception JavaDoc e) {
107
108         throw new QueryExecutionException ("Unable to get value for RHS of expression: " +
109                            this,
110                            e);
111
112     }
113
114     if ((l == null)
115         &&
116         (r == null)
117        )
118     {
119
120         // Debatable about whether 2 nulls make a true...
121
if (this.not)
122         {
123
124         return false;
125
126         }
127
128         return true;
129
130     }
131
132     if ((l == null)
133         ||
134         (r == null)
135        )
136     {
137
138         if (this.not)
139         {
140
141         return true;
142
143         }
144
145         // One of them is null, can't do any checking...
146
return false;
147
148     }
149
150     // If we are here then both are NOT NULL.
151
return Utilities.matches (l,
152                   r,
153                   this.ignoreCase,
154                   4,
155                   this.not);
156
157     /*
158     if (this.ignoreCase)
159     {
160
161         // This means that we convert to a String for both and
162         // compare case-insensitive.
163         String ls = l.toString ();
164
165         String rs = r.toString ();
166
167         if ((ls == null)
168         ||
169         (rs == null)
170            )
171         {
172
173         // Can't compare nulls.
174         return false;
175
176         }
177
178         boolean eq = ls.equalsIgnoreCase (rs);
179
180         if ((this.not)
181         &&
182         (!eq)
183            )
184         {
185
186         return true;
187
188         }
189
190         if (eq)
191         {
192
193         return true;
194
195         }
196
197         return false;
198
199     }
200
201     // See if one of the sides is a number.
202     boolean ln = Utilities.isNumber (l);
203     boolean rn = Utilities.isNumber (r);
204
205     if (ln
206         &&
207         rn
208        )
209     {
210
211         // They are numbers...
212         double ld = Utilities.getDouble (l);
213         double rd = Utilities.getDouble (r);
214
215         if (ld == rd)
216         {
217
218         if (this.not)
219         {
220
221             return false;
222
223         }
224
225         return true;
226
227         }
228
229         if (this.not)
230         {
231
232         return true;
233
234         }
235
236         return false;
237
238     }
239
240     // See if the 2 sides are "comparable".
241     // Need to check to see if they are the "same" type... otherwise a CCE will result.
242     if ((l instanceof Comparable)
243         &&
244         (r instanceof Comparable)
245         &&
246         (l.getClass ().isAssignableFrom (r.getClass ()))
247        )
248     {
249
250         Comparable lc = (Comparable) l;
251         Comparable rc = (Comparable) r;
252
253         boolean eq = lc.compareTo (rc) == 0;
254
255         if ((this.not)
256         &&
257         (!eq)
258            )
259         {
260
261         return true;
262
263         }
264
265         if (eq)
266         {
267
268         return true;
269
270         }
271
272         return false;
273
274     }
275
276     // If we are here then one or more of the items does NOT implement Comparable.
277     // Force a string comparison.
278
279     String ls = l.toString ();
280
281     String rs = r.toString ();
282
283     if (ls == null)
284     {
285
286         ls = "";
287
288     }
289
290     if (rs == null)
291     {
292
293         rs = "";
294
295     }
296
297     boolean eq = false;
298
299     if (this.ignoreCase)
300     {
301
302         eq = ls.equalsIgnoreCase (rs);
303
304     } else {
305
306         eq = ls.equals (rs);
307
308     }
309
310     if (eq)
311     {
312
313         if (this.not)
314         {
315
316         return false;
317
318         }
319
320         return true;
321
322     } else {
323
324         if (this.not)
325         {
326
327         return true;
328
329         }
330
331         return false;
332
333     }
334     */

335
336     }
337
338     /**
339      * Return a string version of the expression.
340      * In the form: {@link Expression#toString() Expression} [ $ ] [ ! ] = {@link Expression#toString() Expression}
341      *
342      * @return The string version.
343      */

344     public String JavaDoc toString ()
345     {
346
347     String JavaDoc pred = "=";
348     
349     if (this.not)
350     {
351
352         pred = "!=";
353
354     }
355
356     if (this.ignoreCase)
357     {
358
359         pred = "$" + pred;
360
361     }
362
363     if (this.isBracketed ())
364     {
365
366         return "(" + this.left.toString () + " " + pred + " " + this.right.toString () + ")";
367
368     }
369
370     return this.left.toString () + " " + pred + " " + this.right.toString ();
371
372     }
373
374 }
375
Popular Tags