KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > el > CmpExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.el;
31
32 import com.caucho.vfs.WriteStream;
33
34 import javax.el.ELContext;
35 import javax.el.ELException;
36 import java.io.IOException JavaDoc;
37
38 /**
39  * Represents a numeric comparison operation: lt, gt, le, ge.
40  */

41 public class CmpExpr extends AbstractBooleanExpr {
42   private int _op;
43   private Expr _left;
44   private Expr _right;
45
46   /**
47    * Creates a comparison expression
48    *
49    * @param op the lexical code for the operation
50    * @param left the left subexpression
51    * @param right the right subexpression
52    */

53   private CmpExpr(int op, Expr left, Expr right)
54   {
55     _op = op;
56     _left = left;
57     _right = right;
58   }
59
60   /**
61    * Creates a comparison expression
62    *
63    * @param op the lexical code for the operation
64    * @param left the left subexpression
65    * @param right the right subexpression
66    */

67   static Expr create(int op, Expr left, Expr right)
68   {
69     switch (op) {
70     case LT:
71       return new LtExpr(left, right);
72     case LE:
73       return new LeExpr(left, right);
74     case GT:
75       return new GtExpr(left, right);
76     case GE:
77       return new GeExpr(left, right);
78     case EQ:
79       return new EqExpr(left, right);
80     case NE:
81       return new NeExpr(left, right);
82     }
83
84     return new CmpExpr(op, left, right);
85   }
86
87   /**
88    * Returns true if this is a constant expression.
89    */

90   @Override JavaDoc
91   public boolean isConstant()
92   {
93     return _left.isConstant() && _right.isConstant();
94   }
95   
96   /**
97    * Evaluate the expression as a boolean.
98    *
99    * @param env the variable environment
100    */

101   @Override JavaDoc
102   public boolean evalBoolean(ELContext env)
103     throws ELException
104   {
105     Object JavaDoc aObj = _left.getValue(env);
106     Object JavaDoc bObj = _right.getValue(env);
107
108     if (aObj == null || bObj == null) {
109       // jsp/1b34, jsp/1b3d
110
return false;
111     }
112
113     if (aObj == bObj)
114       return _op == LE || _op == GE;
115
116     if (aObj instanceof Double JavaDoc || aObj instanceof Float JavaDoc ||
117         bObj instanceof Double JavaDoc || bObj instanceof Float JavaDoc) {
118       double a = toDouble(aObj, env);
119       double b = toDouble(bObj, env);
120
121       switch (_op) {
122       case LT: return a < b;
123       case LE: return a <= b;
124       case GT: return a > b;
125       case GE: return a >= b;
126       }
127     }
128     
129     if (aObj instanceof Number JavaDoc || bObj instanceof Number JavaDoc) {
130       //long a = toLong(aObj, env);
131
//long b = toLong(bObj, env);
132
// #1225 -- XXX: need to change type system
133
double a = toDouble(aObj, env);
134       double b = toDouble(bObj, env);
135
136       switch (_op) {
137       case LT: return a < b;
138       case LE: return a <= b;
139       case GT: return a > b;
140       case GE: return a >= b;
141       }
142     }
143
144     if (aObj instanceof String JavaDoc || bObj instanceof String JavaDoc) {
145       String JavaDoc a = toString(aObj, env);
146       String JavaDoc b = toString(bObj, env);
147
148       int cmp = a.compareTo(b);
149
150       switch (_op) {
151       case LT: return cmp < 0;
152       case LE: return cmp <= 0;
153       case GT: return cmp > 0;
154       case GE: return cmp >= 0;
155       }
156     }
157
158     if (aObj instanceof Comparable JavaDoc) {
159       int cmp = ((Comparable JavaDoc) aObj).compareTo(bObj);
160
161       switch (_op) {
162       case LT: return cmp < 0;
163       case LE: return cmp <= 0;
164       case GT: return cmp > 0;
165       case GE: return cmp >= 0;
166       }
167     }
168
169     if (bObj instanceof Comparable JavaDoc) {
170       int cmp = ((Comparable JavaDoc) bObj).compareTo(aObj);
171
172       switch (_op) {
173       case LT: return cmp > 0;
174       case LE: return cmp >= 0;
175       case GT: return cmp < 0;
176       case GE: return cmp <= 0;
177       }
178     }
179
180     ELException e = new ELException(L.l("can't compare {0} and {1}.",
181                                         aObj, bObj));
182
183     error(e, env);
184
185     return false;
186   }
187
188   /**
189    * Prints the code to create an LongLiteral.
190    */

191   @Override JavaDoc
192   public void printCreate(WriteStream os)
193     throws IOException JavaDoc
194   {
195     os.print("new com.caucho.el.CmpExpr(");
196     os.print(_op + ", ");
197     _left.printCreate(os);
198     os.print(", ");
199     _right.printCreate(os);
200     os.print(")");
201   }
202
203   /**
204    * Returns true for equal strings.
205    */

206   public boolean equals(Object JavaDoc o)
207   {
208     if (! (o instanceof CmpExpr))
209       return false;
210
211     CmpExpr expr = (CmpExpr) o;
212
213     return (_op == expr._op &&
214             _left.equals(expr._left) &&
215             _right.equals(expr._right));
216   }
217   
218   /**
219    * Returns a readable representation of the expr.
220    */

221   public String JavaDoc toString()
222   {
223     String JavaDoc op;
224
225     switch (_op) {
226     case LT:
227       op = " lt";
228       break;
229     case LE:
230       op = " le ";
231       break;
232     case GT:
233       op = " gt ";
234       break;
235     case GE:
236       op = " ge ";
237       break;
238     default:
239       op = " unknown(" + _op + ") ";
240       break;
241     }
242         
243     return "(" + _left + op + _right + ")";
244   }
245 }
246
Popular Tags