1 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 ; 37 import java.math.BigDecimal ; 38 import java.math.BigInteger ; 39 40 43 public class DivExpr extends Expr { 44 private final Expr _left; 45 private final Expr _right; 46 47 53 public DivExpr(Expr left, Expr right) 54 { 55 _left = left; 56 _right = right; 57 } 58 59 62 @Override 63 public boolean isConstant() 64 { 65 return _left.isConstant() && _right.isConstant(); 66 } 67 68 75 @Override 76 public Object getValue(ELContext env) 77 throws ELException 78 { 79 Object aObj = _left.getValue(env); 80 Object bObj = _right.getValue(env); 81 82 if (aObj instanceof BigDecimal 83 || bObj instanceof BigDecimal 84 || aObj instanceof BigInteger 85 || bObj instanceof BigInteger ) { 86 BigDecimal a = toBigDecimal(aObj, env); 87 BigDecimal b = toBigDecimal(bObj, env); 88 89 return a.divide(b, BigDecimal.ROUND_HALF_UP); 90 } 91 else if (aObj == null && bObj == null) 92 return new Long (0); 93 else { 94 double a = toDouble(aObj, env); 95 double b = toDouble(bObj, env); 96 double dValue = a / b; 97 98 return new Double (dValue); 99 } 100 } 101 102 109 @Override 110 public long evalLong(ELContext env) 111 throws ELException 112 { 113 double a = _left.evalDouble(env); 114 double b = _right.evalDouble(env); 115 116 return (long) (a / b + 0.5); 117 } 118 119 126 @Override 127 public double evalDouble(ELContext env) 128 throws ELException 129 { 130 double a = _left.evalDouble(env); 131 double b = _right.evalDouble(env); 132 133 return a / b; 134 } 135 136 141 @Override 142 public void printCreate(WriteStream os) 143 throws IOException 144 { 145 os.print("new com.caucho.el.DivExpr("); 146 _left.printCreate(os); 147 os.print(", "); 148 _right.printCreate(os); 149 os.print(")"); 150 } 151 152 155 public boolean equals(Object o) 156 { 157 if (! (o instanceof DivExpr)) 158 return false; 159 160 DivExpr expr = (DivExpr) o; 161 162 return (_left.equals(expr._left) && 163 _right.equals(expr._right)); 164 } 165 166 169 public String toString() 170 { 171 return "(" + _left + " + " + _right + ")"; 172 } 173 } 174 | Popular Tags |