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.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
29
30
31
32
33
34 public class DivideOperator
35 extends BinaryOperator
36 {
37
38
39
40 private static Log log = LogFactory.getLog(DivideOperator.class);
41
42
43
44
45
46 public static final DivideOperator SINGLETON =
47 new DivideOperator ();
48
49
50
51
52
53
54 public DivideOperator ()
55 {
56 }
57
58
59
60
61
62
63
64
65 public String getOperatorSymbol ()
66 {
67 return "/";
68 }
69
70
71
72
73
74
75 public Object apply (Object pLeft,
76 Object pRight)
77 throws ELException
78 {
79 if (pLeft == null &&
80 pRight == null) {
81 if (log.isWarnEnabled()) {
82 log.warn(
83 MessageUtil.getMessageWithArgs(
84 Constants.ARITH_OP_NULL,
85 getOperatorSymbol()));
86 }
87 return PrimitiveObjects.getInteger (0);
88 }
89
90 if (Coercions.isBigDecimal(pLeft) ||
91 Coercions.isBigInteger(pLeft) ||
92 Coercions.isBigDecimal(pRight) ||
93 Coercions.isBigInteger(pRight)) {
94
95 BigDecimal left = (BigDecimal)
96 Coercions.coerceToPrimitiveNumber(pLeft, BigDecimal.class);
97 BigDecimal right = (BigDecimal)
98 Coercions.coerceToPrimitiveNumber(pRight, BigDecimal.class);
99
100 try {
101 return left.divide(right, BigDecimal.ROUND_HALF_UP);
102 } catch (Exception exc) {
103 if (log.isErrorEnabled()) {
104 String message = MessageUtil.getMessageWithArgs(
105 Constants.ARITH_ERROR,
106 getOperatorSymbol(),
107 "" + left,
108 "" + right);
109 log.error(message);
110 throw new ELException(message);
111 }
112 return PrimitiveObjects.getInteger(0);
113 }
114 } else {
115
116 double left =
117 Coercions.coerceToPrimitiveNumber(pLeft, Double.class).
118 doubleValue();
119 double right =
120 Coercions.coerceToPrimitiveNumber(pRight, Double.class).
121 doubleValue();
122
123 try {
124 return PrimitiveObjects.getDouble(left / right);
125 } catch (Exception exc) {
126 if (log.isErrorEnabled()) {
127 String message = MessageUtil.getMessageWithArgs(
128 Constants.ARITH_ERROR,
129 getOperatorSymbol(),
130 "" + left,
131 "" + right);
132 log.error(message);
133 throw new ELException(message);
134 }
135 return PrimitiveObjects.getInteger(0);
136 }
137 }
138 }
139
140
141 }