1 55 56 package org.apache.commons.el; 57 58 import javax.servlet.jsp.el.ELException ; 59 import java.math.BigInteger ; 60 import java.math.BigDecimal ; 61 62 69 70 public class UnaryMinusOperator 71 extends UnaryOperator 72 { 73 77 public static final UnaryMinusOperator SINGLETON = 78 new UnaryMinusOperator (); 79 80 85 public UnaryMinusOperator () 86 { 87 } 88 89 96 public String getOperatorSymbol () 97 { 98 return "-"; 99 } 100 101 106 public Object apply (Object pValue, 107 Logger pLogger) 108 throws ELException 109 { 110 if (pValue == null) { 111 118 return PrimitiveObjects.getInteger (0); 119 } 120 121 else if (pValue instanceof BigInteger ) { 122 return ((BigInteger ) pValue).negate(); 123 } 124 125 else if (pValue instanceof BigDecimal ) { 126 return ((BigDecimal ) pValue).negate(); 127 } 128 129 else if (pValue instanceof String ) { 130 if (Coercions.isFloatingPointString (pValue)) { 131 double dval = 132 ((Number ) 133 (Coercions.coerceToPrimitiveNumber 134 (pValue, Double .class, pLogger))). 135 doubleValue (); 136 return PrimitiveObjects.getDouble (-dval); 137 } 138 else { 139 long lval = 140 ((Number ) 141 (Coercions.coerceToPrimitiveNumber 142 (pValue, Long .class, pLogger))). 143 longValue (); 144 return PrimitiveObjects.getLong (-lval); 145 } 146 } 147 148 else if (pValue instanceof Byte ) { 149 return PrimitiveObjects.getByte 150 ((byte) -(((Byte ) pValue).byteValue ())); 151 } 152 else if (pValue instanceof Short ) { 153 return PrimitiveObjects.getShort 154 ((short) -(((Short ) pValue).shortValue ())); 155 } 156 else if (pValue instanceof Integer ) { 157 return PrimitiveObjects.getInteger 158 ((int) -(((Integer ) pValue).intValue ())); 159 } 160 else if (pValue instanceof Long ) { 161 return PrimitiveObjects.getLong 162 ((long) -(((Long ) pValue).longValue ())); 163 } 164 else if (pValue instanceof Float ) { 165 return PrimitiveObjects.getFloat 166 ((float) -(((Float ) pValue).floatValue ())); 167 } 168 else if (pValue instanceof Double ) { 169 return PrimitiveObjects.getDouble 170 ((double) -(((Double ) pValue).doubleValue ())); 171 } 172 173 else { 174 if (pLogger.isLoggingError ()) { 175 pLogger.logError 176 (Constants.UNARY_OP_BAD_TYPE, 177 getOperatorSymbol (), 178 pValue.getClass ().getName ()); 179 } 180 return PrimitiveObjects.getInteger (0); 181 } 182 } 183 184 } 186 | Popular Tags |