Files
paradigms/java/expression/exceptions/CheckedMultiply.java
2026-04-08 21:25:17 +03:00

32 lines
1.1 KiB
Java

package expression.exceptions;
import java.math.BigDecimal;
import java.math.BigInteger;
import expression.*;
/**
* @author Doschennikov Nikita (me@fymio.us)
*/
public class CheckedMultiply extends AbstractBinaryOperation {
public CheckedMultiply(AbstractExpression l, AbstractExpression r) { super(l, r); }
@Override protected String getOperator() { return "*"; }
@Override protected int getPriority() { return 2; }
@Override protected boolean isRightAssoc() { return false; }
@Override
protected int applyInt(int a, int b) {
if (a == 0 || b == 0) return 0;
if (a == Integer.MIN_VALUE && b == -1) throw new OverflowException("multiplication");
if (b == Integer.MIN_VALUE && a == -1) throw new OverflowException("multiplication");
int result = a * b;
if (result / a != b) {
throw new OverflowException("multiplication");
}
return result;
}
@Override protected BigInteger applyBi(BigInteger a, BigInteger b) { return a.multiply(b); }
@Override protected BigDecimal applyBd(BigDecimal a, BigDecimal b) { return a.multiply(b); }
}