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:
019: /**
020: * This class represents a "boolean" expression, either <code>true</code> or <code>false</code>.
021: */
022: public class BooleanExpression extends ValueExpression {
023:
024: private Boolean value = null;
025:
026: public BooleanExpression() {
027:
028: }
029:
030: /**
031: * Always returns <code>true</code> since it represents a constant.
032: *
033: * @param q The Query object.
034: * @return <code>true</code> always.
035: */
036: public boolean hasFixedResult(Query q) {
037:
038: return true;
039:
040: }
041:
042: /**
043: * Returns a string version of this expression.
044: * Basically returns: true | false.
045: *
046: * @return A string version of this expression.
047: */
048: public String toString() {
049:
050: if (this .isBracketed()) {
051:
052: return "(" + this .value.toString() + ")";
053:
054: }
055:
056: return this .value.toString();
057:
058: }
059:
060: /**
061: * Get the expected return type.
062: *
063: * @param q The Query object.
064: * @return Always returns: <code>java.lang.Boolean.TYPE</code>.
065: */
066: public Class getExpectedReturnType(Query q) {
067:
068: return Boolean.TYPE;
069:
070: }
071:
072: /**
073: * Init this expression. Actually does nothing since it is a constant.
074: *
075: * @param q The Query object.
076: */
077: public void init(Query q) {
078:
079: // Nothing to do...
080:
081: }
082:
083: public void setValue(Boolean b) {
084:
085: this .value = b;
086:
087: }
088:
089: /**
090: * Returns whether this expression is <code>true</code> or <code>false</code>.
091: *
092: * @param o The current object, not used in this method.
093: * @param q The Query object, not used in this method.
094: * @return The value of this expression.
095: */
096: public boolean isTrue(Object o, Query q) {
097:
098: return this .value.booleanValue();
099:
100: }
101:
102: /**
103: * Get the value of this boolean.
104: *
105: * @param o The current object, not used in this method.
106: * @param q The Query object, not used in this method.
107: * @return The value of this expression.
108: */
109: public Object getValue(Object o, Query q) {
110:
111: return this .value;
112:
113: }
114:
115: /**
116: * Get the value of this boolean.
117: *
118: * @param o The current object, not used in this method.
119: * @param q The Query object, not used in this method.
120: * @return The value of this expression.
121: */
122: public Object evaluate(Object o, Query q) {
123:
124: return this.value;
125:
126: }
127:
128: }
|