KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > el > lang > ELArithmetic


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.el.lang;
19
20 import java.math.BigDecimal JavaDoc;
21 import java.math.BigInteger JavaDoc;
22
23 import org.apache.el.util.MessageFactory;
24
25
26 /**
27  * A helper class of Arithmetic defined by the EL Specification
28  * @author Jacob Hookom [jacob@hookom.net]
29  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
30  */

31 public abstract class ELArithmetic {
32
33     public final static class BigDecimalDelegate extends ELArithmetic {
34
35         protected Number JavaDoc add(Number JavaDoc num0, Number JavaDoc num1) {
36             return ((BigDecimal JavaDoc) num0).add((BigDecimal JavaDoc) num1);
37         }
38
39         protected Number JavaDoc coerce(Number JavaDoc num) {
40             if (num instanceof BigDecimal JavaDoc)
41                 return num;
42             if (num instanceof BigInteger JavaDoc)
43                 return new BigDecimal JavaDoc((BigInteger JavaDoc) num);
44             return new BigDecimal JavaDoc(num.doubleValue());
45         }
46
47         protected Number JavaDoc coerce(String JavaDoc str) {
48             return new BigDecimal JavaDoc(str);
49         }
50
51         protected Number JavaDoc divide(Number JavaDoc num0, Number JavaDoc num1) {
52             return ((BigDecimal JavaDoc) num0).divide((BigDecimal JavaDoc) num1,
53                     BigDecimal.ROUND_HALF_UP);
54         }
55
56         protected Number JavaDoc subtract(Number JavaDoc num0, Number JavaDoc num1) {
57             return ((BigDecimal JavaDoc) num0).subtract((BigDecimal JavaDoc) num1);
58         }
59
60         protected Number JavaDoc mod(Number JavaDoc num0, Number JavaDoc num1) {
61             return new Double JavaDoc(num0.doubleValue() % num1.doubleValue());
62         }
63
64         protected Number JavaDoc multiply(Number JavaDoc num0, Number JavaDoc num1) {
65             return ((BigDecimal JavaDoc) num0).multiply((BigDecimal JavaDoc) num1);
66         }
67
68         public boolean matches(Object JavaDoc obj0, Object JavaDoc obj1) {
69             return (obj0 instanceof BigDecimal JavaDoc || obj1 instanceof BigDecimal JavaDoc);
70         }
71     }
72
73     public final static class BigIntegerDelegate extends ELArithmetic {
74
75         protected Number JavaDoc add(Number JavaDoc num0, Number JavaDoc num1) {
76             return ((BigInteger JavaDoc) num0).add((BigInteger JavaDoc) num1);
77         }
78
79         protected Number JavaDoc coerce(Number JavaDoc num) {
80             if (num instanceof BigInteger JavaDoc)
81                 return num;
82             return new BigInteger JavaDoc(num.toString());
83         }
84
85         protected Number JavaDoc coerce(String JavaDoc str) {
86             return new BigInteger JavaDoc(str);
87         }
88
89         protected Number JavaDoc divide(Number JavaDoc num0, Number JavaDoc num1) {
90             return (new BigDecimal JavaDoc((BigInteger JavaDoc) num0)).divide(new BigDecimal JavaDoc((BigInteger JavaDoc) num1), BigDecimal.ROUND_HALF_UP);
91         }
92
93         protected Number JavaDoc multiply(Number JavaDoc num0, Number JavaDoc num1) {
94             return ((BigInteger JavaDoc) num0).multiply((BigInteger JavaDoc) num1);
95         }
96
97         protected Number JavaDoc mod(Number JavaDoc num0, Number JavaDoc num1) {
98             return ((BigInteger JavaDoc) num0).mod((BigInteger JavaDoc) num1);
99         }
100
101         protected Number JavaDoc subtract(Number JavaDoc num0, Number JavaDoc num1) {
102             return ((BigInteger JavaDoc) num0).subtract((BigInteger JavaDoc) num1);
103         }
104
105         public boolean matches(Object JavaDoc obj0, Object JavaDoc obj1) {
106             return (obj0 instanceof BigInteger JavaDoc || obj1 instanceof BigInteger JavaDoc);
107         }
108     }
109
110     public final static class DoubleDelegate extends ELArithmetic {
111
112         protected Number JavaDoc add(Number JavaDoc num0, Number JavaDoc num1) {
113             // could only be one of these
114
if (num0 instanceof BigDecimal JavaDoc) {
115                 return ((BigDecimal JavaDoc) num0).add(new BigDecimal JavaDoc(num1.doubleValue()));
116             } else if (num1 instanceof BigDecimal JavaDoc) {
117                 return ((new BigDecimal JavaDoc(num0.doubleValue()).add((BigDecimal JavaDoc) num1)));
118             }
119             return new Double JavaDoc(num0.doubleValue() + num1.doubleValue());
120         }
121
122         protected Number JavaDoc coerce(Number JavaDoc num) {
123             if (num instanceof Double JavaDoc)
124                 return num;
125             if (num instanceof BigInteger JavaDoc)
126                 return new BigDecimal JavaDoc((BigInteger JavaDoc) num);
127             return new Double JavaDoc(num.doubleValue());
128         }
129
130         protected Number JavaDoc coerce(String JavaDoc str) {
131             return new Double JavaDoc(str);
132         }
133
134         protected Number JavaDoc divide(Number JavaDoc num0, Number JavaDoc num1) {
135             return new Double JavaDoc(num0.doubleValue() / num1.doubleValue());
136         }
137
138         protected Number JavaDoc mod(Number JavaDoc num0, Number JavaDoc num1) {
139             return new Double JavaDoc(num0.doubleValue() % num1.doubleValue());
140         }
141
142         protected Number JavaDoc subtract(Number JavaDoc num0, Number JavaDoc num1) {
143             // could only be one of these
144
if (num0 instanceof BigDecimal JavaDoc) {
145                 return ((BigDecimal JavaDoc) num0).subtract(new BigDecimal JavaDoc(num1.doubleValue()));
146             } else if (num1 instanceof BigDecimal JavaDoc) {
147                 return ((new BigDecimal JavaDoc(num0.doubleValue()).subtract((BigDecimal JavaDoc) num1)));
148             }
149             return new Double JavaDoc(num0.doubleValue() - num1.doubleValue());
150         }
151
152         protected Number JavaDoc multiply(Number JavaDoc num0, Number JavaDoc num1) {
153             // could only be one of these
154
if (num0 instanceof BigDecimal JavaDoc) {
155                 return ((BigDecimal JavaDoc) num0).multiply(new BigDecimal JavaDoc(num1.doubleValue()));
156             } else if (num1 instanceof BigDecimal JavaDoc) {
157                 return ((new BigDecimal JavaDoc(num0.doubleValue()).multiply((BigDecimal JavaDoc) num1)));
158             }
159             return new Double JavaDoc(num0.doubleValue() * num1.doubleValue());
160         }
161
162         public boolean matches(Object JavaDoc obj0, Object JavaDoc obj1) {
163             return (obj0 instanceof Double JavaDoc
164                     || obj1 instanceof Double JavaDoc
165                     || obj0 instanceof Float JavaDoc
166                     || obj1 instanceof Float JavaDoc
167                     || (obj0 != null && (Double.TYPE == obj0.getClass() || Float.TYPE == obj0.getClass()))
168                     || (obj1 != null && (Double.TYPE == obj1.getClass() || Float.TYPE == obj1.getClass()))
169                     || (obj0 instanceof String JavaDoc && ELSupport
170                             .isStringFloat((String JavaDoc) obj0)) || (obj1 instanceof String JavaDoc && ELSupport
171                     .isStringFloat((String JavaDoc) obj1)));
172         }
173     }
174
175     public final static class LongDelegate extends ELArithmetic {
176
177         protected Number JavaDoc add(Number JavaDoc num0, Number JavaDoc num1) {
178             return new Long JavaDoc(num0.longValue() + num1.longValue());
179         }
180
181         protected Number JavaDoc coerce(Number JavaDoc num) {
182             if (num instanceof Long JavaDoc)
183                 return num;
184             return new Long JavaDoc(num.longValue());
185         }
186
187         protected Number JavaDoc coerce(String JavaDoc str) {
188             return new Long JavaDoc(str);
189         }
190
191         protected Number JavaDoc divide(Number JavaDoc num0, Number JavaDoc num1) {
192             return new Long JavaDoc(num0.longValue() / num1.longValue());
193         }
194
195         protected Number JavaDoc mod(Number JavaDoc num0, Number JavaDoc num1) {
196             return new Long JavaDoc(num0.longValue() % num1.longValue());
197         }
198
199         protected Number JavaDoc subtract(Number JavaDoc num0, Number JavaDoc num1) {
200             return new Long JavaDoc(num0.longValue() - num1.longValue());
201         }
202
203         protected Number JavaDoc multiply(Number JavaDoc num0, Number JavaDoc num1) {
204             return new Long JavaDoc(num0.longValue() * num1.longValue());
205         }
206
207         public boolean matches(Object JavaDoc obj0, Object JavaDoc obj1) {
208             return (obj0 instanceof Long JavaDoc || obj1 instanceof Long JavaDoc);
209         }
210     }
211
212     public final static BigDecimalDelegate BIGDECIMAL = new BigDecimalDelegate();
213
214     public final static BigIntegerDelegate BIGINTEGER = new BigIntegerDelegate();
215
216     public final static DoubleDelegate DOUBLE = new DoubleDelegate();
217
218     public final static LongDelegate LONG = new LongDelegate();
219
220     private final static Long JavaDoc ZERO = new Long JavaDoc(0);
221
222     public final static Number JavaDoc add(final Object JavaDoc obj0, final Object JavaDoc obj1) {
223         if (obj0 == null && obj1 == null) {
224             return new Long JavaDoc(0);
225         }
226
227         final ELArithmetic delegate;
228         if (BIGDECIMAL.matches(obj0, obj1))
229             delegate = BIGDECIMAL;
230         else if (DOUBLE.matches(obj0, obj1))
231             delegate = DOUBLE;
232         else if (BIGINTEGER.matches(obj0, obj1))
233             delegate = BIGINTEGER;
234         else
235             delegate = LONG;
236
237         Number JavaDoc num0 = delegate.coerce(obj0);
238         Number JavaDoc num1 = delegate.coerce(obj1);
239
240         return delegate.add(num0, num1);
241     }
242
243     public final static Number JavaDoc mod(final Object JavaDoc obj0, final Object JavaDoc obj1) {
244         if (obj0 == null && obj1 == null) {
245             return new Long JavaDoc(0);
246         }
247
248         final ELArithmetic delegate;
249         if (BIGDECIMAL.matches(obj0, obj1))
250             delegate = BIGDECIMAL;
251         else if (DOUBLE.matches(obj0, obj1))
252             delegate = DOUBLE;
253         else if (BIGINTEGER.matches(obj0, obj1))
254             delegate = BIGINTEGER;
255         else
256             delegate = LONG;
257
258         Number JavaDoc num0 = delegate.coerce(obj0);
259         Number JavaDoc num1 = delegate.coerce(obj1);
260
261         return delegate.mod(num0, num1);
262     }
263
264     public final static Number JavaDoc subtract(final Object JavaDoc obj0, final Object JavaDoc obj1) {
265         if (obj0 == null && obj1 == null) {
266             return new Long JavaDoc(0);
267         }
268
269         final ELArithmetic delegate;
270         if (BIGDECIMAL.matches(obj0, obj1))
271             delegate = BIGDECIMAL;
272         else if (DOUBLE.matches(obj0, obj1))
273             delegate = DOUBLE;
274         else if (BIGINTEGER.matches(obj0, obj1))
275             delegate = BIGINTEGER;
276         else
277             delegate = LONG;
278
279         Number JavaDoc num0 = delegate.coerce(obj0);
280         Number JavaDoc num1 = delegate.coerce(obj1);
281
282         return delegate.subtract(num0, num1);
283     }
284
285     public final static Number JavaDoc divide(final Object JavaDoc obj0, final Object JavaDoc obj1) {
286         if (obj0 == null && obj1 == null) {
287             return ZERO;
288         }
289
290         final ELArithmetic delegate;
291         if (BIGDECIMAL.matches(obj0, obj1))
292             delegate = BIGDECIMAL;
293         else if (BIGINTEGER.matches(obj0, obj1))
294             delegate = BIGDECIMAL;
295         else
296             delegate = DOUBLE;
297
298         Number JavaDoc num0 = delegate.coerce(obj0);
299         Number JavaDoc num1 = delegate.coerce(obj1);
300
301         return delegate.divide(num0, num1);
302     }
303
304     public final static Number JavaDoc multiply(final Object JavaDoc obj0, final Object JavaDoc obj1) {
305         if (obj0 == null && obj1 == null) {
306             return new Long JavaDoc(0);
307         }
308
309         final ELArithmetic delegate;
310         if (BIGDECIMAL.matches(obj0, obj1))
311             delegate = BIGDECIMAL;
312         else if (DOUBLE.matches(obj0, obj1))
313             delegate = DOUBLE;
314         else if (BIGINTEGER.matches(obj0, obj1))
315             delegate = BIGINTEGER;
316         else
317             delegate = LONG;
318
319         Number JavaDoc num0 = delegate.coerce(obj0);
320         Number JavaDoc num1 = delegate.coerce(obj1);
321
322         return delegate.multiply(num0, num1);
323     }
324
325     public final static boolean isNumber(final Object JavaDoc obj) {
326         return (obj != null && isNumberType(obj.getClass()));
327     }
328
329     public final static boolean isNumberType(final Class JavaDoc type) {
330         return type == (java.lang.Long JavaDoc.class) || type == Long.TYPE || type == (java.lang.Double JavaDoc.class) || type == Double.TYPE || type == (java.lang.Byte JavaDoc.class) || type == Byte.TYPE || type == (java.lang.Short JavaDoc.class) || type == Short.TYPE || type == (java.lang.Integer JavaDoc.class) || type == Integer.TYPE || type == (java.lang.Float JavaDoc.class) || type == Float.TYPE || type == (java.math.BigInteger JavaDoc.class) || type == (java.math.BigDecimal JavaDoc.class);
331     }
332
333     /**
334      *
335      */

336     protected ELArithmetic() {
337         super();
338     }
339
340     protected abstract Number JavaDoc add(final Number JavaDoc num0, final Number JavaDoc num1);
341
342     protected abstract Number JavaDoc multiply(final Number JavaDoc num0, final Number JavaDoc num1);
343
344     protected abstract Number JavaDoc subtract(final Number JavaDoc num0, final Number JavaDoc num1);
345
346     protected abstract Number JavaDoc mod(final Number JavaDoc num0, final Number JavaDoc num1);
347
348     protected abstract Number JavaDoc coerce(final Number JavaDoc num);
349
350     protected final Number JavaDoc coerce(final Object JavaDoc obj) {
351         
352         if (isNumber(obj)) {
353             return coerce((Number JavaDoc) obj);
354         }
355         if (obj instanceof String JavaDoc) {
356             return coerce((String JavaDoc) obj);
357         }
358         if (obj == null || "".equals(obj)) {
359             return coerce(ZERO);
360         }
361
362         Class JavaDoc objType = obj.getClass();
363         if (Character JavaDoc.class.equals(objType) || Character.TYPE == objType) {
364             return coerce(new Short JavaDoc((short) ((Character JavaDoc) obj).charValue()));
365         }
366
367         throw new IllegalArgumentException JavaDoc(MessageFactory.get("el.convert", obj,
368                 objType));
369     }
370
371     protected abstract Number JavaDoc coerce(final String JavaDoc str);
372
373     protected abstract Number JavaDoc divide(final Number JavaDoc num0, final Number JavaDoc num1);
374
375     protected abstract boolean matches(final Object JavaDoc obj0, final Object JavaDoc obj1);
376
377 }
378
Popular Tags