KickJava   Java API By Example, From Geeks To Geeks.

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


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 a binary multiplication numeric operation
42  */

43 public class MulExpr extends Expr {
44   private final Expr _left;
45   private final Expr _right;
46   
47   /**
48    * Creates the multiplication expression.
49    *
50    * @param left the left sub-expression
51    * @param right the right sub-expression
52    */

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

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

75   @Override JavaDoc
76   public Object JavaDoc getValue(ELContext env)
77     throws ELException
78   {
79     Object JavaDoc aObj = _left.getValue(env);
80     Object JavaDoc bObj = _right.getValue(env);
81
82     if (aObj instanceof BigDecimal JavaDoc || bObj instanceof BigDecimal JavaDoc) {
83       BigDecimal JavaDoc a = toBigDecimal(aObj, env);
84       BigDecimal JavaDoc b = toBigDecimal(bObj, env);
85       
86       return a.multiply(b);
87     }
88     else if (isDouble(aObj)) {
89       if (bObj instanceof BigInteger JavaDoc) {
90     BigDecimal JavaDoc a = toBigDecimal(aObj, env);
91     BigDecimal JavaDoc b = toBigDecimal(bObj, env);
92
93     return a.multiply(b);
94       }
95       else {
96     double a = toDouble(aObj, env);
97     double b = toDouble(bObj, env);
98     double dValue = a * b;
99
100     return Double.isNaN(dValue) ? new Double JavaDoc(0) : new Double JavaDoc(dValue);
101       }
102     }
103     else if (isDouble(bObj)) {
104       if (aObj instanceof BigInteger JavaDoc) {
105     BigDecimal JavaDoc a = toBigDecimal(aObj, env);
106     BigDecimal JavaDoc b = toBigDecimal(bObj, env);
107
108     return a.multiply(b);
109       }
110       else {
111     double a = toDouble(aObj, env);
112     double b = toDouble(bObj, env);
113     double dValue = a * b;
114
115     return Double.isNaN(dValue) ? new Double JavaDoc(0) : new Double JavaDoc(dValue);
116       }
117     }
118     else if (aObj instanceof BigInteger JavaDoc || bObj instanceof BigInteger JavaDoc) {
119       BigInteger JavaDoc a = toBigInteger(aObj, env);
120       BigInteger JavaDoc b = toBigInteger(bObj, env);
121       
122       return a.multiply(b);
123     }
124
125     /*
126     if (aObj == null && bObj == null)
127       return new Long(0);
128     */

129
130     if (bObj instanceof Double JavaDoc || bObj instanceof Float JavaDoc) {
131       double a = toDouble(aObj, env);
132       double b = ((Number JavaDoc) bObj).doubleValue();
133       double dValue = a * b;
134
135       return Double.isNaN(dValue) ? new Double JavaDoc(0) : new Double JavaDoc(dValue);
136     }
137     else if (aObj instanceof Number JavaDoc) {
138       long a = ((Number JavaDoc) aObj).longValue();
139       long b = toLong(bObj, env);
140       
141       return new Long JavaDoc(a * b);
142     }
143     else if (bObj instanceof Number JavaDoc) {
144       long a = toLong(aObj, env);
145       long b = ((Number JavaDoc) bObj).longValue();
146
147       return new Long JavaDoc(a * b);
148     }
149
150     if (isDoubleString(aObj) || isDoubleString(bObj)) {
151       double a = toDouble(aObj, env);
152       double b = toDouble(bObj, env);
153     
154       return new Double JavaDoc(a * b);
155     }
156     else {
157       long a = toLong(aObj, env);
158       long b = toLong(bObj, env);
159
160       return new Long JavaDoc(a * b);
161     }
162   }
163   
164   /**
165    * Evaluate the expression as a long
166    *
167    * @param env the variable environment
168    *
169    * @return the result as an long
170    */

171   @Override JavaDoc
172   public long evalLong(ELContext env)
173     throws ELException
174   {
175     long a = _left.evalLong(env);
176     long b = _right.evalLong(env);
177
178     return a * b;
179   }
180   
181   /**
182    * Evaluate the expression as a double
183    *
184    * @param env the variable environment
185    *
186    * @return the result as an double
187    */

188   @Override JavaDoc
189   public double evalDouble(ELContext env)
190     throws ELException
191   {
192     double a = _left.evalDouble(env);
193     double b = _right.evalDouble(env);
194
195     return a * b;
196   }
197
198   /**
199    * Prints the Java code to recreate an LongLiteral.
200    *
201    * @param os the output stream to the *.java file
202    */

203   @Override JavaDoc
204   public void printCreate(WriteStream os)
205     throws IOException JavaDoc
206   {
207     os.print("new com.caucho.el.MulExpr(");
208     _left.printCreate(os);
209     os.print(", ");
210     _right.printCreate(os);
211     os.print(")");
212   }
213
214   /**
215    * Returns true for equal strings.
216    */

217   public boolean equals(Object JavaDoc o)
218   {
219     if (! (o instanceof MulExpr))
220       return false;
221
222     MulExpr expr = (MulExpr) o;
223
224     return (_left.equals(expr._left) &&
225             _right.equals(expr._right));
226   }
227   
228   /**
229    * Returns a readable representation of the expr.
230    */

231   public String JavaDoc toString()
232   {
233     return "(" + _left + " + " + _right + ")";
234   }
235 }
236
Popular Tags