~ubuntu-branches/debian/sid/tomcat6/sid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.el.lang;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.apache.el.util.MessageFactory;


/**
 * A helper class of Arithmetic defined by the EL Specification
 * @author Jacob Hookom [jacob@hookom.net]
 *
 */
public abstract class ELArithmetic {

    public final static class BigDecimalDelegate extends ELArithmetic {

        protected Number add(Number num0, Number num1) {
            return ((BigDecimal) num0).add((BigDecimal) num1);
        }

        protected Number coerce(Number num) {
            if (num instanceof BigDecimal)
                return num;
            if (num instanceof BigInteger)
                return new BigDecimal((BigInteger) num);
            return new BigDecimal(num.doubleValue());
        }

        protected Number coerce(String str) {
            return new BigDecimal(str);
        }

        protected Number divide(Number num0, Number num1) {
            return ((BigDecimal) num0).divide((BigDecimal) num1,
                    BigDecimal.ROUND_HALF_UP);
        }

        protected Number subtract(Number num0, Number num1) {
            return ((BigDecimal) num0).subtract((BigDecimal) num1);
        }

        protected Number mod(Number num0, Number num1) {
            return new Double(num0.doubleValue() % num1.doubleValue());
        }

        protected Number multiply(Number num0, Number num1) {
            return ((BigDecimal) num0).multiply((BigDecimal) num1);
        }

        public boolean matches(Object obj0, Object obj1) {
            return (obj0 instanceof BigDecimal || obj1 instanceof BigDecimal);
        }
    }

    public final static class BigIntegerDelegate extends ELArithmetic {

        protected Number add(Number num0, Number num1) {
            return ((BigInteger) num0).add((BigInteger) num1);
        }

        protected Number coerce(Number num) {
            if (num instanceof BigInteger)
                return num;
            return new BigInteger(num.toString());
        }

        protected Number coerce(String str) {
            return new BigInteger(str);
        }

        protected Number divide(Number num0, Number num1) {
            return (new BigDecimal((BigInteger) num0)).divide(new BigDecimal((BigInteger) num1), BigDecimal.ROUND_HALF_UP);
        }

        protected Number multiply(Number num0, Number num1) {
            return ((BigInteger) num0).multiply((BigInteger) num1);
        }

        protected Number mod(Number num0, Number num1) {
            return ((BigInteger) num0).mod((BigInteger) num1);
        }

        protected Number subtract(Number num0, Number num1) {
            return ((BigInteger) num0).subtract((BigInteger) num1);
        }

        public boolean matches(Object obj0, Object obj1) {
            return (obj0 instanceof BigInteger || obj1 instanceof BigInteger);
        }
    }

    public final static class DoubleDelegate extends ELArithmetic {

        protected Number add(Number num0, Number num1) {
            // could only be one of these
            if (num0 instanceof BigDecimal) {
                return ((BigDecimal) num0).add(new BigDecimal(num1.doubleValue()));
            } else if (num1 instanceof BigDecimal) {
                return ((new BigDecimal(num0.doubleValue()).add((BigDecimal) num1)));
            }
            return new Double(num0.doubleValue() + num1.doubleValue());
        }

        protected Number coerce(Number num) {
            if (num instanceof Double)
                return num;
            if (num instanceof BigInteger)
                return new BigDecimal((BigInteger) num);
            return new Double(num.doubleValue());
        }

        protected Number coerce(String str) {
            return new Double(str);
        }

        protected Number divide(Number num0, Number num1) {
            return new Double(num0.doubleValue() / num1.doubleValue());
        }

        protected Number mod(Number num0, Number num1) {
            return new Double(num0.doubleValue() % num1.doubleValue());
        }

        protected Number subtract(Number num0, Number num1) {
            // could only be one of these
            if (num0 instanceof BigDecimal) {
                return ((BigDecimal) num0).subtract(new BigDecimal(num1.doubleValue()));
            } else if (num1 instanceof BigDecimal) {
                return ((new BigDecimal(num0.doubleValue()).subtract((BigDecimal) num1)));
            }
            return new Double(num0.doubleValue() - num1.doubleValue());
        }

        protected Number multiply(Number num0, Number num1) {
            // could only be one of these
            if (num0 instanceof BigDecimal) {
                return ((BigDecimal) num0).multiply(new BigDecimal(num1.doubleValue()));
            } else if (num1 instanceof BigDecimal) {
                return ((new BigDecimal(num0.doubleValue()).multiply((BigDecimal) num1)));
            }
            return new Double(num0.doubleValue() * num1.doubleValue());
        }

        public boolean matches(Object obj0, Object obj1) {
            return (obj0 instanceof Double
                    || obj1 instanceof Double
                    || obj0 instanceof Float
                    || obj1 instanceof Float
                    || (obj0 instanceof String && ELSupport
                            .isStringFloat((String) obj0)) || (obj1 instanceof String && ELSupport
                    .isStringFloat((String) obj1)));
        }
    }

    public final static class LongDelegate extends ELArithmetic {

        protected Number add(Number num0, Number num1) {
            return new Long(num0.longValue() + num1.longValue());
        }

        protected Number coerce(Number num) {
            if (num instanceof Long)
                return num;
            return new Long(num.longValue());
        }

        protected Number coerce(String str) {
            return new Long(str);
        }

        protected Number divide(Number num0, Number num1) {
            return new Long(num0.longValue() / num1.longValue());
        }

        protected Number mod(Number num0, Number num1) {
            return new Long(num0.longValue() % num1.longValue());
        }

        protected Number subtract(Number num0, Number num1) {
            return new Long(num0.longValue() - num1.longValue());
        }

        protected Number multiply(Number num0, Number num1) {
            return new Long(num0.longValue() * num1.longValue());
        }

        public boolean matches(Object obj0, Object obj1) {
            return (obj0 instanceof Long || obj1 instanceof Long);
        }
    }

    public final static BigDecimalDelegate BIGDECIMAL = new BigDecimalDelegate();

    public final static BigIntegerDelegate BIGINTEGER = new BigIntegerDelegate();

    public final static DoubleDelegate DOUBLE = new DoubleDelegate();

    public final static LongDelegate LONG = new LongDelegate();

    private final static Long ZERO = new Long(0);

    public final static Number add(final Object obj0, final Object obj1) {
        if (obj0 == null && obj1 == null) {
            return new Long(0);
        }

        final ELArithmetic delegate;
        if (BIGDECIMAL.matches(obj0, obj1))
            delegate = BIGDECIMAL;
        else if (DOUBLE.matches(obj0, obj1)) {
            if (BIGINTEGER.matches(obj0, obj1))
                delegate = BIGDECIMAL;
            else
                delegate = DOUBLE;
        } else if (BIGINTEGER.matches(obj0, obj1))
            delegate = BIGINTEGER;
        else
            delegate = LONG;

        Number num0 = delegate.coerce(obj0);
        Number num1 = delegate.coerce(obj1);

        return delegate.add(num0, num1);
    }

    public final static Number mod(final Object obj0, final Object obj1) {
        if (obj0 == null && obj1 == null) {
            return new Long(0);
        }

        final ELArithmetic delegate;
        if (BIGDECIMAL.matches(obj0, obj1))
            delegate = DOUBLE;
        else if (DOUBLE.matches(obj0, obj1))
            delegate = DOUBLE;
        else if (BIGINTEGER.matches(obj0, obj1))
            delegate = BIGINTEGER;
        else
            delegate = LONG;

        Number num0 = delegate.coerce(obj0);
        Number num1 = delegate.coerce(obj1);

        return delegate.mod(num0, num1);
    }

    public final static Number subtract(final Object obj0, final Object obj1) {
        if (obj0 == null && obj1 == null) {
            return new Long(0);
        }

        final ELArithmetic delegate;
        if (BIGDECIMAL.matches(obj0, obj1))
            delegate = BIGDECIMAL;
        else if (DOUBLE.matches(obj0, obj1)) {
            if (BIGINTEGER.matches(obj0, obj1))
                delegate = BIGDECIMAL;
            else
                delegate = DOUBLE;
        } else if (BIGINTEGER.matches(obj0, obj1))
            delegate = BIGINTEGER;
        else
            delegate = LONG;

        Number num0 = delegate.coerce(obj0);
        Number num1 = delegate.coerce(obj1);

        return delegate.subtract(num0, num1);
    }

    public final static Number divide(final Object obj0, final Object obj1) {
        if (obj0 == null && obj1 == null) {
            return ZERO;
        }

        final ELArithmetic delegate;
        if (BIGDECIMAL.matches(obj0, obj1))
            delegate = BIGDECIMAL;
        else if (BIGINTEGER.matches(obj0, obj1))
            delegate = BIGDECIMAL;
        else
            delegate = DOUBLE;

        Number num0 = delegate.coerce(obj0);
        Number num1 = delegate.coerce(obj1);

        return delegate.divide(num0, num1);
    }

    public final static Number multiply(final Object obj0, final Object obj1) {
        if (obj0 == null && obj1 == null) {
            return new Long(0);
        }

        final ELArithmetic delegate;
        if (BIGDECIMAL.matches(obj0, obj1))
            delegate = BIGDECIMAL;
        else if (DOUBLE.matches(obj0, obj1)) {
            if (BIGINTEGER.matches(obj0, obj1))
                delegate = BIGDECIMAL;
            else
                delegate = DOUBLE;
        } else if (BIGINTEGER.matches(obj0, obj1))
            delegate = BIGINTEGER;
        else
            delegate = LONG;

        Number num0 = delegate.coerce(obj0);
        Number num1 = delegate.coerce(obj1);

        return delegate.multiply(num0, num1);
    }

    public final static boolean isNumber(final Object obj) {
        return (obj != null && isNumberType(obj.getClass()));
    }

    public final static boolean isNumberType(final Class<?> type) {
        return type == Long.TYPE || type == Double.TYPE ||
            type == Byte.TYPE || type == Short.TYPE ||
            type == Integer.TYPE || type == Float.TYPE ||
            Number.class.isAssignableFrom(type);
    }

    /**
     *
     */
    protected ELArithmetic() {
        super();
    }

    protected abstract Number add(final Number num0, final Number num1);

    protected abstract Number multiply(final Number num0, final Number num1);

    protected abstract Number subtract(final Number num0, final Number num1);

    protected abstract Number mod(final Number num0, final Number num1);

    protected abstract Number coerce(final Number num);

    protected final Number coerce(final Object obj) {

        if (isNumber(obj)) {
            return coerce((Number) obj);
        }
        if (obj == null || "".equals(obj)) {
            return coerce(ZERO);
        }
        if (obj instanceof String) {
            return coerce((String) obj);
        }
        if (obj instanceof Character) {
            return coerce(new Short((short) ((Character) obj).charValue()));
        }

        throw new IllegalArgumentException(MessageFactory.get("error.convert",
                obj, obj.getClass(), "Number"));
    }

    protected abstract Number coerce(final String str);

    protected abstract Number divide(final Number num0, final Number num1);

    protected abstract boolean matches(final Object obj0, final Object obj1);

}