1 28 29 package com.caucho.el; 30 31 import com.caucho.vfs.WriteStream; 32 33 import javax.el.ELContext; 34 import javax.el.ELException; 35 import java.io.IOException ; 36 import java.math.BigDecimal ; 37 import java.math.BigInteger ; 38 39 42 public class MinusExpr extends Expr { 43 private final Expr _expr; 44 45 51 public MinusExpr(Expr expr) 52 { 53 _expr = expr; 54 } 55 56 59 @Override 60 public boolean isConstant() 61 { 62 return _expr.isConstant(); 63 } 64 65 72 @Override 73 public Object getValue(ELContext env) 74 throws ELException 75 { 76 Object obj = _expr.getValue(env); 77 78 if (obj == null) 79 return new Long (0); 80 81 Class type = obj.getClass(); 82 if (Long .class == type) 83 return new Long (- ((Number ) obj).longValue()); 84 else if (Double .class == type) 85 return new Double (- ((Number ) obj).doubleValue()); 86 else if (Integer .class == type) 87 return new Integer (- ((Number ) obj).intValue()); 88 else if (Short .class == type) 89 return new Short ((short) (- ((Number ) obj).shortValue())); 90 else if (Byte .class == type) 91 return new Byte ((byte) (- ((Number ) obj).byteValue())); 92 else if (Float .class == type) 93 return new Float (- ((Number ) obj).floatValue()); 94 else if (BigDecimal .class == type) 95 return ((BigDecimal ) obj).negate(); 96 else if (BigInteger .class == type) 97 return ((BigInteger ) obj).negate(); 98 else if (String .class == type && isDouble(obj)) 99 return new Double (- toDouble(obj, env)); 100 else if (String .class == type) 101 return new Long (- toLong(obj, env)); 102 else 103 throw new ELException(L.l("Can't convert {0} to number", obj)); 104 } 105 106 113 @Override 114 public long evalLong(ELContext env) 115 throws ELException 116 { 117 return - _expr.evalLong(env); 118 } 119 120 123 @Override 124 public double evalDouble(ELContext env) 125 throws ELException 126 { 127 return - _expr.evalDouble(env); 128 } 129 130 133 public void printCreate(WriteStream os) 134 throws IOException 135 { 136 os.print("new com.caucho.el.MinusExpr("); 137 _expr.printCreate(os); 138 os.print(")"); 139 } 140 141 144 public boolean equals(Object o) 145 { 146 if (! (o instanceof MinusExpr)) 147 return false; 148 149 MinusExpr uexpr = (MinusExpr) o; 150 151 return (_expr.equals(uexpr._expr)); 152 } 153 154 155 158 public String toString() 159 { 160 return "-" + _expr; 161 } 162 } 163 | Popular Tags |