KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.josql.Query;
22 import org.josql.QueryExecutionException;
23
24 import org.josql.internal.Utilities;
25
26 /**
27  * This class represents one of the following:
28  * <ul>
29  * <li><b>></b> - Greater than</li>
30  * <li><b><</b> - Less than</li>
31  * <li><b>>=</b> - Greater than or equal to</li>
32  * <li><b><=</b> - Less than or equal to</li>
33  * </ul>
34  * <p>
35  * You can also force a "string" comparison by prefixing with "$". This will force
36  * both sides to be strings via the <code>toString</code> method.
37  * <p>
38  * Last Modified By: $Author: barrygently $<br />
39  * Last Modified On: $Date: 2005/01/07 17:08:07 $<br />
40  * Current Revision: $Revision: 1.4 $<br />
41  */

42 public class GTLTExpression extends BinaryExpression
43 {
44
45     private int type = -1;
46     private boolean ignoreCase = false;
47
48     public int getType ()
49     {
50
51     return this.type;
52
53     }
54
55     /**
56      * Type is an integer here for speed purposes, however one of the constants
57      * should be used.
58      *
59      * @param t The type of expression.
60      */

61     public void setType (int t)
62     {
63
64     this.type = t;
65
66     }
67
68     public void setIgnoreCase (boolean v)
69     {
70
71     this.ignoreCase = v;
72
73     }
74
75     public boolean isIgnoreCase ()
76     {
77
78     return this.ignoreCase;
79
80     }
81
82     /**
83      * Return whether this expression evaluates to true. The actual comparison
84      * is performed by: {@link Utilities#compare(Object,Object)} which copes with
85      * the object types.
86      *
87      * @param o The current object to evaluate the expression on.
88      * @param q The Query object.
89      * @return <code>true</code> if the expression evaluates to <code>true</code>.
90      * @throws QueryExecutionException If the expression cannot be evaluated.
91      */

92     public boolean isTrue (Object JavaDoc o,
93                Query q)
94                        throws QueryExecutionException
95     {
96
97     // Get the lhs.
98
Object JavaDoc l = this.left.getValue (o,
99                        q);
100
101     Object JavaDoc r = this.right.getValue (o,
102                     q);
103
104     if ((l == null)
105         &&
106         (r == null)
107        )
108     {
109
110         if ((this.type == 1)
111         ||
112         (this.type == 3)
113            )
114         {
115
116         return true;
117
118         }
119
120     }
121
122     if ((l == null)
123         ||
124         (r == null)
125        )
126     {
127
128         return false;
129         
130     }
131
132     return Utilities.matches (l,
133                   r,
134                   this.ignoreCase,
135                   this.type,
136                   false);
137
138     }
139
140     /**
141      * Return a string version of the expression.
142      * In the form: {@link Expression#toString() Expression} [ $ ] <|> [ = ] {@link Expression#toString() Expression}
143      *
144      * @return A string version of the expression.
145      */

146     public String JavaDoc toString ()
147     {
148
149     String JavaDoc pred = "<";
150
151     if (this.type == Utilities.GT)
152     {
153
154         pred = ">";
155
156     }
157
158     if (this.type == Utilities.GTE)
159     {
160
161         pred = ">=";
162
163     }
164
165     if (this.type == Utilities.LTE)
166     {
167
168         pred = "<=";
169
170     }
171
172     if (this.ignoreCase)
173     {
174
175         pred = "$" + pred;
176
177     }
178     
179     if (this.isBracketed ())
180     {
181
182         return "(" + this.left.toString () + " " + pred + " " + this.right.toString () + ")";
183
184     }
185
186     return this.left.toString () + " " + pred + " " + this.right.toString ();
187
188     }
189
190 }
191
Popular Tags