001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.el;
031:
032: import com.caucho.vfs.WriteStream;
033:
034: import javax.el.ELContext;
035: import javax.el.ELException;
036: import java.io.IOException;
037:
038: /**
039: * Represents a numeric comparison operation: lt, gt, le, ge.
040: */
041: public class CmpExpr extends AbstractBooleanExpr {
042: private int _op;
043: private Expr _left;
044: private Expr _right;
045:
046: /**
047: * Creates a comparison expression
048: *
049: * @param op the lexical code for the operation
050: * @param left the left subexpression
051: * @param right the right subexpression
052: */
053: private CmpExpr(int op, Expr left, Expr right) {
054: _op = op;
055: _left = left;
056: _right = right;
057: }
058:
059: /**
060: * Creates a comparison expression
061: *
062: * @param op the lexical code for the operation
063: * @param left the left subexpression
064: * @param right the right subexpression
065: */
066: static Expr create(int op, Expr left, Expr right) {
067: switch (op) {
068: case LT:
069: return new LtExpr(left, right);
070: case LE:
071: return new LeExpr(left, right);
072: case GT:
073: return new GtExpr(left, right);
074: case GE:
075: return new GeExpr(left, right);
076: case EQ:
077: return new EqExpr(left, right);
078: case NE:
079: return new NeExpr(left, right);
080: }
081:
082: return new CmpExpr(op, left, right);
083: }
084:
085: /**
086: * Returns true if this is a constant expression.
087: */
088: @Override
089: public boolean isConstant() {
090: return _left.isConstant() && _right.isConstant();
091: }
092:
093: /**
094: * Evaluate the expression as a boolean.
095: *
096: * @param env the variable environment
097: */
098: @Override
099: public boolean evalBoolean(ELContext env) throws ELException {
100: Object aObj = _left.getValue(env);
101: Object bObj = _right.getValue(env);
102:
103: if (aObj == null || bObj == null) {
104: // jsp/1b34, jsp/1b3d
105: return false;
106: }
107:
108: if (aObj == bObj)
109: return _op == LE || _op == GE;
110:
111: if (aObj instanceof Double || aObj instanceof Float
112: || bObj instanceof Double || bObj instanceof Float) {
113: double a = toDouble(aObj, env);
114: double b = toDouble(bObj, env);
115:
116: switch (_op) {
117: case LT:
118: return a < b;
119: case LE:
120: return a <= b;
121: case GT:
122: return a > b;
123: case GE:
124: return a >= b;
125: }
126: }
127:
128: if (aObj instanceof Number || bObj instanceof Number) {
129: //long a = toLong(aObj, env);
130: //long b = toLong(bObj, env);
131: // #1225 -- XXX: need to change type system
132: double a = toDouble(aObj, env);
133: double b = toDouble(bObj, env);
134:
135: switch (_op) {
136: case LT:
137: return a < b;
138: case LE:
139: return a <= b;
140: case GT:
141: return a > b;
142: case GE:
143: return a >= b;
144: }
145: }
146:
147: if (aObj instanceof String || bObj instanceof String) {
148: String a = toString(aObj, env);
149: String b = toString(bObj, env);
150:
151: int cmp = a.compareTo(b);
152:
153: switch (_op) {
154: case LT:
155: return cmp < 0;
156: case LE:
157: return cmp <= 0;
158: case GT:
159: return cmp > 0;
160: case GE:
161: return cmp >= 0;
162: }
163: }
164:
165: if (aObj instanceof Comparable) {
166: int cmp = ((Comparable) aObj).compareTo(bObj);
167:
168: switch (_op) {
169: case LT:
170: return cmp < 0;
171: case LE:
172: return cmp <= 0;
173: case GT:
174: return cmp > 0;
175: case GE:
176: return cmp >= 0;
177: }
178: }
179:
180: if (bObj instanceof Comparable) {
181: int cmp = ((Comparable) bObj).compareTo(aObj);
182:
183: switch (_op) {
184: case LT:
185: return cmp > 0;
186: case LE:
187: return cmp >= 0;
188: case GT:
189: return cmp < 0;
190: case GE:
191: return cmp <= 0;
192: }
193: }
194:
195: ELException e = new ELException(L.l(
196: "can't compare {0} and {1}.", aObj, bObj));
197:
198: error(e, env);
199:
200: return false;
201: }
202:
203: /**
204: * Prints the code to create an LongLiteral.
205: */
206: @Override
207: public void printCreate(WriteStream os) throws IOException {
208: os.print("new com.caucho.el.CmpExpr(");
209: os.print(_op + ", ");
210: _left.printCreate(os);
211: os.print(", ");
212: _right.printCreate(os);
213: os.print(")");
214: }
215:
216: /**
217: * Returns true for equal strings.
218: */
219: public boolean equals(Object o) {
220: if (!(o instanceof CmpExpr))
221: return false;
222:
223: CmpExpr expr = (CmpExpr) o;
224:
225: return (_op == expr._op && _left.equals(expr._left) && _right
226: .equals(expr._right));
227: }
228:
229: /**
230: * Returns a readable representation of the expr.
231: */
232: public String toString() {
233: String op;
234:
235: switch (_op) {
236: case LT:
237: op = " lt";
238: break;
239: case LE:
240: op = " le ";
241: break;
242: case GT:
243: op = " gt ";
244: break;
245: case GE:
246: op = " ge ";
247: break;
248: default:
249: op = " unknown(" + _op + ") ";
250: break;
251: }
252:
253: return "(" + _left + op + _right + ")";
254: }
255: }
|