001: /*
002: * Copyright 2004-2007 Gary Bentley
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may
005: * not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.josql.expressions;
016:
017: import org.josql.Query;
018: import org.josql.QueryExecutionException;
019: import org.josql.QueryParseException;
020:
021: /**
022: * Super-class of Expressions that return a binary result.
023: * <p>
024: * A binary expression must always have a LHS. The RHS is optional.
025: */
026: public abstract class BinaryExpression extends Expression {
027:
028: protected Expression left = null;
029: protected Expression right = null;
030:
031: /**
032: * Return whether this expression, and more specifically the left and right parts of
033: * the expression return a fixed result.
034: * Sub-classes may override this method for more tailored results, especially if the
035: * binary expression does not demand a RHS.
036: *
037: * @param q The Query object.
038: * @return <code>true<code> if the expression has a fixed result.
039: */
040: public boolean hasFixedResult(Query q) {
041:
042: boolean fr = true;
043:
044: if (this .right != null) {
045:
046: fr = this .right.hasFixedResult(q);
047:
048: }
049:
050: return this .left.hasFixedResult(q) && fr;
051:
052: }
053:
054: /**
055: * Return the expected return type from this expression.
056: *
057: * @param q The Query object.
058: * @return The class of the return type, this method ALWAYS returns <code>Boolean.class</code>.
059: */
060: public Class getExpectedReturnType(Query q) {
061:
062: return Boolean.class;
063:
064: }
065:
066: /**
067: * Init the expression. Sub-classes will often override this method.
068: * This method just calls: {@link Expression#init(Query)} on the LHS and RHS (if present) of the
069: * expression.
070: *
071: * @param q The Query object.
072: * @throws QueryParseException If the LHS and/or RHS cannot be inited.
073: */
074: public void init(Query q) throws QueryParseException {
075:
076: this .left.init(q);
077:
078: // There isn't always a RHS, for example IN expressions.
079: if (this .right != null) {
080:
081: this .right.init(q);
082:
083: }
084:
085: }
086:
087: /**
088: * Get the value of this expression. This will always return an instance of:
089: * <code>java.lang.Boolean</code> created as the result of a call to:
090: * {@link Expression#getValue(Object,Query)}.
091: *
092: * @param o The object to evaluate the expression on.
093: * @param q The Query object.
094: * @return An instance of Boolean.
095: * @throws QueryExecutionException If the expression cannot be evaluated.
096: */
097: public Object getValue(Object o, Query q)
098: throws QueryExecutionException {
099:
100: return Boolean.valueOf(this .isTrue(o, q));
101:
102: }
103:
104: /**
105: * Get the RHS.
106: *
107: * @return The RHS of the expression.
108: */
109: public Expression getRight() {
110:
111: return this .right;
112:
113: }
114:
115: /**
116: * Get the LHS.
117: *
118: * @return The LHS of the expression.
119: */
120: public Expression getLeft() {
121:
122: return this .left;
123:
124: }
125:
126: public void setLeft(Expression exp) {
127:
128: this .left = exp;
129:
130: }
131:
132: public void setRight(Expression exp) {
133:
134: this.right = exp;
135:
136: }
137:
138: }
|