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 divtiplication numeric operation
042: */
043: public class DivExpr 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 DivExpr(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: || aObj instanceof BigInteger
080: || bObj instanceof BigInteger) {
081: BigDecimal a = toBigDecimal(aObj, env);
082: BigDecimal b = toBigDecimal(bObj, env);
083:
084: return a.divide(b, BigDecimal.ROUND_HALF_UP);
085: } else if (aObj == null && bObj == null)
086: return new Long(0);
087: else {
088: double a = toDouble(aObj, env);
089: double b = toDouble(bObj, env);
090: double dValue = a / b;
091:
092: return new Double(dValue);
093: }
094: }
095:
096: /**
097: * Evaluate the expression as a long
098: *
099: * @param env the variable environment
100: *
101: * @return the result as an long
102: */
103: @Override
104: public long evalLong(ELContext env) throws ELException {
105: double a = _left.evalDouble(env);
106: double b = _right.evalDouble(env);
107:
108: return (long) (a / b + 0.5);
109: }
110:
111: /**
112: * Evaluate the expression as a double
113: *
114: * @param env the variable environment
115: *
116: * @return the result as an double
117: */
118: @Override
119: public double evalDouble(ELContext env) throws ELException {
120: double a = _left.evalDouble(env);
121: double b = _right.evalDouble(env);
122:
123: return a / b;
124: }
125:
126: /**
127: * Prints the Java code to recreate an LongLiteral.
128: *
129: * @param os the output stream to the *.java file
130: */
131: @Override
132: public void printCreate(WriteStream os) throws IOException {
133: os.print("new com.caucho.el.DivExpr(");
134: _left.printCreate(os);
135: os.print(", ");
136: _right.printCreate(os);
137: os.print(")");
138: }
139:
140: /**
141: * Returns true for equal strings.
142: */
143: public boolean equals(Object o) {
144: if (!(o instanceof DivExpr))
145: return false;
146:
147: DivExpr expr = (DivExpr) o;
148:
149: return (_left.equals(expr._left) && _right.equals(expr._right));
150: }
151:
152: /**
153: * Returns a readable representation of the expr.
154: */
155: public String toString() {
156: return "(" + _left + " + " + _right + ")";
157: }
158: }
|