KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.math.BigDecimal JavaDoc;
38 import java.math.BigInteger JavaDoc;
39
40 /**
41  * Represents the numeric comparison operation: le
42  */

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

54   public LeExpr(Expr left, Expr right)
55   {
56     _left = left;
57     _right = right;
58   }
59
60   /**
61    * Returns true if this is a constant expression.
62    */

63   @Override JavaDoc
64   public boolean isConstant()
65   {
66     return _left.isConstant() && _right.isConstant();
67   }
68   
69   /**
70    * Evaluate the expression as a boolean.
71    *
72    * @param env the variable environment
73    */

74   @Override JavaDoc
75   public boolean evalBoolean(ELContext env)
76     throws ELException
77   {
78     Object JavaDoc aObj = _left.getValue(env);
79     Object JavaDoc bObj = _right.getValue(env);
80
81     if (aObj == bObj)
82       return true;
83
84     if (aObj == null || bObj == null)
85       return false;
86
87     Class JavaDoc aType = aObj.getClass();
88     Class JavaDoc bType = bObj.getClass();
89     
90     if (aObj instanceof BigDecimal JavaDoc || bObj instanceof BigDecimal JavaDoc) {
91       BigDecimal JavaDoc a = toBigDecimal(aObj, env);
92       BigDecimal JavaDoc b = toBigDecimal(bObj, env);
93
94       return a.compareTo(b) <= 0;
95     }
96     
97     if (aType == Double JavaDoc.class || aType == Float JavaDoc.class ||
98         bType == Double JavaDoc.class || bType == Float JavaDoc.class) {
99       double a = toDouble(aObj, env);
100       double b = toDouble(bObj, env);
101
102       return a <= b;
103     }
104     
105     if (aType == BigInteger JavaDoc.class || bType == BigInteger JavaDoc.class) {
106       BigInteger JavaDoc a = toBigInteger(aObj, env);
107       BigInteger JavaDoc b = toBigInteger(bObj, env);
108
109       return a.compareTo(b) <= 0;
110     }
111     
112     if (aObj instanceof Number JavaDoc || bObj instanceof Number JavaDoc) {
113       long a = toLong(aObj, env);
114       long b = toLong(bObj, env);
115
116       return a <= b;
117     }
118
119     if (aObj instanceof String JavaDoc || bObj instanceof String JavaDoc) {
120       String JavaDoc a = toString(aObj, env);
121       String JavaDoc b = toString(bObj, env);
122
123       return a.compareTo(b) <= 0;
124     }
125
126     if (aObj instanceof Comparable JavaDoc) {
127       int cmp = ((Comparable JavaDoc) aObj).compareTo(bObj);
128
129       return cmp <= 0;
130     }
131
132     if (bObj instanceof Comparable JavaDoc) {
133       int cmp = ((Comparable JavaDoc) bObj).compareTo(aObj);
134
135       return cmp >= 0;
136     }
137
138     ELException e = new ELException(L.l("can't compare {0} and {1}.",
139                                         aObj, bObj));
140
141     error(e, env);
142
143     return false;
144   }
145
146   /**
147    * Prints the code to create an LongLiteral.
148    */

149   @Override JavaDoc
150   public void printCreate(WriteStream os)
151     throws IOException JavaDoc
152   {
153     os.print("new com.caucho.el.LeExpr(");
154     _left.printCreate(os);
155     os.print(", ");
156     _right.printCreate(os);
157     os.print(")");
158   }
159
160   /**
161    * Returns true for equal strings.
162    */

163   public boolean equals(Object JavaDoc o)
164   {
165     if (! (o instanceof LeExpr))
166       return false;
167
168     LeExpr expr = (LeExpr) o;
169
170     return (_left.equals(expr._left) &&
171             _right.equals(expr._right));
172   }
173   
174   /**
175    * Returns a readable representation of the expr.
176    */

177   public String JavaDoc toString()
178   {
179     return "(" + _left + " le " + _right + ")";
180   }
181 }
182
Popular Tags