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