1 55 56 package org.apache.commons.el; 57 58 import javax.servlet.jsp.el.ELException ; 59 import java.math.BigDecimal ; 60 61 68 69 public class DivideOperator 70 extends BinaryOperator 71 { 72 76 public static final DivideOperator SINGLETON = 77 new DivideOperator (); 78 79 84 public DivideOperator () 85 { 86 } 87 88 95 public String getOperatorSymbol () 96 { 97 return "/"; 98 } 99 100 105 public Object apply (Object pLeft, 106 Object pRight, 107 Logger pLogger) 108 throws ELException 109 { 110 if (pLeft == null && 111 pRight == null) { 112 if (pLogger.isLoggingWarning ()) { 113 pLogger.logWarning 114 (Constants.ARITH_OP_NULL, 115 getOperatorSymbol ()); 116 } 117 return PrimitiveObjects.getInteger (0); 118 } 119 120 if (Coercions.isBigDecimal(pLeft) || 121 Coercions.isBigInteger(pLeft) || 122 Coercions.isBigDecimal(pRight) || 123 Coercions.isBigInteger(pRight)) { 124 125 BigDecimal left = (BigDecimal ) 126 Coercions.coerceToPrimitiveNumber(pLeft, BigDecimal .class, pLogger); 127 BigDecimal right = (BigDecimal ) 128 Coercions.coerceToPrimitiveNumber(pRight, BigDecimal .class, pLogger); 129 130 try { 131 return left.divide(right, BigDecimal.ROUND_HALF_UP); 132 } catch (Exception exc) { 133 if (pLogger.isLoggingError()) { 134 pLogger.logError 135 (Constants.ARITH_ERROR, 136 getOperatorSymbol(), 137 "" + left, 138 "" + right); 139 } 140 return PrimitiveObjects.getInteger(0); 141 } 142 } else { 143 144 double left = 145 Coercions.coerceToPrimitiveNumber(pLeft, Double .class, pLogger). 146 doubleValue(); 147 double right = 148 Coercions.coerceToPrimitiveNumber(pRight, Double .class, pLogger). 149 doubleValue(); 150 151 try { 152 return PrimitiveObjects.getDouble(left / right); 153 } catch (Exception exc) { 154 if (pLogger.isLoggingError()) { 155 pLogger.logError 156 (Constants.ARITH_ERROR, 157 getOperatorSymbol(), 158 "" + left, 159 "" + right); 160 } 161 return PrimitiveObjects.getInteger(0); 162 } 163 } 164 } 165 166 } 168 | Popular Tags |