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 ModExpr extends Expr { 44 private final Expr _left; 45 private final Expr _right; 46 47 53 public ModExpr(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 || isDouble(aObj) 84 || bObj instanceof BigDecimal 85 || isDouble(bObj)) { 86 double a = toDouble(aObj, env); 87 double b = toDouble(bObj, env); 88 89 return new Double (a % b); 90 } 91 else if (aObj instanceof BigInteger 92 || bObj instanceof BigInteger ) { 93 BigInteger a = toBigInteger(aObj, env); 94 BigInteger b = toBigInteger(bObj, env); 95 96 return a.remainder(b); 97 } 98 else if (aObj == null && bObj == null) 99 return new Long (0); 100 else { 101 long a = toLong(aObj, env); 102 long b = toLong(bObj, env); 103 104 return new Long (a % b); 105 } 106 } 107 108 115 @Override 116 public long evalLong(ELContext env) 117 throws ELException 118 { 119 long a = _left.evalLong(env); 120 long b = _right.evalLong(env); 121 122 return a % b; 123 } 124 125 132 @Override 133 public double evalDouble(ELContext env) 134 throws ELException 135 { 136 double a = _left.evalDouble(env); 137 double b = _right.evalDouble(env); 138 139 return a % b; 140 } 141 142 147 @Override 148 public void printCreate(WriteStream os) 149 throws IOException 150 { 151 os.print("new com.caucho.el.ModExpr("); 152 _left.printCreate(os); 153 os.print(", "); 154 _right.printCreate(os); 155 os.print(")"); 156 } 157 158 161 public boolean equals(Object o) 162 { 163 if (! (o instanceof ModExpr)) 164 return false; 165 166 ModExpr expr = (ModExpr) o; 167 168 return (_left.equals(expr._left) && 169 _right.equals(expr._right)); 170 } 171 172 175 public String toString() 176 { 177 return "(" + _left + " + " + _right + ")"; 178 } 179 } 180 | Popular Tags |