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: import java.math.BigDecimal;
038: import java.math.BigInteger;
039:
040: /**
041: * Represents a binary multiplication numeric operation
042: */
043: public class MulExpr extends Expr {
044: private final Expr _left;
045: private final Expr _right;
046:
047: /**
048: * Creates the multiplication expression.
049: *
050: * @param left the left sub-expression
051: * @param right the right sub-expression
052: */
053: public MulExpr(Expr left, Expr right) {
054: _left = left;
055: _right = right;
056: }
057:
058: /**
059: * Returns true if this is a constant expression.
060: */
061: @Override
062: public boolean isConstant() {
063: return _left.isConstant() && _right.isConstant();
064: }
065:
066: /**
067: * Evaluate the expression as an object.
068: *
069: * @param env the variable environment
070: *
071: * @return the result as an object
072: */
073: @Override
074: public Object getValue(ELContext env) throws ELException {
075: Object aObj = _left.getValue(env);
076: Object bObj = _right.getValue(env);
077:
078: if (aObj instanceof BigDecimal || bObj instanceof BigDecimal) {
079: BigDecimal a = toBigDecimal(aObj, env);
080: BigDecimal b = toBigDecimal(bObj, env);
081:
082: return a.multiply(b);
083: } else if (isDouble(aObj)) {
084: if (bObj instanceof BigInteger) {
085: BigDecimal a = toBigDecimal(aObj, env);
086: BigDecimal b = toBigDecimal(bObj, env);
087:
088: return a.multiply(b);
089: } else {
090: double a = toDouble(aObj, env);
091: double b = toDouble(bObj, env);
092: double dValue = a * b;
093:
094: return Double.isNaN(dValue) ? new Double(0)
095: : new Double(dValue);
096: }
097: } else if (isDouble(bObj)) {
098: if (aObj instanceof BigInteger) {
099: BigDecimal a = toBigDecimal(aObj, env);
100: BigDecimal b = toBigDecimal(bObj, env);
101:
102: return a.multiply(b);
103: } else {
104: double a = toDouble(aObj, env);
105: double b = toDouble(bObj, env);
106: double dValue = a * b;
107:
108: return Double.isNaN(dValue) ? new Double(0)
109: : new Double(dValue);
110: }
111: } else if (aObj instanceof BigInteger
112: || bObj instanceof BigInteger) {
113: BigInteger a = toBigInteger(aObj, env);
114: BigInteger b = toBigInteger(bObj, env);
115:
116: return a.multiply(b);
117: }
118:
119: /*
120: if (aObj == null && bObj == null)
121: return new Long(0);
122: */
123:
124: if (bObj instanceof Double || bObj instanceof Float) {
125: double a = toDouble(aObj, env);
126: double b = ((Number) bObj).doubleValue();
127: double dValue = a * b;
128:
129: return Double.isNaN(dValue) ? new Double(0) : new Double(
130: dValue);
131: } else if (aObj instanceof Number) {
132: long a = ((Number) aObj).longValue();
133: long b = toLong(bObj, env);
134:
135: return new Long(a * b);
136: } else if (bObj instanceof Number) {
137: long a = toLong(aObj, env);
138: long b = ((Number) bObj).longValue();
139:
140: return new Long(a * b);
141: }
142:
143: if (isDoubleString(aObj) || isDoubleString(bObj)) {
144: double a = toDouble(aObj, env);
145: double b = toDouble(bObj, env);
146:
147: return new Double(a * b);
148: } else {
149: long a = toLong(aObj, env);
150: long b = toLong(bObj, env);
151:
152: return new Long(a * b);
153: }
154: }
155:
156: /**
157: * Evaluate the expression as a long
158: *
159: * @param env the variable environment
160: *
161: * @return the result as an long
162: */
163: @Override
164: public long evalLong(ELContext env) throws ELException {
165: long a = _left.evalLong(env);
166: long b = _right.evalLong(env);
167:
168: return a * b;
169: }
170:
171: /**
172: * Evaluate the expression as a double
173: *
174: * @param env the variable environment
175: *
176: * @return the result as an double
177: */
178: @Override
179: public double evalDouble(ELContext env) throws ELException {
180: double a = _left.evalDouble(env);
181: double b = _right.evalDouble(env);
182:
183: return a * b;
184: }
185:
186: /**
187: * Prints the Java code to recreate an LongLiteral.
188: *
189: * @param os the output stream to the *.java file
190: */
191: @Override
192: public void printCreate(WriteStream os) throws IOException {
193: os.print("new com.caucho.el.MulExpr(");
194: _left.printCreate(os);
195: os.print(", ");
196: _right.printCreate(os);
197: os.print(")");
198: }
199:
200: /**
201: * Returns true for equal strings.
202: */
203: public boolean equals(Object o) {
204: if (!(o instanceof MulExpr))
205: return false;
206:
207: MulExpr expr = (MulExpr) o;
208:
209: return (_left.equals(expr._left) && _right.equals(expr._right));
210: }
211:
212: /**
213: * Returns a readable representation of the expr.
214: */
215: public String toString() {
216: return "(" + _left + " + " + _right + ")";
217: }
218: }
|