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:
020: /**
021: * Represents an "IS NULL" (or "IS NOT NULL") expression.
022: */
023: public class IsNullExpression extends BinaryExpression {
024:
025: private boolean not = false;
026:
027: public boolean isNot() {
028:
029: return this .not;
030:
031: }
032:
033: public void setNot(boolean v) {
034:
035: this .not = v;
036:
037: }
038:
039: /**
040: * Return a string representation of this expression.
041: * In the form: {@link Expression#toString()} IS [ NOT ] NULL
042: *
043: * @return A string representation of this expression.
044: */
045: public String toString() {
046:
047: StringBuffer buf = new StringBuffer("IS ");
048:
049: if (this .not) {
050:
051: buf.append("NOT ");
052:
053: }
054:
055: buf.append(this .left.toString());
056:
057: if (this .isBracketed()) {
058:
059: buf.insert(0, "(");
060: buf.append(")");
061:
062: }
063:
064: return buf.toString();
065:
066: }
067:
068: /**
069: * Determine whether the LHS of this expression is or is not null.
070: * Note that this is equivalent to: <code>LHS = null</code> or:
071: * <code>LHS != null</code>.
072: *
073: * @param o The current object to perform the expression on.
074: * @param q The Query object.
075: * @return <code>true</code> if the LHS is null (or not null is specified).
076: * @throws QueryExecutionException If the expression cannot be evaluated.
077: */
078: public boolean isTrue(Object o, Query q)
079: throws QueryExecutionException {
080:
081: // Get the left...
082: o = this .left.getValue(o, q);
083:
084: if (o == null) {
085:
086: if (this .not) {
087:
088: return false;
089:
090: }
091:
092: return true;
093:
094: }
095:
096: if (this .not) {
097:
098: return true;
099:
100: }
101:
102: return false;
103:
104: }
105:
106: }
|