1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.el;
18
19 import java.math.BigDecimal;
20 import java.math.BigInteger;
21
22 import javax.servlet.jsp.el.ELException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27
28
29
30
31
32
33
34
35 public class UnaryMinusOperator
36 extends UnaryOperator
37 {
38
39
40
41
42 private static Log log = LogFactory.getLog(UnaryMinusOperator.class);
43
44
45
46
47
48 public static final UnaryMinusOperator SINGLETON =
49 new UnaryMinusOperator ();
50
51
52
53
54
55
56 public UnaryMinusOperator ()
57 {
58 }
59
60
61
62
63
64
65
66
67 public String getOperatorSymbol ()
68 {
69 return "-";
70 }
71
72
73
74
75
76
77 public Object apply (Object pValue)
78 throws ELException
79 {
80 if (pValue == null) {
81
82 return PrimitiveObjects.getInteger (0);
83 }
84
85 else if (pValue instanceof BigInteger) {
86 return ((BigInteger) pValue).negate();
87 }
88
89 else if (pValue instanceof BigDecimal) {
90 return ((BigDecimal) pValue).negate();
91 }
92
93 else if (pValue instanceof String) {
94 if (Coercions.isFloatingPointString (pValue)) {
95 double dval =
96 ((Number)
97 (Coercions.coerceToPrimitiveNumber
98 (pValue, Double.class))).
99 doubleValue ();
100 return PrimitiveObjects.getDouble (-dval);
101 }
102 else {
103 long lval =
104 ((Number)
105 (Coercions.coerceToPrimitiveNumber
106 (pValue, Long.class))).
107 longValue ();
108 return PrimitiveObjects.getLong (-lval);
109 }
110 }
111
112 else if (pValue instanceof Byte) {
113 return PrimitiveObjects.getByte
114 ((byte) -(((Byte) pValue).byteValue ()));
115 }
116 else if (pValue instanceof Short) {
117 return PrimitiveObjects.getShort
118 ((short) -(((Short) pValue).shortValue ()));
119 }
120 else if (pValue instanceof Integer) {
121 return PrimitiveObjects.getInteger
122 ((int) -(((Integer) pValue).intValue ()));
123 }
124 else if (pValue instanceof Long) {
125 return PrimitiveObjects.getLong
126 ((long) -(((Long) pValue).longValue ()));
127 }
128 else if (pValue instanceof Float) {
129 return PrimitiveObjects.getFloat
130 ((float) -(((Float) pValue).floatValue ()));
131 }
132 else if (pValue instanceof Double) {
133 return PrimitiveObjects.getDouble
134 ((double) -(((Double) pValue).doubleValue ()));
135 }
136
137 else {
138 if (log.isErrorEnabled()) {
139 String message = MessageUtil.getMessageWithArgs(
140 Constants.UNARY_OP_BAD_TYPE,
141 getOperatorSymbol(),
142 pValue.getClass().getName());
143 log.error(message);
144 throw new ELException(message);
145 }
146 return PrimitiveObjects.getInteger (0);
147 }
148 }
149
150
151 }