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 | 0 | private static Log log = LogFactory.getLog(UnaryMinusOperator.class); |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public static final UnaryMinusOperator SINGLETON = |
49 | |
new UnaryMinusOperator (); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public UnaryMinusOperator () |
57 | 0 | { |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public String getOperatorSymbol () |
68 | |
{ |
69 | 0 | return "-"; |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public Object apply (Object pValue) |
78 | |
throws ELException |
79 | |
{ |
80 | 0 | if (pValue == null) { |
81 | |
|
82 | 0 | return PrimitiveObjects.getInteger (0); |
83 | |
} |
84 | |
|
85 | 0 | else if (pValue instanceof BigInteger) { |
86 | 0 | return ((BigInteger) pValue).negate(); |
87 | |
} |
88 | |
|
89 | 0 | else if (pValue instanceof BigDecimal) { |
90 | 0 | return ((BigDecimal) pValue).negate(); |
91 | |
} |
92 | |
|
93 | 0 | else if (pValue instanceof String) { |
94 | 0 | if (Coercions.isFloatingPointString (pValue)) { |
95 | 0 | double dval = |
96 | |
((Number) |
97 | |
(Coercions.coerceToPrimitiveNumber |
98 | |
(pValue, Double.class))). |
99 | |
doubleValue (); |
100 | 0 | return PrimitiveObjects.getDouble (-dval); |
101 | |
} |
102 | |
else { |
103 | 0 | long lval = |
104 | |
((Number) |
105 | |
(Coercions.coerceToPrimitiveNumber |
106 | |
(pValue, Long.class))). |
107 | |
longValue (); |
108 | 0 | return PrimitiveObjects.getLong (-lval); |
109 | |
} |
110 | |
} |
111 | |
|
112 | 0 | else if (pValue instanceof Byte) { |
113 | 0 | return PrimitiveObjects.getByte |
114 | |
((byte) -(((Byte) pValue).byteValue ())); |
115 | |
} |
116 | 0 | else if (pValue instanceof Short) { |
117 | 0 | return PrimitiveObjects.getShort |
118 | |
((short) -(((Short) pValue).shortValue ())); |
119 | |
} |
120 | 0 | else if (pValue instanceof Integer) { |
121 | 0 | return PrimitiveObjects.getInteger |
122 | |
((int) -(((Integer) pValue).intValue ())); |
123 | |
} |
124 | 0 | else if (pValue instanceof Long) { |
125 | 0 | return PrimitiveObjects.getLong |
126 | |
((long) -(((Long) pValue).longValue ())); |
127 | |
} |
128 | 0 | else if (pValue instanceof Float) { |
129 | 0 | return PrimitiveObjects.getFloat |
130 | |
((float) -(((Float) pValue).floatValue ())); |
131 | |
} |
132 | 0 | else if (pValue instanceof Double) { |
133 | 0 | return PrimitiveObjects.getDouble |
134 | |
((double) -(((Double) pValue).doubleValue ())); |
135 | |
} |
136 | |
|
137 | |
else { |
138 | 0 | if (log.isErrorEnabled()) { |
139 | 0 | String message = MessageUtil.getMessageWithArgs( |
140 | |
Constants.UNARY_OP_BAD_TYPE, |
141 | |
getOperatorSymbol(), |
142 | |
pValue.getClass().getName()); |
143 | 0 | log.error(message); |
144 | 0 | throw new ELException(message); |
145 | |
} |
146 | 0 | return PrimitiveObjects.getInteger (0); |
147 | |
} |
148 | |
} |
149 | |
|
150 | |
|
151 | |
} |