Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DivideOperator |
|
| 6.666666666666667;6.667 |
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 | package org.apache.commons.el; | |
18 | ||
19 | import java.math.BigDecimal; | |
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 | * <p>The implementation of the divide operator | |
29 | * | |
30 | * @author Nathan Abramson - Art Technology Group | |
31 | * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $ | |
32 | **/ | |
33 | ||
34 | public class DivideOperator | |
35 | extends BinaryOperator | |
36 | { | |
37 | //------------------------------------- | |
38 | // Constants | |
39 | //------------------------------------- | |
40 | 0 | private static Log log = LogFactory.getLog(DivideOperator.class); |
41 | ||
42 | //------------------------------------- | |
43 | // Singleton | |
44 | //------------------------------------- | |
45 | ||
46 | 0 | public static final DivideOperator SINGLETON = |
47 | new DivideOperator (); | |
48 | ||
49 | //------------------------------------- | |
50 | /** | |
51 | * | |
52 | * Constructor | |
53 | **/ | |
54 | public DivideOperator () | |
55 | 0 | { |
56 | 0 | } |
57 | ||
58 | //------------------------------------- | |
59 | // Expression methods | |
60 | //------------------------------------- | |
61 | /** | |
62 | * | |
63 | * Returns the symbol representing the operator | |
64 | **/ | |
65 | public String getOperatorSymbol () | |
66 | { | |
67 | 0 | return "/"; |
68 | } | |
69 | ||
70 | //------------------------------------- | |
71 | /** | |
72 | * | |
73 | * Applies the operator to the given value | |
74 | **/ | |
75 | public Object apply (Object pLeft, | |
76 | 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, | |
85 | getOperatorSymbol())); | |
86 | } | |
87 | 0 | return PrimitiveObjects.getInteger (0); |
88 | } | |
89 | ||
90 | 0 | if (Coercions.isBigDecimal(pLeft) || |
91 | Coercions.isBigInteger(pLeft) || | |
92 | Coercions.isBigDecimal(pRight) || | |
93 | Coercions.isBigInteger(pRight)) { | |
94 | ||
95 | 0 | BigDecimal left = (BigDecimal) |
96 | Coercions.coerceToPrimitiveNumber(pLeft, BigDecimal.class); | |
97 | 0 | BigDecimal right = (BigDecimal) |
98 | Coercions.coerceToPrimitiveNumber(pRight, BigDecimal.class); | |
99 | ||
100 | try { | |
101 | 0 | return left.divide(right, BigDecimal.ROUND_HALF_UP); |
102 | 0 | } catch (Exception exc) { |
103 | 0 | if (log.isErrorEnabled()) { |
104 | 0 | String message = MessageUtil.getMessageWithArgs( |
105 | Constants.ARITH_ERROR, | |
106 | getOperatorSymbol(), | |
107 | "" + left, | |
108 | "" + right); | |
109 | 0 | log.error(message); |
110 | 0 | throw new ELException(message); |
111 | } | |
112 | 0 | return PrimitiveObjects.getInteger(0); |
113 | } | |
114 | } else { | |
115 | ||
116 | 0 | double left = |
117 | Coercions.coerceToPrimitiveNumber(pLeft, Double.class). | |
118 | doubleValue(); | |
119 | 0 | double right = |
120 | Coercions.coerceToPrimitiveNumber(pRight, Double.class). | |
121 | doubleValue(); | |
122 | ||
123 | try { | |
124 | 0 | return PrimitiveObjects.getDouble(left / right); |
125 | 0 | } catch (Exception exc) { |
126 | 0 | if (log.isErrorEnabled()) { |
127 | 0 | String message = MessageUtil.getMessageWithArgs( |
128 | Constants.ARITH_ERROR, | |
129 | getOperatorSymbol(), | |
130 | "" + left, | |
131 | "" + right); | |
132 | 0 | log.error(message); |
133 | 0 | throw new ELException(message); |
134 | } | |
135 | 0 | return PrimitiveObjects.getInteger(0); |
136 | } | |
137 | } | |
138 | } | |
139 | ||
140 | //------------------------------------- | |
141 | } |