KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.josql.QueryParseException;
20
21 /**
22  * Super-class of Expressions that return a binary result.
23  * <p>
24  * A binary expression must always have a LHS. The RHS is optional.
25  * <p>
26  * Last Modified By: $Author: barrygently $<br />
27  * Last Modified On: $Date: 2004/12/20 16:22:43 $<br />
28  * Current Revision: $Revision: 1.2 $<br />
29  */

30 public abstract class BinaryExpression extends Expression
31 {
32
33     protected Expression left = null;
34     protected Expression right = null;
35
36     /**
37      * Return whether this expression, and more specifically the left and right parts of
38      * the expression return a fixed result.
39      * Sub-classes may override this method for more tailored results, especially if the
40      * binary expression does not demand a RHS.
41      *
42      * @param q The Query object.
43      * @return <code>true<code> if the expression has a fixed result.
44      */

45     public boolean hasFixedResult (Query q)
46     {
47
48     boolean fr = true;
49
50     if (this.right != null)
51     {
52
53         fr = this.right.hasFixedResult (q);
54
55     }
56
57     return this.left.hasFixedResult (q) && fr;
58
59     }
60
61     /**
62      * Return the expected return type from this expression.
63      *
64      * @param q The Query object.
65      * @return The class of the return type, this method ALWAYS returns <code>Boolean.class</code>.
66      */

67     public Class JavaDoc getExpectedReturnType (Query q)
68     {
69
70     return Boolean JavaDoc.class;
71
72     }
73
74     /**
75      * Init the expression. Sub-classes will often override this method.
76      * This method just calls: {@link Expression#init(Query)} on the LHS and RHS (if present) of the
77      * expression.
78      *
79      * @param q The Query object.
80      * @throws QueryParseException If the LHS and/or RHS cannot be inited.
81      */

82     public void init (Query q)
83                   throws QueryParseException
84     {
85
86     this.left.init (q);
87
88     // There isn't always a RHS, for example IN expressions.
89
if (this.right != null)
90     {
91
92         this.right.init (q);
93
94     }
95
96     }
97
98     /**
99      * Get the value of this expression. This will always return an instance of:
100      * <code>java.lang.Boolean</code> created as the result of a call to:
101      * {@link Expression#getValue(Object,Query)}.
102      *
103      * @param o The object to evaluate the expression on.
104      * @param q The Query object.
105      * @return An instance of Boolean.
106      * @throws QueryExecutionException If the expression cannot be evaluated.
107      */

108     public Object JavaDoc getValue (Object JavaDoc o,
109                 Query q)
110                         throws QueryExecutionException
111     {
112
113     return Boolean.valueOf (this.isTrue (o,
114                          q));
115
116     }
117
118     /**
119      * Get the RHS.
120      *
121      * @return The RHS of the expression.
122      */

123     public Expression getRight ()
124     {
125
126     return this.right;
127
128     }
129
130     /**
131      * Get the LHS.
132      *
133      * @return The LHS of the expression.
134      */

135     public Expression getLeft ()
136     {
137
138     return this.left;
139
140     }
141
142     public void setLeft (Expression exp)
143     {
144
145     this.left = exp;
146
147     }
148
149     public void setRight (Expression exp)
150     {
151
152     this.right = exp;
153
154     }
155
156 }
157
Popular Tags