1 16 package org.apache.commons.jexl.parser; 17 18 import java.math.BigDecimal ; 19 import java.math.BigInteger ; 20 21 import org.apache.commons.jexl.JexlContext; 22 23 29 public class ASTUnaryMinusNode extends SimpleNode { 30 35 public ASTUnaryMinusNode(int id) { 36 super(id); 37 } 38 39 45 public ASTUnaryMinusNode(Parser p, int id) { 46 super(p, id); 47 } 48 49 50 public Object jjtAccept(ParserVisitor visitor, Object data) { 51 return visitor.visit(this, data); 52 } 53 54 55 public Object value(JexlContext jc) throws Exception { 56 Object val = ((SimpleNode) jjtGetChild(0)).value(jc); 57 58 if (val instanceof Byte ) { 59 byte valueAsByte = ((Byte ) val).byteValue(); 60 return new Byte ((byte) -valueAsByte); 61 } else if (val instanceof Short ) { 62 short valueAsShort = ((Short ) val).shortValue(); 63 return new Short ((short) -valueAsShort); 64 } else if (val instanceof Integer ) { 65 int valueAsInt = ((Integer ) val).intValue(); 66 return new Integer (-valueAsInt); 67 } else if (val instanceof Long ) { 68 long valueAsLong = ((Long ) val).longValue(); 69 return new Long (-valueAsLong); 70 } else if (val instanceof Float ) { 71 float valueAsFloat = ((Float ) val).floatValue(); 72 return new Float (-valueAsFloat); 73 } else if (val instanceof Double ) { 74 double valueAsDouble = ((Double ) val).doubleValue(); 75 return new Double (-valueAsDouble); 76 } else if (val instanceof BigDecimal ) { 77 BigDecimal valueAsBigD = (BigDecimal ) val; 78 return valueAsBigD.negate(); 79 } else if (val instanceof BigInteger ) { 80 BigInteger valueAsBigI = (BigInteger ) val; 81 return valueAsBigI.negate(); 82 } 83 throw new Exception ("expression not a number"); 84 } 85 } 86 | Popular Tags |