1 55 56 package org.apache.commons.el; 57 58 import javax.servlet.jsp.el.ELException ; 59 import java.math.BigInteger ; 60 61 68 69 public class ModulusOperator 70 extends BinaryOperator 71 { 72 76 public static final ModulusOperator SINGLETON = 77 new ModulusOperator (); 78 79 84 public ModulusOperator () 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 ((pLeft != null && 121 (Coercions.isFloatingPointType (pLeft) || 122 Coercions.isFloatingPointString (pLeft))) || 123 Coercions.isBigDecimal(pLeft) || 124 (pRight != null && 125 (Coercions.isFloatingPointType (pRight) || 126 Coercions.isFloatingPointString (pRight) || 127 Coercions.isBigDecimal(pRight)))) { 128 double left = 129 Coercions.coerceToPrimitiveNumber (pLeft, Double .class, pLogger). 130 doubleValue (); 131 double right = 132 Coercions.coerceToPrimitiveNumber (pRight, Double .class, pLogger). 133 doubleValue (); 134 135 try { 136 return PrimitiveObjects.getDouble (left % right); 137 } 138 catch (Exception exc) { 139 if (pLogger.isLoggingError ()) { 140 pLogger.logError 141 (Constants.ARITH_ERROR, 142 getOperatorSymbol (), 143 "" + left, 144 "" + right); 145 } 146 return PrimitiveObjects.getInteger (0); 147 } 148 } 149 else if (Coercions.isBigInteger(pLeft) || Coercions.isBigInteger(pRight)) { 150 BigInteger left = (BigInteger ) 151 Coercions.coerceToPrimitiveNumber(pLeft, BigInteger .class, pLogger); 152 BigInteger right = (BigInteger ) 153 Coercions.coerceToPrimitiveNumber(pRight, BigInteger .class, pLogger); 154 155 try { 156 return left.remainder(right); 157 } catch (Exception exc) { 158 if (pLogger.isLoggingError()) { 159 pLogger.logError 160 (Constants.ARITH_ERROR, 161 getOperatorSymbol(), 162 "" + left, 163 "" + right); 164 } 165 return PrimitiveObjects.getInteger(0); 166 } 167 } 168 else { 169 long left = 170 Coercions.coerceToPrimitiveNumber (pLeft, Long .class, pLogger). 171 longValue (); 172 long right = 173 Coercions.coerceToPrimitiveNumber (pRight, Long .class, pLogger). 174 longValue (); 175 176 try { 177 return PrimitiveObjects.getLong (left % right); 178 } 179 catch (Exception exc) { 180 if (pLogger.isLoggingError ()) { 181 pLogger.logError 182 (Constants.ARITH_ERROR, 183 getOperatorSymbol (), 184 "" + left, 185 "" + right); 186 } 187 return PrimitiveObjects.getInteger (0); 188 } 189 } 190 } 191 192 } 194 | Popular Tags |