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.BigInteger; |
20 | |
|
21 | |
import javax.servlet.jsp.el.ELException; |
22 | |
|
23 | |
import org.apache.commons.logging.Log; |
24 | |
import org.apache.commons.logging.LogFactory; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class ModulusOperator |
35 | |
extends BinaryOperator |
36 | |
{ |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | private static Log log = LogFactory.getLog(ModulusOperator.class); |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public static final ModulusOperator SINGLETON = |
48 | |
new ModulusOperator (); |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public ModulusOperator () |
56 | 0 | { |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public String getOperatorSymbol () |
67 | |
{ |
68 | 0 | return "%"; |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public Object apply (Object pLeft, Object pRight) |
77 | |
throws ELException |
78 | |
{ |
79 | 0 | if (pLeft == null && |
80 | |
pRight == null) { |
81 | 0 | if (log.isWarnEnabled()) { |
82 | 0 | log.warn( |
83 | |
MessageUtil.getMessageWithArgs( |
84 | |
Constants.ARITH_OP_NULL, getOperatorSymbol())); |
85 | |
} |
86 | 0 | return PrimitiveObjects.getInteger (0); |
87 | |
} |
88 | |
|
89 | 0 | if ((pLeft != null && |
90 | |
(Coercions.isFloatingPointType (pLeft) || |
91 | |
Coercions.isFloatingPointString (pLeft))) || |
92 | |
Coercions.isBigDecimal(pLeft) || |
93 | |
(pRight != null && |
94 | |
(Coercions.isFloatingPointType (pRight) || |
95 | |
Coercions.isFloatingPointString (pRight) || |
96 | |
Coercions.isBigDecimal(pRight)))) { |
97 | 0 | double left = |
98 | |
Coercions.coerceToPrimitiveNumber (pLeft, Double.class). |
99 | |
doubleValue (); |
100 | 0 | double right = |
101 | |
Coercions.coerceToPrimitiveNumber (pRight, Double.class). |
102 | |
doubleValue (); |
103 | |
|
104 | |
try { |
105 | 0 | return PrimitiveObjects.getDouble (left % right); |
106 | |
} |
107 | 0 | catch (Exception exc) { |
108 | 0 | if (log.isErrorEnabled()) { |
109 | 0 | String message = MessageUtil.getMessageWithArgs( |
110 | |
Constants.ARITH_ERROR, |
111 | |
getOperatorSymbol(), |
112 | |
"" + left, |
113 | |
"" + right); |
114 | 0 | log.error(message); |
115 | 0 | throw new ELException(message); |
116 | |
} |
117 | 0 | return PrimitiveObjects.getInteger (0); |
118 | |
} |
119 | |
} |
120 | 0 | else if (Coercions.isBigInteger(pLeft) || Coercions.isBigInteger(pRight)) { |
121 | 0 | BigInteger left = (BigInteger) |
122 | |
Coercions.coerceToPrimitiveNumber(pLeft, BigInteger.class); |
123 | 0 | BigInteger right = (BigInteger) |
124 | |
Coercions.coerceToPrimitiveNumber(pRight, BigInteger.class); |
125 | |
|
126 | |
try { |
127 | 0 | return left.remainder(right); |
128 | 0 | } catch (Exception exc) { |
129 | 0 | if (log.isErrorEnabled()) { |
130 | 0 | String message = MessageUtil.getMessageWithArgs( |
131 | |
Constants.ARITH_ERROR, |
132 | |
getOperatorSymbol(), |
133 | |
"" + left, |
134 | |
"" + right); |
135 | 0 | log.error(message); |
136 | 0 | throw new ELException(message); |
137 | |
} |
138 | 0 | return PrimitiveObjects.getInteger(0); |
139 | |
} |
140 | |
} |
141 | |
else { |
142 | 0 | long left = |
143 | |
Coercions.coerceToPrimitiveNumber (pLeft, Long.class). |
144 | |
longValue (); |
145 | 0 | long right = |
146 | |
Coercions.coerceToPrimitiveNumber (pRight, Long.class). |
147 | |
longValue (); |
148 | |
|
149 | |
try { |
150 | 0 | return PrimitiveObjects.getLong (left % right); |
151 | |
} |
152 | 0 | catch (Exception exc) { |
153 | 0 | if (log.isErrorEnabled()) { |
154 | 0 | String message = MessageUtil.getMessageWithArgs( |
155 | |
Constants.ARITH_ERROR, |
156 | |
getOperatorSymbol(), |
157 | |
"" + left, |
158 | |
"" + right); |
159 | 0 | log.error(message); |
160 | 0 | throw new ELException(message); |
161 | |
} |
162 | 0 | return PrimitiveObjects.getInteger (0); |
163 | |
} |
164 | |
} |
165 | |
} |
166 | |
|
167 | |
|
168 | |
} |