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: import org.josql.internal.Utilities;
021:
022: /**
023: * This class represents one of the following:
024: * <ul>
025: * <li><b>></b> - Greater than</li>
026: * <li><b><</b> - Less than</li>
027: * <li><b>>=</b> - Greater than or equal to</li>
028: * <li><b><=</b> - Less than or equal to</li>
029: * </ul>
030: * <p>
031: * You can also force a "string" comparison by prefixing with "$". This will force
032: * both sides to be strings via the <code>toString</code> method.
033: */
034: public class GTLTExpression extends BinaryExpression {
035:
036: private int type = -1;
037: private boolean ignoreCase = false;
038:
039: public int getType() {
040:
041: return this .type;
042:
043: }
044:
045: /**
046: * Type is an integer here for speed purposes, however one of the constants
047: * should be used.
048: *
049: * @param t The type of expression.
050: */
051: public void setType(int t) {
052:
053: this .type = t;
054:
055: }
056:
057: public void setIgnoreCase(boolean v) {
058:
059: this .ignoreCase = v;
060:
061: }
062:
063: public boolean isIgnoreCase() {
064:
065: return this .ignoreCase;
066:
067: }
068:
069: /**
070: * Return whether this expression evaluates to true. The actual comparison
071: * is performed by: {@link Utilities#compare(Object,Object)} which copes with
072: * the object types.
073: *
074: * @param o The current object to evaluate the expression on.
075: * @param q The Query object.
076: * @return <code>true</code> if the expression evaluates to <code>true</code>.
077: * @throws QueryExecutionException If the expression cannot be evaluated.
078: */
079: public boolean isTrue(Object o, Query q)
080: throws QueryExecutionException {
081:
082: // Get the lhs.
083: Object l = this .left.getValue(o, q);
084:
085: Object r = this .right.getValue(o, q);
086:
087: if ((l == null) && (r == null)) {
088:
089: if ((this .type == 1) || (this .type == 3)) {
090:
091: return true;
092:
093: }
094:
095: }
096:
097: if ((l == null) || (r == null)) {
098:
099: return false;
100:
101: }
102:
103: return Utilities.matches(l, r, this .ignoreCase, this .type,
104: false);
105:
106: }
107:
108: /**
109: * Return a string version of the expression.
110: * In the form: {@link Expression#toString() Expression} [ $ ] <|> [ = ] {@link Expression#toString() Expression}
111: *
112: * @return A string version of the expression.
113: */
114: public String toString() {
115:
116: String pred = "<";
117:
118: if (this .type == Utilities.GT) {
119:
120: pred = ">";
121:
122: }
123:
124: if (this .type == Utilities.GTE) {
125:
126: pred = ">=";
127:
128: }
129:
130: if (this .type == Utilities.LTE) {
131:
132: pred = "<=";
133:
134: }
135:
136: if (this .ignoreCase) {
137:
138: pred = "$" + pred;
139:
140: }
141:
142: if (this .isBracketed()) {
143:
144: return "(" + this .left.toString() + " " + pred + " "
145: + this .right.toString() + ")";
146:
147: }
148:
149: return this .left.toString() + " " + pred + " "
150: + this.right.toString();
151:
152: }
153:
154: }
|