1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jexl3;
19
20 import java.math.BigInteger;
21 import java.math.MathContext;
22
23
24
25
26 public class Arithmetic360 extends JexlArithmetic {
27 public Arithmetic360(final boolean astrict) {
28 super(astrict);
29 }
30
31 public Arithmetic360(final boolean astrict, final MathContext bigdContext, final int bigdScale) {
32 super(astrict, bigdContext, bigdScale);
33 }
34
35
36
37
38
39
40
41
42 @Override
43 public Object and(final Object left, final Object right) {
44 final Number l = asLongNumber(left);
45 final Number r = asLongNumber(right);
46 if (l != null && r != null) {
47 return narrowLong(left, right, l.longValue() & r.longValue());
48 }
49 return toBigInteger(left).and(toBigInteger(right));
50 }
51
52
53
54
55
56
57
58 protected Number asIntNumber(final Object value) {
59 return value instanceof Integer
60 || value instanceof Short
61 || value instanceof Byte
62 ? (Number) value
63 : null;
64 }
65
66
67
68
69
70 protected Long castLongNumber(final Object value) {
71 return value instanceof Long ? (Long) value : null;
72 }
73
74
75
76
77
78
79
80 @Override
81 public Object complement(final Object val) {
82 final long l = toLong(val);
83 return narrowLong(val, ~l);
84 }
85
86
87
88
89
90
91
92
93 protected Number narrowLong(final Object operand, final long result) {
94 if (!(operand instanceof Long)) {
95 final int ir = (int) result;
96 if (result == ir) {
97 return ir;
98 }
99 }
100 return result;
101 }
102
103
104
105
106
107
108
109
110
111 @Override
112 protected Number narrowLong(final Object lhs, final Object rhs, final long result) {
113 if (!(lhs instanceof Long || rhs instanceof Long)) {
114 final int ir = (int) result;
115 if (result == ir) {
116 return ir;
117 }
118 }
119 return result;
120 }
121
122
123
124
125
126
127
128
129 @Override
130 public Object or(final Object left, final Object right) {
131 final Number l = asLongNumber(left);
132 final Number r = asLongNumber(right);
133 if (l != null && r != null) {
134 return narrowLong(left, right, l.longValue() | r.longValue());
135 }
136 return toBigInteger(left).or(toBigInteger(right));
137 }
138
139
140
141
142
143
144
145
146 @Override
147 public Object shiftLeft(final Object left, final Object right) {
148 if (left == null && right == null) {
149 return controlNullNullOperands(JexlOperator.SHIFTLEFT);
150 }
151 final int r = toInteger(right);
152 Number l = asIntNumber(left);
153 if (l != null) {
154 return l.intValue() << r;
155 }
156 l = castLongNumber(left);
157 if (l != null) {
158 return l.longValue() << r;
159 }
160 return toBigInteger(left).shiftLeft(r);
161 }
162
163
164
165
166
167
168
169
170 @Override
171 public Object shiftRight(final Object left, final Object right) {
172 if (left == null && right == null) {
173 return controlNullNullOperands(JexlOperator.SHIFTRIGHT);
174 }
175 final int r = toInteger(right);
176 Number l = asIntNumber(left);
177 if (l != null) {
178 return l.intValue() >> r;
179 }
180 l = castLongNumber(left);
181 if (l != null) {
182 return l.longValue() >> r;
183 }
184 return toBigInteger(left).shiftRight(r);
185 }
186
187
188
189
190
191
192
193
194 @Override
195 public Object shiftRightUnsigned(final Object left, final Object right) {
196 if (left == null && right == null) {
197 return controlNullNullOperands(JexlOperator.SHIFTRIGHTU);
198 }
199 final int r = toInteger(right);
200 Number l = asIntNumber(left);
201 if (l != null) {
202 return l.intValue() >>> r;
203 }
204 l = castLongNumber(left);
205 if (l != null) {
206 return l.longValue() >>> r;
207 }
208 final BigInteger bl = toBigInteger(left);
209 return bl.signum() < 0? bl.negate().shiftRight(r) : bl.shiftRight(r);
210 }
211
212
213
214
215
216
217
218
219 @Override
220 public Object xor(final Object left, final Object right) {
221 final Number l = asLongNumber(left);
222 final Number r = asLongNumber(right);
223 if (l != null && r != null) {
224 return narrowLong(left, right, l.longValue() ^ r.longValue());
225 }
226 return toBigInteger(left).xor(toBigInteger(right));
227 }
228
229 }