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 constant String or number.
021: * ALL numbers in JoSQL are represented by <b>double</b>.
022: */
023: public class ConstantExpression extends ValueExpression {
024:
025: private Object val = null;
026:
027: /**
028: * Get the expected return type.
029: * Will be either: <code>java.lang.String.class</code>
030: * or: <code>java.lang.Double.class</code>.
031: *
032: * @param q The Query object.
033: * @return The expected class.
034: */
035: public Class getExpectedReturnType(Query q) {
036:
037: if (this .val == null) {
038:
039: return null;
040:
041: }
042:
043: return this .val.getClass();
044:
045: }
046:
047: /**
048: * Inits the expression, in reality does nothing here, can't init a constant!
049: *
050: * @param q The Query object.
051: */
052: public void init(Query q) {
053:
054: // Nothing to do...
055:
056: }
057:
058: public void setValue(Object v) {
059:
060: this .val = v;
061:
062: }
063:
064: /**
065: * Returns whether the value of this constant represents a <code>true</code>
066: * value. See: {@link ArithmeticExpression#isTrue(Object,Query)} for details of how
067: * the return value is determined.
068: *
069: * @param o The current object. Not used in this method.
070: * @param q The Query object.
071: * @return <code>true</code> if the constant evaluates to <code>true</code>.
072: */
073: public boolean isTrue(Object o, Query q) {
074:
075: if (this .val == null) {
076:
077: return false;
078:
079: }
080:
081: if (this .val instanceof Boolean) {
082:
083: return ((Boolean) this .val).booleanValue();
084:
085: }
086:
087: if (this .val instanceof Number) {
088:
089: return ((Number) this .val).doubleValue() > 0;
090:
091: }
092:
093: return true;
094:
095: }
096:
097: /**
098: * Get the value of this constant.
099: *
100: * @param o The current object, not used in this method.
101: * @param q The Query object, not used in this method.
102: * @return The constant value.
103: */
104: public Object getValue(Object o, Query q) {
105:
106: return this .val;
107:
108: }
109:
110: /**
111: * Get the value of this constant.
112: *
113: * @param o The current object, not used in this method.
114: * @param q The Query object, not used in this method.
115: * @return The constant value.
116: */
117: public Object evaluate(Object o, Query q) {
118:
119: return this .val;
120:
121: }
122:
123: /**
124: * Always returns <code>true</code>, well duh!
125: *
126: * @param q The Query object.
127: * @return <code>true</code> always.
128: */
129: public boolean hasFixedResult(Query q) {
130:
131: // Well duh...
132: return true;
133:
134: }
135:
136: /**
137: * Returns a string representation of this constant.
138: * If the constant is a String then it should be noted that the original
139: * quoting character is not kept in this class and the output of this method
140: * will contain the character "'" instead.
141: * If the constant is a number then "toString" is called on it, so any ","
142: * or other number formatting will have been lost.
143: *
144: * @return A string representation of this constant.
145: */
146: public String toString() {
147:
148: String v = null;
149:
150: if (this .val == null) {
151:
152: v = val + "";
153:
154: } else {
155:
156: if (this .val instanceof String) {
157:
158: v = "'" + val + "'";
159:
160: } else {
161:
162: v = this .val.toString();
163:
164: }
165:
166: }
167:
168: if (this .isBracketed()) {
169:
170: v = "(" + v + ")";
171:
172: }
173:
174: return v;
175:
176: }
177:
178: }
|