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 AddExpr extends Expr { 44 private final Expr _left; 45 private final Expr _right; 46 47 53 public AddExpr(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 || bObj instanceof BigDecimal ) { 83 BigDecimal a = toBigDecimal(aObj, env); 84 BigDecimal b = toBigDecimal(bObj, env); 85 86 return a.add(b); 87 } 88 else if (isDouble(aObj)) { 89 if (bObj instanceof BigInteger ) { 90 BigDecimal a = toBigDecimal(aObj, env); 91 BigDecimal b = toBigDecimal(bObj, env); 92 93 return a.add(b); 94 } 95 else { 96 double a = toDouble(aObj, env); 97 double b = toDouble(bObj, env); 98 double dValue = a + b; 99 100 return Double.isNaN(dValue) ? new Double (0) : new Double (dValue); 101 } 102 } 103 else if (isDouble(bObj)) { 104 if (aObj instanceof BigInteger ) { 105 BigDecimal a = toBigDecimal(aObj, env); 106 BigDecimal b = toBigDecimal(bObj, env); 107 108 return a.add(b); 109 } 110 else { 111 double a = toDouble(aObj, env); 112 double b = toDouble(bObj, env); 113 double dValue = a + b; 114 115 return Double.isNaN(dValue) ? new Double (0) : new Double (dValue); 116 } 117 } 118 else if (aObj instanceof BigInteger || bObj instanceof BigInteger ) { 119 BigInteger a = toBigInteger(aObj, env); 120 BigInteger b = toBigInteger(bObj, env); 121 122 return a.add(b); 123 } 124 125 129 130 if (bObj instanceof Double || bObj instanceof Float ) { 131 double a = toDouble(aObj, env); 132 double b = ((Number ) bObj).doubleValue(); 133 double dValue = a + b; 134 135 return Double.isNaN(dValue) ? new Double (0) : new Double (dValue); 136 } 137 else if (aObj instanceof Number ) { 138 long a = ((Number ) aObj).longValue(); 139 long b = toLong(bObj, env); 140 141 return new Long (a + b); 142 } 143 else if (bObj instanceof Number ) { 144 long a = toLong(aObj, env); 145 long b = ((Number ) bObj).longValue(); 146 147 return new Long (a + b); 148 } 149 150 if (isDoubleString(aObj) || isDoubleString(bObj)) { 151 double a = toDouble(aObj, env); 152 double b = toDouble(bObj, env); 153 154 return new Double (a + b); 155 } 156 else { 157 long a = toLong(aObj, env); 158 long b = toLong(bObj, env); 159 160 return new Long (a + b); 161 } 162 } 163 164 171 @Override 172 public long evalLong(ELContext env) 173 throws ELException 174 { 175 long a = _left.evalLong(env); 176 long b = _right.evalLong(env); 177 178 return a + b; 179 } 180 181 188 @Override 189 public double evalDouble(ELContext env) 190 throws ELException 191 { 192 double a = _left.evalDouble(env); 193 double b = _right.evalDouble(env); 194 195 return a + b; 196 } 197 198 203 @Override 204 public void printCreate(WriteStream os) 205 throws IOException 206 { 207 os.print("new com.caucho.el.AddExpr("); 208 _left.printCreate(os); 209 os.print(", "); 210 _right.printCreate(os); 211 os.print(")"); 212 } 213 214 217 public boolean equals(Object o) 218 { 219 if (! (o instanceof AddExpr)) 220 return false; 221 222 AddExpr expr = (AddExpr) o; 223 224 return (_left.equals(expr._left) && 225 _right.equals(expr._right)); 226 } 227 228 231 public String toString() 232 { 233 return "(" + _left + " + " + _right + ")"; 234 } 235 } 236 | Popular Tags |