(
int op1,
int op2
)
| 85 | * @throws ArithmeticException if int-range overflows |
| 86 | */ |
| 87 | public static int safeAdd( |
| 88 | int op1, |
| 89 | int op2 |
| 90 | ) { |
| 91 | |
| 92 | if (op2 == 0) { |
| 93 | return op1; |
| 94 | } |
| 95 | |
| 96 | long result = ((long) op1) + ((long) op2); |
| 97 | |
| 98 | if ((result < Integer.MIN_VALUE) || (result > Integer.MAX_VALUE)) { |
| 99 | StringBuilder sb = new StringBuilder(32); |
| 100 | sb.append("Integer overflow: ("); |
| 101 | sb.append(op1); |
| 102 | sb.append(','); |
| 103 | sb.append(op2); |
| 104 | sb.append(')'); |
| 105 | throw new ArithmeticException(sb.toString()); |
| 106 | } else { |
| 107 | return (int) result; |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * <p>Sums up the numbers with range check. </p> |
no test coverage detected