Upload files to "java/expression/exceptions"

This commit is contained in:
2026-04-13 10:54:33 +03:00
parent 6ab37c41ec
commit ea33166b32
5 changed files with 173 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
package expression.exceptions;
import java.math.BigDecimal;
import java.math.BigInteger;
import expression.*;
/**
* @author Doschennikov Nikita (me@fymio.us)
*/
public class CheckedAdd extends AbstractBinaryOperation {
public CheckedAdd(AbstractExpression l, AbstractExpression r) { super(l, r); }
@Override protected String getOperator() { return "+"; }
@Override protected int getPriority() { return 1; }
@Override protected boolean isRightAssoc() { return false; }
@Override
protected int applyInt(int a, int b) {
int result = a + b;
if (((a ^ result) & (b ^ result)) < 0) {
throw new OverflowException("addition");
}
return result;
}
@Override protected BigInteger applyBi(BigInteger a, BigInteger b) { return a.add(b); }
@Override protected BigDecimal applyBd(BigDecimal a, BigDecimal b) { return a.add(b); }
}